http://www.perlmonks.org?node_id=489017


in reply to Reading and Writing Perl Config Files

Thank you very much,jdhedden for this excelent lesson of OO use of this basic config file style!

I agree that there are several interesting aproaches to standarize config files. I also prefer the Apache config way. OTH, I should better prefer an XML way. I know.

But this is similar to what I use in my personal config files. And in this OO way, I won't feel guilty when keeping on doing so, programatically. It is error prone!

I should only point out that it is not possible to keep a special order in the variables, because of the hash nature of these config files.

But it is Ok for me and my scripts :)

{ \ ( ' v ' ) / }
( \ _ / ) _ _ _ _ ` ( ) ' _ _ _ _
( = ( ^ Y ^ ) = ( _ _ ^ ^ ^ ^
_ _ _ _ \ _ ( m _ _ _ m ) _ _ _ _ _ _ _ _ _ ) c h i a n o , a l b e r t o
Wherever I lay my KNOPPIX disk, a new FREE LINUX nation could be established
  • Comment on Re: Reading and Writing Perl Config Files

Replies are listed 'Best First'.
Re^2: Reading and Writing Perl Config Files
by simul (Novice) on Feb 19, 2009 at 03:22 UTC
    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...)
      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).