in reply to Development version of a script?

Perl gives you at least three different ways to dynamically choose what file Perl loads:

my $module=My::File; my $filename="My/File.pm"; do $filename; eval `cat $filename`; eval "require $module";

The difference between them is that

For more information see require, do, and eval.

One additional thought: If you have 40 variables that all co-vary together depending on context (development vs. production), perhaps you might consider bundling those variables in a configuration object/class. That would make any future work with these variables (in particular additional setting sets for testing purposes) much much easier to manage. 40 of anything is not a small number and is hard for the human eye to check and recheck. It is important that you use code structure/data organization as much as possible to ensure that they are set up as a group. If you decide eventually to go the object route, you might also want to consider using YAML to store the settings. The data files are very easy to write by hand (and read) and they can be loaded and dumped with ease.

Update: revised pros/cons of eval - not using @INC can sometimes be desired.

Update: added YAML suggestion

update: changed quotes to backticks on eval

Replies are listed 'Best First'.
Re^2: Development version of a script?
by kurt2439 (Sexton) on Feb 07, 2011 at 23:28 UTC

    I must be missing something simple, or else poorly explained my problem.

    When I use any of the methods you guys have explained, I get many compilations error about explicit package names -- so perl is not seeing the file I am trying to include with those variables declared, or else they are out of scope? This was from the require perldoc -- I know it is mentioning the reverse case of what I am doing, but perhaps still relevant

    The file is included via the do-FILE mechanism, which is essentially just a variety of eval with the caveat that lexical variables in the invoking script will be invisible to the included code.

    Thanks for the tip about including my variables in an object -- it's a little over my head at my current perl level but I will put it on the list of things to check out

    Here is the most basic reference to my code so you can see two of my trials of your suggestions

    #require ::usr::local::bin::ebook_distributor_DEV::ebooksend_config; eval "cat /usr/local/bin/ebook_distributor_DEV/ebooksend_config";

      No you aren't missing anything - my bad. The eval cat option should be using backticks, not double quotes, e.g. eval `cat myfile.pl`

      My apologies.

      If that doesn't help then perhaps you could show a reduced version of your whole set up using just 1 or two variables. Even with the backticks, to get this to work you must take care with how you declare your variables. You will need to declare your variables in the calling script. If you declare them as "my", your config file should merely set variables and not declare them (no my). Alternatively you can declare your variables in both the calling and config script if you use our.

        I was being too dependent and not thinking about the code you were giving me to run -- should have caught your meaning and put in the backticks...

        Now I am understanding you -- I was trying to completely remove the variables from the main script, but I see what you mean about needing them declared in the main script and then just setting them in the "config" file. I haven't tested but I get it now -- thanks!