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: " . ($@||$!); +}