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

Hi Guys,

I have a development version of a script which is 99% the same as the production script except for about 40 variables which are mostly paths and usernames and such. I was hoping to just have a line in the beginning of the code like:

require "/path/to/file/";

which would contain all my variables, and then I could extract them from the program and never have to worry about changing them again.

After doing some googling, it seems like this is better achieved when the file you are including contains subroutines instead of variables, but I just want my code to be cleaner and have less variables to change. What is the perl way of doing this?

Many thanks

Replies are listed 'Best First'.
Re: Development version of a script?
by ELISHEVA (Prior) on Feb 07, 2011 at 23:02 UTC

    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

    • eval qq{require $module} can only be called once per program because it is designed for loading code that might barf if you try to load it twice in a row, and of course, to avoid unnecessary recompiling of module. You have to run this code as eval string because require needs a bare word, not a quoted string, for a package name.
    • do can be called multiple times, if that is what you need. Like require it uses @INC.
    • eval, like do can be run multiple times. The biggest difference from do is that it can see variable values set in the calling environment. There are certain situations where that might be an advantage - for instance in a template file. However, in most cases it is likely to cause unwanted side effects. Another difference is that eval cat ... doesn't use @INC to find the file. If the file is coming from your home directory that is good. If it is meant to be stored in directory findable via @INC then you are doing things the hard way. Another disadvantage of C<eval> is that it can't spit out file names and line numbers if there is a problem reading the file

    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

      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.

Re: Development version of a script?
by CountZero (Bishop) on Feb 07, 2011 at 22:53 UTC
    Yes you can do that. require Path::to::file; is the canonical way to do it.

    Perl will read in this file and its contents become part of your script. See require.

    CountZero

    A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

Re: Development version of a script?
by Anonymous Monk on Feb 08, 2011 at 00:40 UTC