无法找到由“require”加载的@INC Perl perl文件

问题描述 投票:1回答:1

我有一个简单的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.

[EDIT 1]

如果我登录到远程计算机并运行相同的脚本,则没有错误。

[EDIT 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)

如何摆脱这个问题?

perl ssh
1个回答
4
投票

如果您需要使用require,请提供完整路径

require "/full/path/to/common.pl";

即使文件位于同一目录中,这也需要ssh,因为那时.中的@INC不是脚本的目录(但可能是你的HOME)。在其他情况下也会发生这种情况。

请注意,这样您将导入common.pl中的所有内容。

使用适当的模块有许多优点。然后该文件是.pm,按照惯例,文件名是大写的(并且是驼峰式的)。

这是一个简单的例子,文件bin/mail.pllib/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.pmlibdir你只需要说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是有用的,特别是如果呼叫者导入的符号列表变长。

© www.soinside.com 2019 - 2024. All rights reserved.