Calling a Perl Module which is Installed in different path/directory

删除回忆录丶 提交于 2020-12-06 04:27:08

问题


I am writing a Perl script which does -

  • 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.

Actually these three Perl Modules have been installed in different directories.

Lets say -

  • Net::OpenSSH is installed in /path/to/lib1
  • Log::Log4perl is installed in /path/to/lib2
  • Text::CSV is installed in /path/to/lib3

Since these Perl Modules has been installed in different locations, I am mentioning the respective paths in Shebang line like below:

Method1:

#!/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
..

This works perfectly fine for me.

I found one more method to call these Perl modules like below:

Method 2:

#!/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.

I found the standard way to call these Perl modules by the use of FindBin.

I am confused with its syntax. How can I achieve same using FindBin Perl Module.

Method 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";
...

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 ...

Where I am doing wrong in Method 3?


回答1:


$RealBin is the directory in which the script is located. ($Bin is a broken version of the same thing.)

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.

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 is usually used when the script and modules are packaged together (part of the same project). Same for use lib, really.



来源:https://stackoverflow.com/questions/63915444/calling-a-perl-module-which-is-installed-in-different-path-directory

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!