Issue
I have following code: demo.pm
#! user/local/bin/perl
use strict;
use warnings;
package demo;
sub doit{
print("Inside DoIt\n");
my $a = shift();
my $b = shift();
print("$a\n");
}
The file demo.pm is located elsewhere and want to use relative path to fetch the module and run it
Using following from linux command throws error:
perl -M/sub1/sub2/demo.pm -e 'demo::doit('arg1')'
Error:
syntax error at -e line 0, near "use /sub1/"
Execution of -e aborted due to compilation errors.
Solution
If you have an absolute path and you want all scripts to find the module:
export PERL5LIB="${PERL5LIB:+"$PERL5LIB:"}/path/to/lib" # In your login script.
script
If you have an absolute path and you want one execution of a script to find the module:
PERL5LIB="${PERL5LIB:+"$PERL5LIB:"}/path/to/lib" script
If you have a module that's installed in a directory relative to a script:
use FindBin qw( $RealBin );
use lib "$RealBin/../lib";
In your specific case:
perl -Mlib=/sub1/sub2 -Mdemo -e'demo::doit("arg1")'
Avoid using -I
. It doesn't include related arch dirs like PERL5LIB
and lib.pm do, so modules with arch-specific components will fail to load.
Answered By - ikegami Answer Checked By - Cary Denson (WPSolving Admin)