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

Alright, so that solves the problem of a single module, but what if Foo uses several other modules?

Also, this would work in the limited example, but I also have a third module path that will need to be restored afterwards:

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

It seems cumbersome to have to stick an additional delete/require after the loop.

Replies are listed 'Best First'.
Re^3: Changing Module Tree
by wind (Priest) on Apr 05, 2011 at 21:25 UTC

    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{$_}; }

      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); }