I'm packaging some common functions in a small Perl module, that I load in the script using
use lib path/to/lib
In the module file I imported some other system installed modules (e.g. Carp qw(confess), but I cannot invoke confess directly, but rather Carp::confess, which is unusual to me.
This is my (non)-working example:
https://github.com/telatin/bioinfo/blob/master/mini/script.pl
use 5.012;
use FindBin qw($Bin);
use lib "$Bin/Demo/lib";
use Local::Module;
say "Version: ", $Local::Module::VERSION;
Local::Module->new();
The module: https://github.com/telatin/bioinfo/blob/master/mini/Demo/lib/Local/Module.pm
use 5.012;
use warnings;
use Carp qw(confess);
package Local::Module;
$Local::Module::VERSION = 2;
sub new {
my ($class, $args) = #_;
my $self = {
debug => $args->{debug},
};
my $object = bless $self, $class;
confess "Unable to create fake object";
return $object;
}
1;
What should I do in the .pm file to avoid this problem?
The problem is here:
use 5.012;
use warnings;
use Carp qw(confess);
package Local::Module;
First you load Carp and import confess, but at that point you're still in package main, so confess is imported into main.
Then you switch packages with package Local::Module, but there is no confess function defined here.
You need to switch packages first:
package Local::Module;
use 5.012;
use warnings;
use Carp qw(confess);
Now all imports and all the following code are in the same package.
Related
I tried to using Module::Load to dynamically load plugin from directory, but it doesn't work, even after maximal simplifing the code.
Main file:
#!/usr/bin/perl
use strict;
use warnings;
use Module::Load;
Module::Load::load_remote('Package', 'plugins/Blank.pm') or die;
plugins/Blank.pm:
#!/usr/bin/perl
package plugins::Blank;
use strict;
use warnings;
1;
But it still die. When i remove '...or die' part, it no dies, but Package still not loaded.
Module::Load::load_remote dies when failing, but does not return anything. Your example works fine for me, and the package is loaded. If it doesn't load the package it should die inside load_remote. load_remote only loads the functions that are exported into the other namespace. It also only seem to work if the other module is loaded as a module name, instead of a filename:
load.pl:
#!/usr/bin/perl
use strict;
use warnings;
use Module::Load;
Module::Load::load_remote("Package","plugins::Blank","hw");
Package::hw();
plugins/Blank.pm:
package plugins::Blank;
use strict;
use warnings;
use Exporter qw(import);
our #EXPORT_OK = qw(hw);
sub hw {
print "Hello World\n";
}
1;
Outputs:
Hello World
But to achieve what you want you can probably use an approach like this:
#!/usr/bin/perl
use strict;
use warnings;
use Module::Load;
my $pkg = 'plugins::Blank';
Module::Load::load($pkg);
$pkg->hw();
I have a very small module and I am having trouble with exporting the functions from it.
Take the function GetVar() if I call it using TestMod::GetVar() It will work fine, however when I call GetVar() from the script in which the module is imported I get an error (listed below). I am 99% sure that I have not exported the function GetVar() correctly, however I can not find my mistake.
Module source:
#!/usr/bin/perl
use strict;
package TestMod;
our (#ISA, #EXPORT, $VERSION);
use Exporter;
#ISA = qw(Exporter);
#EXPORT = qw(&GetVar);
$VERSION = 0.01;
sub GetVar()
{
return "something";
}
1;
Script source:
#!/usr/bin/perl
use strict;
use lib "/home/user1221/projects/";
use module;
print "Version: $TestMod::VERSION";
my $a = GetVar(); #should work but doesn't
print "$a\n";
Error:
Undefined subroutine &main::GetVar called at /home/user1221/projects/module_use.pl line 8.
use module; in your script is exact equivalent of
BEGIN { require module; module->import(); }
but since you don't have module package nothing is imported.
What you need is
BEGIN { require module; TestMod->import(); }
or make sure your module file names follow package names.
How does Perl resolve subroutine naming conflicts when parsing modules in the #INC path?
I'm writing a Perl module and I don't want my subroutine names to conflict with anything that already exists.
This should shed some light here.
Test script
#!/usr/bin/perl
use warnings;
use strict;
# import 'print_stuff' from TestPackage
# so you can call this sub without package name
use TestPackage qw(print_stuff);
my $string = "whatever";
TestPackage::print_stuff($string);
print_stuff($string);
sub print_stuff {
my $str = shift;
print "test.pl: $str\n";
}
Test package
package TestPackage;
# Export subs, which will enable you to call these
# subs without package name(TestPackage)
require Exporter;
#ISA = qw(Exporter);
#EXPORT_OK = qw(print_stuff);
sub print_stuff {
my $str = shift;
print "TestPackage: $str\n";
}
1;
Output:
$ test.pl
TestPackage: whatever
test.pl: whatever
Use package to build separate namespaces in modules, so you can avoid this kind of problems. The package statement declares the compilation unit as being in the given namespace. This link may helps you.
I try write an perl_mod script, but it say: "Internal Server Error".
this is module file: (mm1.pm)
package mm1;
use strict;
use XML::Simple;
use DBI;
require Exporter;
our #ISA = qw(Exporter);
our #EXPORT_OK = qw(asd);
our #EXPORT = qw(asd);
sub asd(){
print "2222";
}
this is main file: (main.perl)
#!/usr/bin/perl -w
use strict;
use CGI qw/:standard/;
use CGI::Carp qw/fatalsToBrowser warningsToBrowser/;
use mm1;
print header(-type => 'text/html', -charset=>'utf-8');
print "asdasd";
If i try run without "use mm1;" this main.perl then succeed.
What is the problem?
Thanks!
That's not a mod_perl module, it's a CGI program.
The (first) problem is almost certainly the fact that you've omitted the 1; from the end of your module file. But looking in the web server error log will confirm that.
module.pm
package module;
use 5.012;
use warnings;
sub Parse
{
return 1;
}
1;
script.pl
#!/usr/bin/perl -w
use 5.012;
use warnings;
use lib 'C:/';
use module;
print Parse("value");
Stdout
Undefined subroutine &main::Parse
You need either to write:
print module::Parse("value");
or to change the module package to export the name Parse.
See http://perldoc.perl.org/perlmod.html#Perl-Modules for guidance in exporting symbols from your module.
(By the way, you should really name your module Module rather than module. Lowercase module-names are used for Perl built-in features like use warnings and use strict.)
Several things:
First, use Local as your module prefix. That way, if you just happen to have a module with the same name in your Perl installation, it will use yours. Call it "Local::Module". Then, create a Local directory, and name your module Module.pm.
The other thing you have to understand is that you define your module in another namespace. By default, everything is in the main namespace until you use the package statement. That creates another namespace that your package uses. This way, if your package has a function foo, and you've defined a function foo in your main program, they won't collide.
Thus, you have two choices: One (the preferred now) is to simply call your subroutine with the full package name prepended to it. The second is to export your subroutine names to your main program. This can cause problems with duplicate names, but you don't have to keep typing in the package name every time you call your subroutine.
Without Exporting the name
Local/Module.pm
# /usr/bin/env perl
# Local/Module.pm
package Local::Module;
use strict;
use warnings;
sub Parse {
my $value = shift; #Might as well get it.
print "I got a value of $value\n";
return $value;
}
1; #Need this or the module won't load
program.pl
# /usr/bin/env perl
# program.pl
use strict;
use warnings;
use Local::Module;
Local::Module::Parse("Foo");
With export:
Local/Module.pm
# /usr/bin/env perl
# Local/Module.pm
package Local::Module;
use strict;
use warnings;
use Exporter qw(import);
our #EXPORT_OK(Parse); #Allows you to export this name into your main program
sub Parse {
my $value = shift; #Might as well get it.
print "I got a value of $value\n";
return $value;
}
1; #Need this or the module won't load
program.pl
# /usr/bin/env perl
# program.pl
use strict;
use warnings;
use Local::Module qw(Parse);
Parse("Foo");