Discussions
Categories
Groups
Community Home
Categories
INTERNAL ENABLEMENT
POPULAR
PUBLIC CLOUD
PRIVATE CLOUD
Quick Links
MY LINKS
HELPFUL TIPS
Back to website
Home
Web CMS (TeamSite)
use, require and/or blocking for performance?
System
Does it make a difference in terms of performance in Perl whether I use "use" or "require"? It seems like when I use "use" it parses the module regardless of whether I actually use the module or not; I am not sure if it would be the same with require (or what the difference is here really).
Also if I put a use in a conditional, does it not parse the module unless the condition is true? For instance
if ( something )
{
use module;
}
Does module get parsed even if something is not true?
Find more posts tagged with
Comments
james1
Please see:
http://perldoc.com/perl5.005_03/pod/func/require.html
http://perldoc.com/perl5.005_03/pod/func/use.html
http://perldoc.com/perl5.005_03/pod/perlmod.html
I think the answers to your questions are:
> It seems like when I use "use" it parses the module
> regardless of whether I actually use the module or not; I am
> not sure if it would be the same with require (or what the
> difference is here really).
Both cause the file to be parsed. use additionally imports the module's symbols into the current namespace and is done at compilation time.
> Also if I put a use in a conditional, does it not parse the
> module unless the condition is true?
No, a use statement is always evaluated at compile time. (I think.)
> Does module get parsed even if something is not true?
I think you want to use require, which can be evaluated conditionally. (I think.)
-- James
--
James H Koh
Interwoven Engineering
Adam Stoller
You can use a conditional to surround an eval() statement consisting of the 'use' (or 'require') clause to delay / prevent that module from being loaded into memory. e.g.:
{
no warnings;
eval { "use Win32API::Net;" } if ($^O eq "MSWin32");
}
The
no warnings;
statement scoped into this select block of code prevents the generation of a warning like: "Useless use of a constant in void context at ..."
--fish
Senior Consultant, Quotient Inc.
http://www.quotient-inc.com
BSS-2460.JPG