Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Re: Require in modules...?

by Joost (Canon)
on May 10, 2009 at 02:06 UTC ( [id://763098]=note: print w/replies, xml ) Need Help??


in reply to Require in modules...?

require only loads a file if it hasn't been loaded yet, given reasonable checks, so it (normally) won't include stuff twice. and even if it *did* load the module twice, that probably doesn't mean you get multiple copies - that's all dependent on the scoping rules - "global" entities like named functions are unique and you cannot have more than one of them at the same time. Also, I consider it good form for a module to use/require all the code it depends on instead of relying on the user to do that.

Anyway, that's to say that you can usually use require wherever you want.

Now, it's probably better to use use instead, since use directives are processed earlier, and all you have to do to convert "data_defs.pl" to a useable module is to to rename it to "data_defs.pm" (that name is contrary to conventions but it'll work all the same).

None of this has anything to do with OO - it's just a code loading mechanism - and also, you probably should not use Switch, since it can really mess up your code.

Replies are listed 'Best First'.
Re^2: Require in modules...?
by ikegami (Patriarch) on May 10, 2009 at 03:06 UTC

    Now, it's probably better to use use instead, since use directives are processed earlier, and all you have to do to convert "data_defs.pl" to a useable module is to to rename it to "data_defs.pm" (that name is contrary to conventions but it'll work all the same).

    I disagree. Modules can be required or used multiple times, but data_defs.pl cannot. To convert data_defs.pl to a module, you'll also need to add a package declaration, which means you'll also need to import the variables from the module use access them via their qualified names.

    If you want the loading to occur earlier, use

    BEGIN { do 'data_defs.pl' or die "Can't load data defs: " . ($@||$!); +}

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others avoiding work at the Monastery: (4)
As of 2024-04-25 05:08 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found