Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical

Understanding use and require fully

by rpike (Scribe)
on Aug 01, 2008 at 14:30 UTC ( [id://701705] : perlquestion . print w/replies, xml ) Need Help??

rpike has asked for the wisdom of the Perl Monks concerning the following question:

Hi all, Can comeone explain to me thoroughly how use and require actually work and the differences between them? I'm having some trouble figuring out how they differ and such. I wanted to include a file in-line in a program as though a library item, whereby the main code would flow down through and take that include as though stored there initially. If I put a require statement instead of a loop with other statements in it the values would get lost on each time the required file got hit. Any helpful information would be greatly appreciated. Thanks. Rob

Replies are listed 'Best First'.
Re: Understanding use and require fully
by pc88mxer (Vicar) on Aug 01, 2008 at 14:35 UTC
    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(); ... }

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

Re: Understanding use and require fully
by pjotrik (Friar) on Aug 01, 2008 at 14:36 UTC

      Here are few differences bewteen use and require which may make it more clear for you.

      • use only expects a bareword, require can take a bareword or an expression
      • use is evaluated at compile-time, require at run-time
      • use implicitly calls the import method of the module being loaded, require does not
      • use excepts arguments in addition to the bareword (to be passed to import), require does not
      • use does not behave like a function (i.e can't be called with parens, can't be used in an expression, etc), whereas require does

      Sushil Kumar
Re: Understanding use and require fully
by olus (Curate) on Aug 01, 2008 at 14:39 UTC
Re: Understanding use and require fully
by shmem (Chancellor) on Aug 01, 2008 at 15:25 UTC
    the values would get lost on each time the required file got hit.

    In What's the difference between require and use?

    1) do $file is like eval `cat $file`, except the former 1.1: searches @INC and updates %INC. 1.2: bequeaths an *unrelated* lexical scope on the eval'ed code.

    It is the unrelated bit; in the eval'ed code lexicals from the including code are not visible. Consider:

    # file print $var,"\n";
    # requiring code $var = 'foo'; require '';
    prints foo whilst
    # requiring code my $var = 'foo'; require '';
    does not. Also, a file is only required once, so constructs like
    our $var; for $var ( qw(foo bar quux) ) { require ''; }
    won't work as you would expect, since only foo is printed: require on subsequent passes through the loop will just return 1 and not eval the code again.

    See also use, require, do or what?


    _($_=" "x(1<<5)."?\n".q·/)Oo.  G°\        /
                                  /\_¯/(q    /
    ----------------------------  \__(m.====·.(_("always off the crowd"))."·
    ");sub _{s./.($e="'Itrs `mnsgdq Gdbj O`qkdq")=~y/"-y/#-z/;$e.e && print}
Re: Understanding use and require fully
by rpike (Scribe) on Aug 01, 2008 at 15:36 UTC
    Thanks all for the helpful info. pc88mxer (and all) just wondering if you could comment on my below points/questions : 1) when I create a module that I include in the main script using "use" I end up requiring Exporter in the module that (I guess) provides a means to exporting everything into my main script. If I were to exclude the Exporter what "use" would use Module be in my main script? 2) since BEGIN { require Module..... } mimics a "use" statement is it because BEGIN happens at compile time like the use forcing the time of evaluation for the require? 3) without using require or importing is there is way to still use the modules effectively? Sorry for the elementary questions, just trying to get a thorough understanding of what each does and where each can or cannot be used. Thanks so much again. Rob
      1) Exporter is a way to export names into your namespace. If you use a module that doesn't use Exporter or something similar, you can still use the module, but for example a sub DoPiff() in module Bang cannot be called as DoPiff() (would generate an error) but only as Bang::DoPiff()

      2) not really understanding the question.

      3) If you don't use, require, do or eval a module, that module doesn't exist for your script.