Re: Using a module more than once
by blazar (Canon) on Aug 02, 2005 at 15:28 UTC
|
Is this bad practice? Will it hurt anything? (I assume "no" to both questions, but I could be wrong.)
I don't think so, unless these modules do something really strange. In which case you would probably already know from their documentation. See e.g.:
#! /usr/bin/perl -l
use strict;
use warnings;
use lib sub { print pop; undef };
use File::Find;
use File::Basename;
use File::Find;
__END__
which gives me
File/Find.pm
warnings/register.pm
Cwd.pm
vars.pm
XSLoader.pm
File/Basename.pm
re.pm
File/Spec.pm
File/Spec/Unix.pm
Scalar/Util.pm
List/Util.pm
DynaLoader.pm
AutoLoader.pm
| [reply] [d/l] [select] |
|
I've never seen a sub ref being passed to use lib before. How does that work?
I also don't see anything in the docs about it. Is it an unintended consequence (accidental feature? :-) or am I just out of date?
| [reply] [d/l] |
|
perldoc -f require will explain it better than I ever could. It was a big discovery for me too. Of course you can exploit this feature to do more {bizarre,uselful} things.
| [reply] |
|
Thanks! That's the kind of factual confirmation I was hoping to get.
| [reply] |
Re: Using a module more than once
by holli (Abbot) on Aug 02, 2005 at 15:19 UTC
|
so I put a "use" statement for each CPAN module inside my methods as needed.
Why don't you just put them on the top of your modules code?
| [reply] [d/l] |
|
I guess I could do that - thanks for the suggestion! I just wanted to load them situationally, as not all of them are needed for every method. Or am I misunderstanding how that mechanism works?
| [reply] |
|
Or am I misunderstanding how that mechanism works?
You are. Any module that gets used, will be loaded at compile time, thus, when your module is loaded. So there's no use in putting a use statement inside a method.
Sometimes, when some modules are only needed in exceptional circumstances, people can use a require statement in a method or sub, for example to load Carp in an error trapping routine.
You have to be aware of the differences between use and require: require doesn't call import in the loaded modules, and it won't change the allowable syntax for your code either. Thus, subs defined in that module won't be easily recognized as such in the loading script. It's something to look out for.
For OO modules, it makes no difference, whether you choose use or require.
| [reply] |
|
|
|
|
yes -- use causes everything to be loaded at the beginning.. if you want it conditionaly loaded, one way is to do require Foo::Bar instead. (Also see my other reply below)
Update: See Also:
| [reply] [d/l] [select] |
|
|
You're misunderstanding. use happens at compile time. Consider:
#file: /perl/site/lib/Foo.pm
package Foo;
use strict;
use warnings;
sub bar
{
return "BAR";
}
1;
#file: test.pl
use strict;
use warnings;
my $i = 0;
if ( $i )
{
use Foo;
}
print Foo::bar; #prints "BAR"
| [reply] [d/l] [select] |
Re: Using a module more than once
by jeteve (Pilgrim) on Aug 02, 2005 at 15:24 UTC
|
Hi.
I don't think putting several
use Foo ;
on your code will load many times the module in the memory. IMHO, once the module is loaded in memory by the first use, the following use has no effect.
So if you need to explicitely use the module Foo in your module, just write use Foo; at the head.
If other developpers use the same modules as you in their code, that will cause no problem at all.
Hope it helps !
Nice photos of naked perl sources here !
| [reply] [d/l] |
|
Correct. Perl maintains a global %INC hash, listing all modules that have been required (use is just a special case of require). If a reference to the file already exists in %INC, it will not be loaded a second time.
If you wanted to force Perl to reload the module, you could delete the reference to it in %INC (the key is the file name, which can be absolute, or relative to one of the directories in @INC). There aren't many cases where you'd want to do this, of course. (One legitimate case is Apache::Reload).
| [reply] [d/l] [select] |
|
IMHO, once the module is loaded in memory by the first use, the following use has no effect
Not entirely true. Subseqent use's don't load the file again, but they still call the import method of the use'd class.
Dave.
| [reply] [d/l] |
Re: Using a module more than once
by davidrw (Prior) on Aug 02, 2005 at 15:31 UTC
|
Also, will any problems (or benefits) arise from developers adding "use" statements for the same CPAN modules in their own programs?
I've tried a SuperSearch with no luck, just to head that off.
No, it's fine -- they only get included once. perldoc -f use says that use Module LIST is exactly equivalent to BEGIN { require Module; import Module LIST; } and perldoc -f require says that 'the file will not be included twice
under the same specified name'.
| [reply] [d/l] [select] |
Re: Using a module more than once
by cowboy (Friar) on Aug 02, 2005 at 21:04 UTC
|
I think everyone else has covered the use vs require.
One benefit to them adding use statements is in an environment such as mod_perl or another environment where alot of processes are forked from a parent.
In this case, if you load the modules in the parent process process, the memory can be shared among the children using the OS's copy-on-write abilities.
| [reply] |