Perl use module, keep getting Undefined subroutine &main error - mysql

I have a perl module DB.pm, inside is somthing like:
package GUI::DB;
use strict;
use DBI;
use vars qw(#ISA #EXPORT);
use Exporter;
#ISA = qw(Exporter);
#EXPORT = qw(fun);
sub fun {
my $dsn = "DBI:mysql:database=test";
return $dsn;
}
Then I wrote test.pl:
#!/usr/bin/perl
use strict;
use warnings;
use lib '~/Downloads/GUI'; #Here is the path of the DB.pm module.
use DB;
my $aa = fun();
I have been trying to fix it for hours, I tried to use comment perl -l /path/to/file aa.pl and it gave me no error but the script didn't run at all, I am purely new to Perl, really stuck.
Please help me.
EDIT: So the module's name is DB.pm and folder's name is GUI now, and I an using use DB in my script, still doesn't work, where should I save the DB.pm file?

use HA; does a couple of things. First, it finds a file HA.pm in the perl library paths (#INC). Second, it calls HA::->import() to allow the HA module to do whatever initialization/exporting it wants; this relies on the module's package matching its filename. If it doesn't, this initialization is quietly skipped (method calls to an import method don't generate an error even if the method does not exist).
So explicitly call import on the package you want, or make the package name match the filename.

Perl doesn't understand ~, see How do I find a user's home directory in Perl?
You also need to give use lib the directory which GUI/DB.pm is in and use GUI::DB:
use lib $ENV{HOME}."/Downloads";
use GUI::DB;

Related

Cython and Exec()?

If I made a python file named hello.py that has a script made like this.
msg = input("insert your message here: ")
script = '''
def say_something():
print("{msg}")
'''
exec(script)
say_something()
And then I tried to use Cython
from distutils.core import setup
from Cython.Build import cythonize
setup(
ext_modules=cythonize("Hello.py")
)
It will show an error like this: undeclared name not builtin: say_something
I do understand why this happens but I'm not really an expert with python and C just yet. This is just an example, but it's similar to what I'm trying to do with one of my projects. Is there any way I could resolve this? I want to find a way to convert the script string into C as well.
I was trying to build an editable python script.
Cython compiles the Python functions to a native binary that does what the CPython interpreter should do. exec is a function that execute arbitrary code at runtime (which is generally a very bad idea for speed, maintainability/readability and security). Cython does not support exec because it would mean that the could would be compiled at runtime. Thus, the code executed by exec cannot be a Cython code. However, the exec function can still be used to execute a pure-Python code. The error can be removed by turning off the Cython.Compiler.Options.error_on_unknown_names in the setup script (just before calling setup) as pointed out by #DavidW. With this Cython will not complain when it does not find a function defined by exec (or similar methods). Please keep in mind that CPython can only be used in this case instead of Cython (which partially defeat the purpose of using Cython in the first place).

How to tell PhpStorm that function argument is file path

In PhpStorm (and other JetBrains IDE), is it possible to make function attributes as file / resource path?
E.g. in this function:
function mix($file): string
{
// check mix maninfest for $file and return path from mix manifest
return $fire_path_with_cachebuster;
}
I'd like PHP to suggest files in the project as I define $file attribute when calling mix function.
Only manually for the moment, when calling that function 😒. And it's a temp injection (for a session) so it's not convenient:
mix('')
Place caret inside the string parameter
Use Alt + Enter (or via light bulb icon) to bring the Quick Fix / Intentions menu (on some Keymaps it might be different one)
Use "Inject language or reference" option
Then choose "File Reference" entry there (just start typing to filter the list).
The result:
Hopefully they will implement the following tickets for a permanent solution:
Using #[Language] PHP attribute at the function declaration: https://youtrack.jetbrains.com/issue/WI-56996
Or in-place via PHPDoc-like comment (before the parameter when calling that function): https://youtrack.jetbrains.com/issue/WI-20028
Watch those tickets (star/vote/comment) to get notified on any progress (and hopefully speed it up by bringing dev's attention).
Like LazyOne stated, there is currently no way to declare a parameter as being a file reference.
However, you can get a more permanent File Reference "injection" by [mis]using __DIR__.
PhpStorm considers a string mixed with the __DIR__ constant to be a file path:
It isn't perfect as it depends on what directory you are currently located in. If you only want the filename passed to your method, you can wrap the string in basename, or handle that from within your method.
echo mix(basename(__DIR__ . '/slack_bot.php'));

How to register coffeescript transpiler in ES6 way?

Previously I've been using following in my JS entry points:
require('coffeescript/register');
module.exports = require('./entry.coffee');
What is the corresponding ES6 syntax of this?
Following does not seem to register anyhing.
import 'coffeescript/register';
export * from 'entry.coffee';
Error is:
Cannot find module 'entry.coffee'
Tested on Coffeescript 2.0 beta2.
Update:
Changing path to relative:
import 'coffeescript/register';
export * from './entry.coffee';
finds the entry.coffee, but treats it as JS. Hence, Coffeescript is not handled by transpiler.
You don't need to use coffeescript/register if you're transpiling the CoffeeScript as part of your bundling process (which you need to, if you're using Rollup) — that's just a way to enable Node to run CoffeeScript files without having to first convert them.
Instead, try adding rollup-plugin-coffee-script to your rollup.config.js file.

Using JUnit in Jython - NameError for assertTrue

Environment Details
Mac OS X 10.9
Oracle JDK 1.7.0_55 64-bit
jython-standalone-2.5.3.jar
junit-4.11
What I have done so far
I have added the junit jar to /Library/Java/Extensions.
I invoked Jython as follows java -jar jython-standalone-2.5.3.jar
In the Jython interpreter, I imported the following import org.junit.Assert, and this import was successful.
Problem
When I tried to use assertTrue, I got a NameError in the interpreter. Why is this so?
I understand that assertTrue is a static method. Not sure what implication this has when I try to use it in Jython.
Additional Context
I am using XMLUnit in Jython. Was able to successfully import the Diff class from org.custommonkey.xmlunit in Jython. Also able to use the methods in this class, and call them on a Diff object. The result of this method call is what I am trying to pass to assertTrue, when it throws the error.
from org.custommonkey.xmlunit import Diff
import org.junit.Assert
xml1 = ...some XML string...
xml2 = ...some XML string...
myDiff = Diff(xml1, xml2)
assertTrue(myDiff.similar())
Hope this additional information is useful in identifying a solution to this problem.
Latest Status
I narrowed it down to setting this property python.security.respectJavaAccessibility = false, since the Assert() constructor is protected.
Still trying to get it to work. Any help is greatly appreciated.
Figured it out.
In addition to junit.jar file, the hamcrest-core.jar file also needed to be copied to /Library/Java/Extensions.
Then I got rid of the jython.jar file, and instead installed it using the jython installer.
After the installation was completed, I updated the registry file in the installation folder, specifically setting this property python.security.respectJavaAccessibility = false.
Now I am able to see the assertTrue method, and no longer getting a NameError.

Why aren't namespace autoload classes loading in phpunit tests?

To compliment an existing smorgasbord of arrangements between phpunit,autoload and namespace is this:
I have created a simple test project that runs PhpUnit tests and uses namespace autoloading. I register the autoloading in the bootstrap file like so:
set_include_path(get_include_path() . PATH_SEPARATOR . "/path/to/classes/folder");
spl_autoload_register();
and inside a unit test I load and test my class like so:
$obj = new \some\space\someClass(); // which is in the classes/some/space folder
$this->assertTrue($obj->foo()=='bar');
And I get an error
Fatal error: Class '\some\space\someClass' not found in testSomeClass.php...
While this is not phpunit specific you need to change:
spl_autoload_register();
to
spl_autoload_register('spl_autoload');
Any other component that registers an autoloader unregisters the default __autoload().
If your code has an existing __autoload function then this function must be explicitly registered on the __autoload stack. This is because spl_autoload_register() will effectively replace the engine cache for the __autoload function by either spl_autoload() or spl_autoload_call().
So this is how spl-autoload works together with anything else that uses autoloading.
Make sure your path to classes folder is made relative to script which is run for tests execution. If the script is in subfolder of your projects (for example tests/) then your path should be ../path/to/classes/folder
That's what I have in my tests/bootstrap.php
set_include_path(dirname(__FILE__).'/../classes'.PATH_SEPARATOR.get_include_path());
spl_autoload_extensions('.php');
spl_autoload_register('spl_autoload');