I have never fully understood Perl's resolution of package names, but I always assumed that the following should always work, assuming you are executing myscript.pl from within the directory that contains it:
myscript.pl (contains the following statement: use Class1::Class2::Class3) Class1/ Class2/ Class3.pm (contains the following package declaration: package Class1::Class2::Class3;)
However, this is not working in my code because Class3.pm cannot be located. Looking at @INC, it does not include the current directory, only various directories of my Strawberry Perl installation.
What is the recommended way to solve this? I suppose I could modify @INC, or I could start using FindBin, but I'm not sure which is best. I have inherited this code and am simply migrating it to a new location, but it doesn't look like the old code needed either such solution (I could be wrong, still looking...)
my $cwd = getcwd(); Returns the current working directory.
Perl interpreter is compiled with a specific @INC default value. To find out this value, run env -i perl -V command ( env -i ignores the PERL5LIB environmental variable - see #2) and in the output you will see something like this: $ env -i perl -V ... @INC: /usr/lib/perl5/site_perl/5.18.
It is typically used to add extra directories to Perl's search path so that later do, require, and use statements will find library files that aren't located in Perl's default search path.
Perl doesn't search the current directory for modules or the script's directory for modules, at least not anymore. The current directory was removed from @INC
in 5.26 for security reasons.
However, any code that relies on the current directory being in @INC
was buggy far before 5.26. Code that did so, like yours, incorrectly used the current directory as a proxy for the script's directory. That assumption is often incorrect.
To tell Perl to look in the script's directory for modules, use the following:
use FindBin 1.51 qw( $RealBin ); use lib $RealBin;
or
use Cwd qw( abs_path ); use File::Basename qw( dirname ); use lib dirname(abs_path($0));
Having .
(the current directory) in @INC
was removed in 5.26 for security reasons (CVE-2016-1238). Some Linux distributions have backported the change, so you might run into this problem even if you're using e.g. 5.24.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With