Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Comment on

( #3333=superdoc: print w/ replies, xml ) Need Help??

The most important difference between require and use when importing them is namespace control.

Prior to Perl 5 you would require libraries to import everything into your script's namespace. Libraries would then potentially clash with both your own variables/subs and those of other libraries. Those libraries would typically have the extension .pl

In Perl 5 this was solved by adding namespaces that spring into existence whenever referenced, and modules imported by use are by definition packages that have their own namespace. Only by explicitly exporting stuff into your main namespace can they cause problems. Modules to be imported with use must have the extension .pm.

Well-written modules let you control what gets exported like this, for example this would ask the Foo plugin to put "bar" and "baz" (and nothing else thank you) into your main namespace:

use Foo qw( bar baz );

Another thing you will often see is modules offering a set of definitions as a group, those group names typically begin with a :colon. This reads as "give me everything needed for using that feature". But nothing else, please.

Ofcourse, badly written modules exist, this is more of a guideline than a strict rule.

Because namespaces matter so much, you're really not supposed to decide yourself how to import stuff written by others. Just as you can use a flathead screwdriver on a philips head, there are always ways to make a module work using require (and with some hacking, vice versa) but it doesn't make you look very professional and you will end up breaking stuff in the long run.

-- FloydATC

Time flies when you don't know what you're doing


In reply to Re: What are the advantages or disadvantages of "require" over "use" by FloydATC
in thread What are the advantages or disadvantages of "require" over "use" by ghosh123

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • Outside of code tags, you may need to use entities for some characters:
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others rifling through the Monastery: (7)
    As of 2014-12-20 03:53 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      Is guessing a good strategy for surviving in the IT business?





      Results (95 votes), past polls