Perl: Include current directory to @INC?

Viewed 377

I'm in the process of moving some websites from an old webserver to a new webserver. The old webserver uses Perl 5.8, the new webserver uses Perl 5.28. One of the differences between the two is that on 5.8, the directory a script is running from is included by default in @INC. Not so with 5.28. This means that cgi scripts that "require" local files are throwing an error.

I know that I can add these lines to a script to solve the problem:

use FindBin qw( $RealBin );
use lib $RealBin;

but the problem with this is, that I need to do this for a a lot of scripts.

Is there a way I can add the current directory (.) to @INC for all scripts in one go? Preferably for the www-data user only?

2 Answers
export PERL_USE_UNSAFE_INC=1

According to perldelta:

PERL_USE_UNSAFE_INC

There is a new environment variable recognised by the perl interpreter. If this variable has the value 1 when the perl interpreter starts up, then "." will be automatically appended to @INC (except under tainting).

This allows you restore the old perl interpreter behaviour on a case-by-case basis. But note that this is intended to be a temporary crutch, and this feature will likely be removed in some future perl version. It is currently set by the cpan utility and Test::Harness to ease installation of CPAN modules which have not been updated to handle the lack of dot. Once again, don't use this unless you are sure that this will not reintroduce any security concerns.

I would heed the advice and try to discourage setting this, though. Maybe in a bind, as you're in, it may be okay as you're already at a security risk; but I would definitely make sure to update the files and unset this variable ASAP.

There's no global solution because the path you want to add is different for each script.

One might be tempted to add ., but . refers to the current directory and not the directory containing the script. It's not correct to do so.


You can fix the files using something like the following:

# See what files will be modified.
find . -name '*.pl'

# Actually modify them.
find . -name '*.pl' -exec perl -pe'$_ .= "use FindBin qw( \$RealBin );\nuse lib \$RealBin;\n" if $. == 1' -i~ {} \;

The above makes a backup. Once you're satisfied with the changes, you can remove the backups using

# See what files will be deleted.
find . -name '*~'

# Actually delete them.
find . -name '*~' -delete
Related