在Perl中,无法在@INC错误中找到packgeName.pm

前端之家收集整理的这篇文章主要介绍了在Perl中,无法在@INC错误中找到packgeName.pm前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
这是一个带有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;

这是调用add函数的脚本script.pl:

#!/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

猜你在找的Perl相关文章