Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.
 
PerlMonks  

Difference between 'use' and 'require'

by Anonymous Monk
on Dec 07, 2004 at 09:08 UTC ( [id://412860]=perlquestion: print w/replies, xml ) Need Help??

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

What is the difference between use and require


when we are using perl modules we are using use as well as require to include a module


What is the difference between them?


Thanks in advance

Rudhra

Edited by Chady -- changed <h2> tags to <p> tags

Replies are listed 'Best First'.
Re: Difference between 'use' and 'require'
by broquaint (Abbot) on Dec 07, 2004 at 09:57 UTC
    The differences are many and often subtle:
    • 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
    So they behave differently but achieve the same goal. Then there's a list of cultural differences in addition to the technical differences, but they're not so hard and fast.
    HTH

    _________
    broquaint

      use implicitly calls the import method of the module being loaded, require does not

      That's optional. Adding empty parens (as in use Socket ();) prevents import from being called.

Re: Difference between 'use' and 'require'
by Happy-the-monk (Canon) on Dec 07, 2004 at 09:33 UTC

    See also the manuals for details:
    perldoc -f require
    perldoc -f use   and
    perldoc perlmod   on modules.

    Cheers, Sören

Re: Difference between 'use' and 'require'
by gothic_mallard (Pilgrim) on Dec 07, 2004 at 09:15 UTC

    Any reason why you needed to post that in uber-print?

    Anyhow, as far as I know you don't need to do a require AND a use as:

    use MyModule;

    .. is equivalent to:

    require MyModule; import MyModule;

    --- Jay

    All code is untested unless otherwise stated.
    All opinions expressed are my own and are intended as guidance, not gospel; please treat what I say as such and as Abigail said Think for yourself.
    If in doubt ask.

      Except of course that use is evaluated at compile time where as require is evaluated at run time.

      And I second the question about the stupidly large font.

        Ah yes, forgot that one :)

        A use anywhere in the code will be evaluated when the code is run compiled, but require - import's can only get evaluated when encoutered - good for when you want one module or another but both have quite a large initialisation overhead which means you only want the one you need.

        update D'oh, sorry, meant compile not run.

        --- Jay

        All code is untested unless otherwise stated.
        All opinions expressed are my own and are intended as guidance, not gospel; please treat what I say as such and as Abigail said Think for yourself.
        If in doubt ask.

        @anonymous monk

        "the stupidly large font"

        if you really wanted the OP to post in a different format, you wd have made a suggestion using a civilized vocabulary or, as the first commenter did, ask a question in a civilized manner. but i suspect that you do not really care about the font the OP used. but wanted simply to vent some spleen. you are doing life in a very self-defeating manner. you are also befouling this site.
      Not quite, perldoc -f use says use is equivalent to BEGIN { require Module; import Module LIST; }, meaning it is executed at compile time before everything else. This can bite if your modules depend on some initialisation written in normal code (outside a BEGIN-Block).

      Please read the elaboration on working behind the scenes for "use" and "require" tags. http://www.perl.com/pub/2002/05/14/mod_perl.html

Re: Difference between 'use' and 'require'
by Crian (Curate) on Dec 07, 2004 at 09:51 UTC

    The use statement works at compile time, require at run time. So if you have a module that does a lot in a begin block and you don't really need it in all cases, then it's clever to "require" it there where you want to use it but don't "use" it. So you don't have to wait for all that initializations of that module in case you don't need it.

    update: As I have seen now, this point was already said. But it wasn't as I startet to write my post (I was interrupted while typing).

Re: Difference between 'use' and 'require'
by rrwo (Friar) on Dec 07, 2004 at 20:29 UTC

    Don't forget the arcane 'unimport' routine that is called when you use 'no' instead of 'use'.

    It's good for making subroutines and constants have no-op or undefined values, so that code which calls the module doesn't have to worry about breaking in strict mode.

    This only works for modules that have an unimport subroutine defined, though.

    Carp::Asset is a good example:

    no Carp::Assert; # change to use to enable assert( 0 ), if DEBUG;
      Use excepts module name only. but require excepts whole filename.

        Use excepts module name only. but require excepts whole filename.

        Sure it doesn't "require excepts" such a thing

        Do you mean "accepts" as in "takes" or "excepts" as in "creates an exception for"?

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others chilling in the Monastery: (4)
As of 2024-03-29 14:44 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found