Re: How can I prevent a module from being added to the %INC hash?
by ikegami (Patriarch) on Oct 04, 2006 at 02:35 UTC
|
Perhaps you should be using do instead of require or use? It does add the module to %INC, but since %INC is not checked, you can execute the "module" multiple times.
do should be used for "modules" with no package statement. require or use should be used with modules with a package statement.
# config.pl
# ---------
$var = 'test';
# main.pl
# -------
use strict;
use warnings;
our $var;
do 'config.pl';
print($var);
| [reply] [Watch: Dir/Any] [d/l] [select] |
|
Is it possible to use a module another way? For example, putting a reference to a subroutine that uses a module into @INC? I think the docs indicate that only the reference is added to %INC. But I have never tried adding a code reference to @INC.
| [reply] [Watch: Dir/Any] |
|
| [reply] [Watch: Dir/Any] [d/l] |
|
|
Re: How can I prevent a module from being added to the %INC hash?
by syphilis (Archbishop) on Oct 04, 2006 at 02:13 UTC
|
I think you're on the right track with the BEGIN block. Here's an example that (I hope) removes Net::FTP and any modules loaded by Net::FTP from %INC:
BEGIN {
use strict;
use warnings;
use Carp;
use Digest::MD5;
our %inc_copy = %INC;
};
use Net::FTP;
%INC = %inc_copy;
for(keys(%INC)) {print "$_: $INC{$_}\n"}
Note that I explicitly loaded Carp.pm, and that Net::FTP also loads Carp.pm. Do you therefore require that Carp.pm not appear in %INC ? With the above example, Carp.pm still appears in %INC.
Cheers, Rob | [reply] [Watch: Dir/Any] [d/l] |
|
One problem: What if you are putting the code above into a module and then use that module in a program? The %INC hash will contain all the modules that the program uses as well. And those modules will not be in the %INC hash when you assign it to %inc_copy.
| [reply] [Watch: Dir/Any] |
|
What if you are putting the code above into a module and then use that module in a program?
I might be missing something here, but it still seems to work ok:
## My_Foo.pm
package My_Foo;
BEGIN {
use strict;
use warnings;
use Carp;
use Digest::MD5;
our %inc_copy = %INC;
};
use Net::FTP;
%INC = %inc_copy;
1;
## test.pl
use Math::BigInt;
use My_Foo;
for(keys(%INC)) {print "$_: $INC{$_}\n"}
Net::FTP and the modules it loads are still missing from %INC.
Cheers, Rob | [reply] [Watch: Dir/Any] [d/l] [select] |
|
|
Re: How can I prevent a module from being added to the %INC hash?
by Tanktalus (Canon) on Oct 04, 2006 at 03:31 UTC
|
Perhaps I'm being somewhat dense here, but ... why?
/me smells an XY Problem although that could actually be dinner.
| [reply] [Watch: Dir/Any] |
Re: How can I prevent a module from being added to the %INC hash?
by sgifford (Prior) on Oct 04, 2006 at 02:27 UTC
|
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?...
| [reply] [Watch: Dir/Any] [d/l] [select] |
|
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
}
| [reply] [Watch: Dir/Any] [d/l] [select] |
|
Ah, I forgot about local. It worked. I have to do further testing in the morning. But it looks good so far. thanks
| [reply] [Watch: Dir/Any] |
|
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.
| [reply] [Watch: Dir/Any] [d/l] |
Re: How can I prevent a module from being added to the %INC hash?
by diotalevi (Canon) on Oct 04, 2006 at 15:59 UTC
|
This is a strange problem and it doesn't sound like you're solving a sane problem. It's easy enough to modify a hash - just remove the stuff that's added.
my %pre_existing_INC;
BEGIN { @pre_existing_INC{ keys %INC } = () }
use Your::Module;
BEGIN { delete @INC{ grep { not exists $pre_existing_INC{$_} } keys %I
+NC } }
| [reply] [Watch: Dir/Any] [d/l] |