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

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 }

Replies are listed 'Best First'.
Re^3: How can I prevent a module from being added to the %INC hash?
by jacques (Priest) on Oct 04, 2006 at 07:13 UTC
    Ah, I forgot about local. It worked. I have to do further testing in the morning. But it looks good so far. thanks
Re^3: How can I prevent a module from being added to the %INC hash?
by sgifford (Prior) on Oct 04, 2006 at 19:43 UTC
    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.