Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Re^2: Reading and Writing Perl Config Files

by simul (Novice)
on Feb 19, 2009 at 03:22 UTC ( [id://744931]=note: print w/replies, xml ) Need Help??


in reply to Re: Reading and Writing Perl Config Files
in thread Reading and Writing Perl Config Files

The trouble with this is that config files are not always 100% "trusted" sources of information. Other processes may write to them. I would reccommend to anyone reading this, at the very least.
use Safe; my $comp = new Safe; $comp->reval($config_file);
Instead of just "do" on a config file This gets you, at least, a modicum of protection from your own errors. (Other than that, though, I'm gonna use this code right now...)

Replies are listed 'Best First'.
Re^3: Reading and Writing Perl Config Files
by Anonymous Monk on Mar 27, 2009 at 01:33 UTC
    Hi,

    I'm trying to apply code and have problems. Would kindly ask for help.

    I have this in Log.conf :
    # Defines logging groups in terms of room: if device's room maatches r +oom name or if device name is listed separately in hash, then is logg +ed into same log: %Log_rooms = ( "gallery" => { 'BM_shutts_south_00' => 1, 'BM_shutt_no +rth_00' => 1 }, "kitchen" => { }, "office" => { }, "living" => { }, "utility" => { }, "kurilnica" => {}, "lobby" => { }, "toilet" => { } );


    I'd like to read that hash definition into Perl program :
    use Data::Dumper; use Safe; my %Log_rooms; my $config_file ="./Log.conf"; if (-e $config_file) { print "File Exists!"; # my $comp = new Safe; # my $return =$comp->reval($config_file); my $return = do $config_file ; warn "couldn't parse $config_file: $@" if $@; warn "couldn't do $config_file: $!" unless defined $return; warn "couldn't run $config_file" unless $return; } else { %Log_rooms = ( "default" => { 'BM_shutts_south_00' => 1, 'BM_shutt_no +rth_00' => 1 } ); }; print "############################################################### +#################\n"; print Data::Dumper::Dumper(%Log_rooms);


    but I don't get anything in %Log_rooms (default works if config file is missing).

    I'd also like to implement safer way with using Safe, but it also doesn't work. I'd kindly ask for some help...

    Thanks in advance,

    Rob.

      You will have to use $comp->rdo instead of $comp->reval when using a file name.

      Uncomment the two Safe lines and comment the 'do' line.

      then try this print statement:

      print Data::Dumper::Dumper(%Safe::Root0::Log_rooms);

      You can set the name space to something more readable too.

      my $comp = new Safe 'CFG'; print Data::Dumper::Dumper(%CFG::Log_rooms);

      pvb.

      With the line:
      $comp->reval($config_file)
      you are trying to evaluate the file name of the config file. You'd have to open it You want to use rdo instead of reval (or first read the file into a string).

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others having a coffee break in the Monastery: (3)
As of 2024-04-24 03:28 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found