in reply to eval not behaving like I expected... why?
Other folks have helped you figure out your perl/syntax issues, but I think there's a more fundamental issue + solution.
If you're going to have your config file be executable perl code that sets some variables, at least one of the following conditions should hold:
In the first case, you can declare the variables with my in the calling code; that solves the problems you were having.
In the second case, you can write your config variables like so:
$config::fee="FEE" $config::fi="FI" $config::fo="FO" @config::fum=qw / fee fi fo fum /
Of course, that means that the variables are global, rather than lexicalized to the scope of the caller.
To get a little more fancy with that approach, you could insert a dynamically decided namespace into the assignments before eval'ing:
my $namespace = "here"; while (<DAT>) { s/([\$\@\%])/$1$namespace\::/; eval $_; }
But if you really want to go the lexical route, my personal inclination would be to store all the config variables in a single anymous hash:
then just eval the file as a whole:# config: { fee=>"FEE", fi=>"FI", fo=>"FO", fum=>[qw/ fee fi fo fum /], }
my $config_hr = do 'datafile.txt';
If you're trying to keep it simple for the sake of some non-programmers who'll have to be maintaining these config files, I respectfully suggest that you abandon the eval'able code approach altogether, and use one of the solutions already created to address this concern. Please take a look at a short list of config file modules.
|
|---|