Modification to Perl's @INC array seems for an individual scope very confusing. I would like some clarification, as it seems to be fighting any means of dynamic initialization of objects.
One would think that I could define it as local to solve this problem.
According to the manual, "local modifies the listed variables to be local to the enclosing block, file, or eval."
The part that is annoying me is the "or" portion.
Problem: I would like to change the @INC array to include one and ONLY one directory under certain circumstances and ONLY for the current file.
Example attempt and issues:
Lets say I have a launching script index.pl:
#!/usr/bin/perl
use strict;
use warnings FATAL => 'all';
use File::Basename;
# Lets say I want to modify @INC here to look in ONLY one path. Local
# should allow us to declare for one scope or file (how non explicit this
# is annoys me) Since I have not defined a scope with brackets, it should
# be effective for the current file
local @INC = (dirname(__FILE__) . '/foo/'); #some relative path
# Lets say bar now uses standard perl modules
require 'bar.pm';
# ^- This will fail because local did not work as described, fails at use
# XML::Simple because it is traversing foo
my $bar = bar->new();
For the sake of being comprehensive, here is a bar.pm:
package bar;
use strict;
use warnings;
sub new
{
my $class = shift;
my $self = bless {}, $class;
use XML::Simple;
return $self;
}
1;
Is there anyway to modify @INC ONLY for the current file while leaving it intact in all parsed files afterwards?
(I know I can unshift, but eventually there could be dozens of directories it could be traversing)
require(dirname(__FILE__) . '/foo/bar.pm');