我有一个简单的 Perl 脚本,它使用位于另一个文件 common.pl
中的辅助函数:
main.pl
#!/usr/bin/perl
use strict;
use warnings;
use Cwd 'abs_path';
use File::Basename qw( fileparse );
use File::Path qw( make_path );
use File::Spec;
require "common.pl"; # line 15
#...
common.pl
#!/usr/bin/perl
use strict;
use warnings;
sub getTimeLoggerHelper{
#....
}
1;
本地一切运行良好,但当我尝试通过 ssh 运行它时,出现错误:
Can't locate common.pl in @INC (@INC contains:
/Library/Perl/5.18/darwin-thread-multi-2level /Library/Perl/5.18
/Network/Library/Perl/5.18/darwin-thread-multi-2level
/Network/Library/Perl/5.18
/Library/Perl/Updates/5.18.2/darwin-thread-multi-2level
/Library/Perl/Updates/5.18.2
/System/Library/Perl/5.18/darwin-thread-multi-2level
/System/Library/Perl/5.18
/System/Library/Perl/Extras/5.18/darwin-thread-multi-2level
/System/Library/Perl/Extras/5.18 .) at /Users/snaggs/scripts/main.pl line 15.
[编辑1]
如果我登录到远程计算机并运行相同的脚本,则不会出现错误。
[编辑2]
我还尝试将 common.pl
转换为模块:
common.pm
package Common;
use strict;
use warnings;
sub getTimeLoggerHelper{
#....
}
1;
__END__
从 main.pl
我这样调用它
use Module::Load;
load Common;
同样的问题,本地工作,来自 ssh - 相同的错误:
Can't locate Common.pm in @INC (you may need to install the Common module)
如何解决这个问题?
最佳答案
如果您需要使用require
为此,请提供完整路径
require "/full/path/to/common.pl";
这需要 ssh
即使该文件与脚本位于同一目录中,因为超过 ssh
.
在@INC
不是脚本的目录(因为工作目录 .
可能是您的 $HOME
)。在其他情况下也会发生这种情况。†
请注意,这样您将导入 common.pl
中的所有内容 .
使用适当的模块有很多优点。那么文件就是.pm
按照惯例,文件名是大写的(并且是 Pascal 大小写的)。
这是一个包含文件 bin/main.pl
的简单示例和lib/Common.pm
bin/main.pl
use warnings;
use strict;
use FindBin 1.51 qw($RealBin); # Directory in which the script is
use lib "$RealBin/../lib"; # Where modules are, relative to $RealBin
use Common qw(test_me);
test_me();
设置的关键部分@INC
,在哪里寻找模块,
完成 lib杂注。它将目录添加到默认 @INC
的开头,在编译时。 FindBin的$RealBin
是脚本的目录,已解析链接。我们使用它,以便添加的路径是相对于脚本的,而不是硬编码的。当脚本及其库结合在一起时,这有助于源组织。
另一种设置方法是通过 environment variable PERL5LIB
。与bash
export PERL5LIB=/path/to/libdir
然后是Module.pm
那就是libdir
你只需要说use Module
它将会被发现。这对于位于特定位置并由各种脚本使用的模块非常有用。
lib/Common.pm
package Common;
use strict;
use warnings;
use Exporter qw(import);
our @EXPORT_OK = qw( test_me );
sub test_me { print "Hello from ", __PACKAGE__, "\n" }
1;
当包裹是use时d 它的文件是第一个 require d 然后是模块的 import方法在编译时运行。通过import
调用者实际上获取了模块中定义的符号(函数和变量的名称),我们必须提供 import
模块中的方法(或在调用者中使用完全限定名称 Module::function
)。
带有 use Exporter
的行引入其 import
例程,所以我们不必自己编写。使用旧版本的 Exporter这是通过继承使用的,通常由@ISA = ('Exporter')
。请参阅文档。
然后可以通过@EXPORT_OK
获得符号。这需要调用者列出要使用的函数;默认情况下,它不会将任何内容“推送”到其 namespace 中。还有%EXPORT_TAG
这很有帮助,特别是当调用者导入的符号列表变长时。
† 这实际上只是使用当前 working directory 时出现的一般问题的一个示例。 ( .
) 对于脚本的目录:不是;一般来说,事实并非如此。
还有一点需要注意,从 5.26 版本开始,.
(当前目录)is not anymore在@INC
,出于安全原因。这也可能已向后移植到早期版本。
所以只需使用 $RealBin
,如图所示(1.51 版本有一个重要的修复)。
关于perl - 无法在 "require"加载的 @INC Perl perl 文件中定位,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41414782/