调用安装在不同路径/目录中的 Perl 模块
我正在编写一个 Perl 脚本 -
I am writing a Perl script which does -
- SSH 到其中一台服务器并使用
Net::OpenSSH
Perl 模块执行一些操作. - 我希望使用
Log::Log4perl
Perl 模块将脚本的整个日志存储在某个日志文件中. - 我想使用
Text::CSV
Perl 模块将一些数据写入 CSV 文件.
- SSH to one of the server and do some operation using
Net::OpenSSH
Perl Module. - I want to have entire log of the script to be stored in certain log file using
Log::Log4perl
Perl Module. - I want to write some data to CSV file using
Text::CSV
Perl Module.
实际上这三个 Perl Module 已经安装在不同的目录中.
Actually these three Perl Modules have been installed in different directories.
让我们说 -
-
Net::OpenSSH
安装在/path/to/lib1 -
Log::Log4perl
安装在/path/to/lib2 -
Text::CSV
安装在/path/to/lib3
-
Net::OpenSSH
is installed in /path/to/lib1 -
Log::Log4perl
is installed in /path/to/lib2 -
Text::CSV
is installed in /path/to/lib3
由于这些 Perl 模块已经安装在不同的位置,我在 Shebang 行中提到了各自的路径,如下所示:
Since these Perl Modules has been installed in different locations, I am mentioning the respective paths in Shebang line like below:
方法一:
#!/usr/bin/perl -I/path/to/lib1 -I/path/to/lib2 -I/path/to/lib3
use strict;
use warnings;
use Net::OpenSSH;
use Log::Log4perl;
use Text::CSV;
#continue flow of the script
..
这对我来说非常好.
我找到了另一种调用这些 Perl 模块的方法,如下所示:
I found one more method to call these Perl modules like below:
方法二:
#!/usr/bin/perl
use strict;
use warnings;
use lib '/path/to/lib1';
use lib '/path/to/lib2';
use lib '/path/to/lib3';
...
以上方法对我来说也很好用.
Above method also works fine for me.
我通过使用 FindBin 找到了调用这些 Perl 模块的标准方法.
I found the standard way to call these Perl modules by the use of FindBin.
我对它的语法感到困惑.我如何使用 FindBin
Perl 模块实现相同的目标.
I am confused with its syntax. How can I achieve same using FindBin
Perl Module.
方法 3:
#!/usr/bin/perl
use strict;
use warnings;
use FindBin qw($Bin);
use lib "$Bin/path/to/lib1";
use lib "$Bin/path/to/lib2";
use lib "$Bin/path/to/lib3";
...
这会引发以下错误,当我使用前两种方法(方法 1、方法 2)时不会出现该错误.
This throws me following error which doesn't appears when I use first two methods (Method1, Method2).
unable to load Perl module IO::Pty: Can't locate IO/Pty.pm in @INC ...
我在方法 3 中哪里做错了?
Where I am doing wrong in Method 3?
$RealBin
是脚本所在的目录.($Bin
是同一事物的损坏版本.)
$RealBin
is the directory in which the script is located. ($Bin
is a broken version of the same thing.)
如果脚本所在的目录和模块所在的目录没有关系,那么使用$RealBin
是没有意义的.事实上,在这种情况下,使用 PERL5LIB
env var 比 use lib
通常更有意义.
If there's no relationship between the directory in which the script is located and the directory in which a module is located, it doesn't make sense to use $RealBin
. In fact, it usually makes more sense to use the PERL5LIB
env var than use lib
in such cases.
另一方面,如果脚本位于 /path/to/bin
,则使用相对于 $RealBin
的路径可能是有意义的.>
On the other hand, if the script was located in /path/to/bin
, it might make sense to use a path relative to $RealBin
.
use lib # In /path/to/bin/script,
"$RealBin/../lib1", # This is /path/to/bin/../lib1, or /path/to/lib1
"$RealBin/../lib2", # This is /path/to/bin/../lib2, or /path/to/lib2
"$RealBin/../lib3"; # This is /path/to/bin/../lib3, or /path/to/lib3
$RealBin
通常在脚本和模块打包在一起(同一项目的一部分)时使用.use lib
也一样,真的.
$RealBin
is usually used when the script and modules are packaged together (part of the same project). Same for use lib
, really.