simbabque simbabque - 19 days ago 4
Perl Question

Is it possible to trick Perl to use a variable %INC and still keep the global %INC untouched?

I have just found a script we are using which has a sub that says

my %INC
in it, where it stores some values about incentives, thus
. This never seemed to be a problem, or no-one ever noticed. For me it produced 20 screens of redefine warnings, because
, holding all the file names Perl has
d or
d, was pretty large, and now is
('stuff' => 123)

Do I really have to go and rename every single reference to this in the sub, or is there another way to make Perl forgive this ... ?

Here's a part of the output of:

print Dumper \%INC; # I added this line
my %INC;
print Dumper \%INC; # I added this line
exit; # I added this line


'' => 'lib1/',
'Lib/' => 'Lib/'
$VAR1 = {};
[Fri Jun 29 16:09:13 2012] Subroutine export_fail redefined at /usr/lib/perl5/5.14.2/ line 43.
[Fri Jun 29 16:09:13 2012] Subroutine _cgc redefined at /usr/lib/perl5/5.14.2/ line 45.
[Fri Jun 29 16:09:13 2012] Subroutine longmess redefined at /usr/lib/perl5/5.14.2/ line 51.
[Fri Jun 29 16:09:13 2012] Subroutine shortmess redefined at /usr/lib/perl5/5.14.2/ line 71.
[...] (Snipped like 20 screens of redefine warnings)

The warnings only show if I create an object of one of my classes (which happens to contain
so it has a lot of stuff in it). I'm not sure why those are redefine warnings. If
is empty, how can it know stuff is being redefined?


It seems you actually can create a lexical
my %INC

use strict; use warnings; use Data::Dumper;
print Dumper \%INC;
my %INC = ('asdf' => 'asdf');
print Dumper \%INC;
print Dumper \%INC;

Produces (snipped):

'' => 'C:/Perl/lib/'
$VAR1 = {
'asdf' => 'asdf'
$VAR1 = {
'warnings/' => 'C:/Perl/lib/warnings/',

The problem in my case does not seem to be the
my %INC
, but rather the
%INC = &sub_that_has_my_percent_INC_and_returns_it()
in the script that I actually have to
. Now that, in turn, also has
use vars qw(%INC)
. Replacing it... well, I'm not sure what that is going to break.


The global variable %INC (or %main::INC or %::INC) is a completely different variable from the lexically scoped %INC, created with my. You can safely use my %INC in your subroutine.