我有一个简单的Perl脚本,它使用位于另一个文件common.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
#...
#!/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.
如果我登录到远程计算机并运行相同的脚本,则没有错误。
我也尝试将common.pl
转换为模块:
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
,因为那时.
中的@INC
不是脚本的目录(但可能是你的HOME
)。在其他情况下也会发生这种情况。
请注意,这样您将导入common.pl
中的所有内容。
使用适当的模块有许多优点。然后该文件是.pm
,按照惯例,文件名是大写的(并且是驼峰式的)。
这是一个简单的例子,文件bin/mail.pl
和lib/Common.pm
斌/ main.pl
use warnings;
use strict;
use FindBin qw($RealBin); # Directory in which the script lives
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;
当一个包是used时,它的文件是第一个required,然后模块的import方法在编译时运行。通过import
,调用者实际上获取了模块中定义的符号(函数和变量的名称),并且我们必须在模块中提供import
方法(或者在调用者中使用完全限定名,Module::function
)。
与use Exporter
的线路引入了import
例程,因此我们不必编写自己的例程。旧版本的Exporter通过继承使用,通常由@ISA = ('Exporter')
使用。查看文档。
然后通过@EXPORT_OK
提供符号。这要求呼叫者列出要使用的功能;默认情况下,它不会将任何内容“推送”到其命名空间中。还有%EXPORT_TAG
是有用的,特别是如果呼叫者导入的符号列表变长。