perl - 无法在 "require"加载的 @INC Perl perl 文件中定位

标签 perl ssh

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

相关文章:

perl - 打开和保存文件时丢失编码

mysql - 为 mySQL 数据库实现定期匹配系统的正确方法是什么?

ssh - 如何在没有键盘/显示器的情况下在新的 raspbian Jessie 镜像上启用 SSH?

javascript - 在 Google Chrome 扩展程序中使用 SSH

c - 当我的 Perl 程序在 cmd.exe 中输出 UTF-8 编码的字符串时,为什么我会重复最后一个八位字节?

excel - Perl 使用 OLE 写入 Excel 工作表

linux - perl wkhtmltopdf错误: Unable to write to destination

python - Paramiko 在 Windows 上无法从 ssh-agent 找到 key

python - 在远程 Web 服务器上使用 Django 构建 Web 应用程序的正确方法

perl - Net::SSH::Perl 返回特定服务器的错误