Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

comment on

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

You seem to be making a distinction between subroutines in .pl files and modules. There is no such distinction. Modules are typically just a collection of subroutines/functions/methods/widgets - call them what you will. See Simple Module Tutorial. Anyway besides suggesting that you put everyting into modules for consistency and simplicity's sake the main difference between use and require is that use does a require PLUS calls the modules import function. Never seen a module with an import() function? That is becaue it is inherited from Exporter Depending on what the module exports by default (in @EXPORT) and what you ask for (in @EXPORT_OK) zero or more functions/subroutines will become available via their unqualified name ie you can call somefunction(). You can *always* call Some::Module::somefunction() by its fully qualified name, regardless of whether or not you have imported it. The only pre-requisite is that you have used or required that module first.

The other significant difference between use and require is that use happens at compile time. require happens at runtime as Perl stumbles across it. require is almost the same as do (it uses it), except it will search @INC for you. For example you can do (not recommended) this to effectively get very similar results to useing a module:

do 'd:/perl/lib/Data/Dumper.pm'; Data::Dumper->import(); print Dumper(\@INC,\%INC);

So what do I personally do? Almost all code is in modules. In a given script/module I will use any other Module I know I will need, and import the functions that always get used. In any given function, if it is possible that a module has not been used and/or the required function imported I will have:

sub widget { # blah require Some::Module; my $result = Some::Module::somefunction(@args);

cheers

tachyon


In reply to Re: use and require inside subs by tachyon
in thread use and require inside subs by Anonymous Monk

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!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • 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
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            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 imbibing at the Monastery: (3)
    As of 2020-08-13 13:53 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?
      Which rocket would you take to Mars?










      Results (73 votes). Check out past polls.

      Notices?