Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Re: Understanding use and require fully

by pc88mxer (Vicar)
on Aug 01, 2008 at 14:35 UTC ( [id://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(); ... }

Replies are listed 'Best First'.
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
Domain Nodelet?
Node Status?
node history
Node Type: note [id://701708]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others chilling in the Monastery: (3)
As of 2024-04-19 23:37 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found