Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

"use" vs "require"

by mrguy123 (Hermit)
on Aug 07, 2006 at 13:04 UTC ( [id://565934]=perlquestion: print w/replies, xml ) Need Help??

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

Hi monks
I have recently started working on code written by my predacessor. Some of the files he uses he adds with use (e.g use Simple), and others with require (require "Simple.pl"). Which way is more recommended, and why?
Thanks,
Guy

Replies are listed 'Best First'.
Re: "use" vs "require"
by Corion (Patriarch) on Aug 07, 2006 at 13:05 UTC
Re: "use" vs "require"
by Velaki (Chaplain) on Aug 07, 2006 at 13:11 UTC

    According to the manual, use is the equivalent of BEGIN { require Module; import Module LIST; } except that Module must be a bareword.

    It really comes down to if you wish to import names into your current namespace, so that you would be able to type bar(), and not Foo::bar(). Your project will dictate which is better at the time.

    Hope this helped,
    -v.

    Update: Kudos to friedo for reminding me in his post that there is also the matter of whether you want your code loaded at compile-time vs. runtime.

    "Perl. There is no substitute."
      It really comes down to if you wish to import names into your current namespace

      Well, no. It also comes down to whether you want code loaded at compile-time or runtime. And you can always manually call import if you require a module.

        Thanks! Yeah, forgot about that one.

        -v.
        "Perl. There is no substitute."
      I find that fully qualified calls to imported subs makes it easier to decipher the code at some later date. Otherwise the reader has to look up the sub in their head, or perldoc, when it could be right there where they need it most.

        An alternative is to explicitely state all the imported functions in the use statement. For example, instead of saying

        use Module;
        use
        use Module qw( func1 func2 );
Re: "use" vs "require"
by bobf (Monsignor) on Aug 07, 2006 at 15:22 UTC
Re: "use" vs "require"
by gellyfish (Monsignor) on Aug 07, 2006 at 13:06 UTC

    See the perlfunc documentation for a discussion of the differences.

    /J\

Re: "use" vs "require"
by izut (Chaplain) on Aug 07, 2006 at 13:10 UTC

    Please check use and require.

    Igor 'izut' Sutton
    your code, your rules.

Re: "use" vs "require" vs "do"
by ikegami (Patriarch) on Aug 07, 2006 at 16:56 UTC
    and others with require (require "Simple.pl").

    Files with no package should be included using do, not require or use.

      Erm... why?

      It seems to me that the main difference between require and do is that do FILE will always load and execute the code, while require will only load it once:

      From require:

      ... "require" demands that a library file be included if it hasn’t already been included. The file is included via the do-FILE mechanism, which is essentially just a variety of "eval". ...

        Exactly. require will mysteriously fail if two modules need to use the file. It's a good habit to use do instead. You never know what someone will do in the future.

        There's another reason. Using do over require conveys more information to the reader. Since modules must not be loaded using do, it's safe to assume something loaded using do is not a module. Using require wouldn't be as clear.

Re: "use" vs "require"
by kulls (Hermit) on Aug 08, 2006 at 03:09 UTC
    As simple
    Try this in the command prompt.
    $shell> perl use Module; # Not installed in your machine $shell> perl require Module; # Not installed in your machine

    you must feel the difference I guess.
    Thanks,
    kulls

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://565934]
Approved by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others musing on the Monastery: (9)
As of 2024-04-19 16:33 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found