http://qs321.pair.com?node_id=701708


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.)