Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

comment on

( [id://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":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others surveying the Monastery: (3)
As of 2024-03-28 17:17 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found