in reply to getting hashes out of config files

Assuming "foo.dat" is the file which consists of what Data::Dumper stored, AND that "%hash" is the name of the hash defined in that file, all you need to do is
use strict; use vars '%hash'; do('foo.dat');
And the hash should be set. I point out the use strict and use vars because I was bitten just yesterday by the problem that since the data resides in a separate file, do(file) doesn't work well with lexical variables (those created with my). See perlfunc:do for more info, and thanks to merlyn, dchetlin, and tye for helping sort me out on this issue.

(Oh yeah, and check the file Data::Dumper created to make sure it defines '%hash')

Philosophy can be made out of anything. Or less -- Jerry A. Fodor

Replies are listed 'Best First'.
RE: RE: getting hashes out of config files
by Anonymous Monk on Oct 25, 2000 at 22:31 UTC
    that works nicely, except for one problem:

    do('$filename');

    doesn't seem to work, however if i type the path and file instead of using $filename variable it works ... is there a way to use the $filename variable with the do function?

    it tend to be much more usefull to use a variable ... its a lot sorter thanks in advance
      You're using single quotes, which do not do variable interpolation. Thus, you are attempting to open the file named $filename (literally). Remove the quotes entirely, as they're unnecessary, or use double-quotes, which will correctly do interpolation:
      do($filename) or die "..."; do("$filename") or die "...";
        double quotes and no quotes yield the same result as single quotes ... i have been using hte following to test:
        do('filename'); %hashname = %$VAR1; print "$hashname{name} \n\n"; ###the above works fine ###the code below only prints the \n's $filename = "filename"; do($filename); %hashname = %$VAR1; print "$hashname{name} \n\n";
        thanks again