in reply to Changing Module Tree

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

As demonstrated by GrandFather recently in Re: Adding code to a Perl program without stopping it.

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

    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.

      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.