tôi đang cố gắng để tạo ra mô-đun Perl của riêng tôi trong/usr/local/lib/perlPerl module sử dụng
Tôi có biến môi trường PERL5LIB thiết lập:
$ env | grep PERL
PERL5LIB=/usr/local/lib/perl
Nếu tôi tạo ra một mô-đun: $ 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;
Tôi muốn sử dụng $ PERL5LIB như đường dẫn thư viện cho các module của tôi để tách chúng ra khỏi thư mục cài đặt.
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
.
vẻ lẻ: $ thử nghiệm của tôi = new của bạn :: ModuleTest; –
Oyvind: Nó hoạt động, mặc dù hầu hết người hâm mộ Perl sẽ khuyên bạn sử dụng 'my $ test = My :: ModuleTest-> new();' để tránh những cạm bẫy nhất định. – Konerak