Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Re: Understanding use and require fully

by pc88mxer (Vicar)
on Aug 01, 2008 at 14:35 UTC ( #701708=note: print w/ replies, xml ) Need Help??


in reply to Understanding use and require fully

use Module LIST is equivalent to:

BEGIN { require Module; import Module LIST; }
So one _big_ difference is that use is executed at _compile_ time, whereas require is only executed at "run" time.

Generally I would always use use even if you don't export any symbols.

One place where require would be preferred over use is when you want to include a module only when it really is needed. So you'll see code like:

if ($type eq "Excel file") { require SpreadsheetReader::Excel; my $parser = SpreadsheetReader::Excel->new(); ... }


Comment on Re: Understanding use and require fully
Select or Download Code
Re^2: Understanding use and require fully
by chromatic (Archbishop) on Aug 01, 2008 at 17:44 UTC

    use Module LIST is equivalent to: BEGIN { require Module; import Module LIST; }

    Nit: it's actually equivalent to:

    BEGIN { require Module; Module->import( LIST ); }

    use always performs a method call, where the indirect invocation form you had sometimes does not perform a method call. (If you want to be very specific, it's probably closer to Module::->import( LIST ), but you have to read the code to Perl_utilize() in C to make sure of that.)

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://701708]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (4)
As of 2015-07-05 08:30 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (61 votes), past polls