rashley has asked for the wisdom of the Perl Monks concerning the following question:

I'm setting up some scripts to be run by crontab.

Portability is an issue, so I'm doing my best to create a config file that will contain anything that may change in order to move the code.

I've managed this, with one exception. My scripts all use a common package. In order to get this into @INC I've been doing something like this:

use strict; use lib '/my/path/'; use PackageName; our ( $var1, $var2); require Config;
And it works fine. Now I'm trying to get this path into my config file like such:
our ( $var1, var2, $myPath); require SCP_Config; use lib $myPath; use PackageName;
And it doesn't work. I know that this is because 'use lib' is a compile-time command, which is causing problems.

Still, there must be a way to do this. Any ideas?

Thanks.

Replies are listed 'Best First'.
Re: Dynamic Package Loading
by ikegami (Patriarch) on Dec 16, 2005 at 20:23 UTC

    Why not use use SCP_Config instead of require SCP_Config? That will execute it at compile-time instead of at run-time.

    Actually, if SCP_Config doesn't contain package SCP_Config;, I'd use BEGIN { do 'SCP_Config.pm' }. do will allow multiple files to use SCP_Config, and BEGIN will cause it to execute at compile-time.

Re: Dynamic Package Loading
by BrowserUk (Patriarch) on Dec 16, 2005 at 20:25 UTC

    Something like

    BEGIN{ our $mypath; require SCP_Config; require lib $mypath; }

    Should do it. (untested)


    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    Lingua non convalesco, consenesco et abolesco. -- Rule 1 has a caveat! -- Who broke the cabal?
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.
      This produced a syntax error near require lib $mypath;

        Grrr. Of course it does. It needs to be like this

        #! perl -slw use strict; our $path; BEGIN { require SP_Config; } use lib $path; print for @INC;

        Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
        Lingua non convalesco, consenesco et abolesco. -- Rule 1 has a caveat! -- Who broke the cabal?
        "Science is about questioning the status quo. Questioning authority".
        In the absence of evidence, opinion is indistinguishable from prejudice.