Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

Re: What are the advantages or disadvantages of "require" over "use"

by tobyink (Canon)
on Apr 18, 2014 at 07:32 UTC ( [id://1082723]=note: print w/replies, xml ) Need Help??


in reply to What are the advantages or disadvantages of "require" over "use"

If a module has a compile-time effect (exporters and pragmata), then use use. Otherwise (e.g. for OO modules), you can choose.

A good reason to go with require would be when loading a module that isn't always needed. Say you are writing a module that offers load_from_file and load_from_web functions. In this case you might want to require LWP::UserAgent (or HTTP::Tiny or whatever) inside the definition of load_from_web. That way, if the person using your module never needs to load anything from the web (they only ever load from files), they can avoid loading LWP::UserAgent at all.

use Moops; class Cow :rw { has name => (default => 'Ermintrude') }; say Cow->new->name

Log In?
Username:
Password:

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

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

    No recent polls found