我正在尝试在/usr/local/lib/perl中创建自己的Perl模块。
我设置了环境变量PERL5LIB:
$ env | grep PERL
PERL5LIB=/usr/local/lib/perl
如果我创建一个模块:$PERL5LIB/My/ModuleTest.pm
$ ./test.pl
Can't locate object method "new" via package "My::ModuleTest" (perhaps you forgot to load "My::ModuleTest"?) at ./test.pl line 8.
test.pl:
#!/usr/bin/perl
use strict;
use warnings;
use My::ModuleTest;
my $test = new My::ModuleTest;
print $test->check;
ModuleTest.pm:
package ModuleTest;
use strict;
use warnings;
sub new {
my $class = shift;
my ($opts)= @_;
my $self = {};
$self->{test} = "Hello World";
return bless $self, $class;
}
sub check {
my $self = shift;
my ($opts) = @_;
return $self->{test};
}
1;
我希望使用$PERL5LIB作为我的模块库路径,以将它们与安装目录分开。
Perl @INC:
$ perl -le 'print foreach @INC'
/usr/local/lib/perl
/usr/lib/perl5/site_perl/5.8.8/i386-linux-thread-multi
/usr/lib/perl5/site_perl/5.8.8
/usr/lib/perl5/site_perl
/usr/lib/perl5/vendor_perl/5.8.8/i386-linux-thread-multi
/usr/lib/perl5/vendor_perl/5.8.8
/usr/lib/perl5/vendor_perl
/usr/lib/perl5/5.8.8/i386-linux-thread-multi
/usr/lib/perl5/5.8.8
.
my $test = My::ModuleTest->new();
来避免某些陷阱。 - Konerak