in reply to Re^2: Changing Module Tree
in thread Changing Module Tree

Save the state of the %INC before the loop, and restore it at the beginning of each iteration.

Something as simple as this might work, assuming %INC isn't a tied hash:

%INC = %old_INC;
Or alternatively:
for (keys %INC) { delete $INC{$_} if ! $old_INC{$_}; }

Replies are listed 'Best First'.
Re^4: Changing Module Tree
by lackita (Sexton) on Apr 06, 2011 at 14:21 UTC

    Alright, I tried to modify the test script, but I've discovered that with this code the "b" tree is never loaded.

    #!/usr/bin/perl use Foo; for my $tree ("a","b") { local $ENV{PERL5LIB} = $tree; delete $INC{'Foo.pm'}; require Foo; Foo::Bar(); } Foo::Bar();

    A side note, PERL5LIB is set to "a" when the script is executed.

      PERLLIB/PERL5LIB does not affect a running perl process

      $ perl -le "print for @INC
      C:/perl/site/5.12.2/lib/MSWin32-x86-multi-thread
      C:/perl/site/5.12.2/lib
      C:/perl/5.12.2/lib/MSWin32-x86-multi-thread
      C:/perl/5.12.2/lib
      .
       
      $ set PERL5LIB=blah
       
      $ perl -le "print for @INC
      blah
      C:/perl/site/5.12.2/lib/MSWin32-x86-multi-thread
      C:/perl/site/5.12.2/lib
      C:/perl/5.12.2/lib/MSWin32-x86-multi-thread
      C:/perl/5.12.2/lib
      .

      You want to manipulate @INC directly

      use lib
      #!/usr/bin/perl require lib; for my $tree ("a","b") { lib->import($tree); delete $INC{'Foo.pm'}; require Foo; Foo::Bar(); lib->unimport($tree); }