Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 922
vkk05 Avatar asked Oct 19 '25 15:10

vkk05


1 Answers

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

like image 136
ikegami Avatar answered Oct 21 '25 05:10

ikegami



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!