Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Re: eval not behaving like I expected... why?

by jdporter (Paladin)
on Mar 14, 2006 at 12:28 UTC ( [id://536549]=note: print w/replies, xml ) Need Help??


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:

  1. the variable names are known a priori to the calling code.
  2. the variables live in a namespace — and there is no ambiguity as to what that namespace is.

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:

# config: { fee=>"FEE", fi=>"FI", fo=>"FO", fum=>[qw/ fee fi fo fum /], }
then just eval the file as a whole:
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.

We're building the house of the future together.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://536549]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others chilling in the Monastery: (3)
As of 2024-04-19 05:05 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found