in reply to How can I prevent a module from being added to the %INC hash?

Just an interesting side note: I expected local %INC in a BEGIN block or an eval to automatically back out changes to %INC when the enclosing block finished, but that didn't work. I wonder why?...

Replies are listed 'Best First'.
Re^2: How can I prevent a module from being added to the %INC hash?
by ammon (Sexton) on Oct 04, 2006 at 02:43 UTC
    It doesn't work due to how BEGIN blocks and use statements are compiled/executed. If you take the use out of the equation, you can see that there's nothing special about %INC:

    For example, compare the difference between these two commands:

    perl -e'BEGIN{ eval "use Net::FTP;" } print %INC'

    and

    perl -e'BEGIN{ local %INC = %INC; eval "use Net::FTP;" } print %INC'

    The only difference between the two is the localization of %INC in the BEGIN block.

    Of course, doing an eval on the string is slow. The OP might want to use a require inside a BEGIN block, instead:

    BEGIN { # Hide the fact that our module was imported. local %INC = %INC; require Net::FTP; # call import() if necessary }
      Ah, I forgot about local. It worked. I have to do further testing in the morning. But it looks good so far. thanks
      Yup, that does seem to work. I swear I tried exactly that yesterday and it didn't work. I suspect the cost of string eval will be very small compared to the cost of loading and compiling the module.