这是一个带有2个基本函数的math.pm模块add和multiply:
package Math; use strict; use warnings; use Exporter qw(import); our @EXPORT_OK = qw(add multiply); sub add { my ($x,$y) = @_; return $x + $y; } sub multiply { my ($x,$y) = @_; return $x * $y; } 1;
#!/usr/bin/perl use strict; use warnings; use Math qw(add); print add(19,23);
它给出了一个错误:
can’t locate math.pm in @INC <@INC contain: C:/perl/site/lib C:/perl/lib .> at C:\programs\script.pl line 5.
BEGIN Failed–compilation aborted at C:\programs\script.pl line 5.
如何解决这个问题呢?
解决方法
use lib
Adding a use lib statement to the script will add the directory to @INC for that specific script. Regardless who and in what environment runs it.
You just have to make sure to have the use lib statement before trying to load the module:
use lib '/path/to/module'; use Math qw(add);
有关设置@INC的更多详细信息,请查看以下内容:
How do I include a Perl module that’s in a different directory