Perl – Doesn’t Perl include current directory in @INC by default


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 from within the directory that contains it: (contains the following statement: use Class1::Class2::Class3)
    Class2/ (contains the following package declaration: package Class1::Class2::Class3;)

However, this is not working in my code because 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…)

Best Solution

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;


use Cwd qw( abs_path );
use File::Basename qw( dirname );
use lib dirname(abs_path($0));
Related Question