in reply to Breaking a script into smaller files

You'll first have to decide if you want the current directory to end up in the beginning or in the end of your @INC search path. Flame already showed you how to get it in the end of @INC search path.

I'd definitely recommend doing this in a BEGIN block. That way you're sure it will be available for all modules used by your script. If not, you may loose track of what module uses the modified search path and what module doesn't.

The following will put the 'current directory' first (which is probably what you want in this case).

BEGIN{unshift(@INC,'.');}


Note. This can also be useful if you want to use a specific version of a system wide module on a box where the administrator doesn't want to upgrade to the latest and gratest of the module you're using. Or if they don't want to install that module at all. It will only make sense for no-binary modules, though.

f--k the world!!!!
/dev/world has reached maximal mount count, check forced.

Replies are listed 'Best First'.
Re: Re: Breaking a script into smaller files
by John M. Dlugosz (Monsignor) on Nov 13, 2001 at 00:33 UTC
    How does using a BEGIN block (as opposed to use lib) help you not "loose track of what module uses the modified search path and what module doesn't"?

    And isn't @INC global, so changing it first in my main script will affect all modules?

      BEGIN blocks fire at compile time, just like use statements. (Camel 3 points out that "use" is just a fancy BEGIN block. See page 465.) They also go in order, top to bottom.

      The point of changing @INC at compile time is to make sure that the new paths are there when modules are loaded. If you don't do that at compile time (with either BEGIN or use lib), it won't affect any of your modules.

      Can I say "compile time" enough?

      Update: There's no effective difference between use lib and the BEGIN block (with unshift), except that lib adds some architecture-dependent directories, if they exist. I was only addressing the "@INC is global" question. (Short answer: it is, but the important thing is *when* it is changed.)

        I'm quite aware that BEGIN blocks fire at compile time. But so does use lib. My question is: why does Biker say that BEGIN is better? And what did he mean by "..loose[sic] track of what module uses the modified search path and what module doesn't"?

        —John