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

Hi Guys,

I have a very simple, quick question :)

I have a script which stores all its config $variables in an external conf.cfg file, which I call via require at the beginning of my script - which is great - but, I use the -w flag and I keep getting Use of Uninitialized value $config for those I call from my external file.

Any ideas how I could approach this better, and get rid of these errors? Thank you

Jon
  • Comment on Unitialized value warnings with require

Replies are listed 'Best First'.
Re: Using Variables
by runrig (Abbot) on Dec 27, 2001 at 23:13 UTC
    Maybe you need to 'use vars' to declare your variables in the main script instead of using my (are you using strict?). 'my' variables in the main script are in a different scope than those used in the required config file, so they are not really getting initialized.

    Another alternative is to use a module like AppConfig to keep your config information in non-perl-variable form (that would require changing your config file, of course). Either solution is good in that it documents in your main script what is global, but global variables can be badly abused, so I might go with the second method.

Re: Using Variables
by vek (Prior) on Dec 27, 2001 at 23:24 UTC
    Would really have to see your code to give you an educated answer. I wrote a module to pull in the variables from the flat file and send the data back to the calling prog as a hash reference.

    The format on the variable file is 'variable:value':
    store_dir:/path-to/the-storage-dir
    Split on ':', create a hash. Send it back to the calling prog as a reference.

    Here's some quick sample code from the calling prog:
    my $config = getConfigVars (); my $storageDir = $config->{store_dir};
    This is nice if you have a lot of programs that need to know what the store_dir is. You only have to modify your config file and all progs will recognize the change.

    Alternatively if there are no other programs that care what store_dir is, why not store the vars in __DATA__ and have a routine that loops through those recs:
    my %config; while (<DATA>) { my ($var, $val) = split (/:/, $_); $config{$var} = $val; } __DATA__ store_dir:/path-to/the-store-dir

      Storable is a more reliable way to store variables. From the Storable manpage:

      use Storable; store \%table, 'file'; $hashref = retrieve('file');

      or alternatively:

      use Storable qw(nstore store_fd nstore_fd freeze thaw dclone); # Storing to and retrieving from an already opened file store_fd \@array, \*STDOUT; nstore_fd \%table, \*STDOUT; $aryref = fd_retrieve(\*SOCKET); $hashref = fd_retrieve(\*SOCKET);

      Of course, if you need to include other things besides variables in the config, you may want to consider turning your code into a module

        Storable is a more reliable way to store variables.

        Except that then you lose the ability to easily view and edit the file, which is what you'd like to be able to do with most config files (I like Storable, but not for this) :-)