SPUG: parsing a config file

Todd Wells toddw at wrq.com
Sat Jan 8 12:11:26 CST 2000


Here's what I actually ended up doing -- I sent the code to the list
earlier, but it was the offending code with the magic word in it, so it only
made the list tagged onto Dr. Tim's msg (you had to scroll down quite a ways
to figure that out).  

This is not quite as elegant, but it seems to work.  I tried using Jim's
method, but hit the same snag you did.  I may tackle it again using your
revised method, we'll see... TMTOWTDI.  But your method is nice how it sets
up the nested hashes...

        open (CONFIG, "< $config_file")
                or die "Couldn't open $config_file for reading: $!\n";

        while (<CONFIG>)
        {
                chomp;  #remove newline character
                s/^\s*{\s*(?!\S)//;     # remove a lone "{" on a line
                s/^\s*};//;             # remove a line with only "};"
                s/^\s*}\s*(?!\S)//;     # remove a lone "}" on a line
                s/^\s*\w+ = {\s*(?!\S)//; # remove if only "NAME = {"
                next unless length;     # is there anything left?
                s/(.+);$/$1/;           # remove ";" at the end of line
                s/^\s+(.*)/$1/;         # remove leading whitespace
                # now split remaining string into variable and value
                my ($var, $value) = split(/\s*=\s*/, $_, 2);
                $config{$var} = $value; #create a hash of variable/value
pairs
        }




-----Original Message-----
From: Andrew Sweger [mailto:andy at n2h2.com]
Sent: Friday, January 07, 2000 7:31 PM
To: jimfl
Cc: Todd Wells; spug-list at pm.org
Subject: Re: SPUG: parsing a config file


On Jan 7, 2000 @ 3:19pm, jimfl wrote:

>       $cdata = q($Config = );
>       while ($line = <DATA>) {
>         $line =~ s/=/=>/g;
>         $line =~ s/\;/\,/g;
>         $cdata .= $line;
>       }       
>       $cdata .= ";";
>       eval $cdata;

Oh, that's very nice. That's a beaut. But it's slightly flawed. 

Odd number of elements in hash assignment at (eval 5) line 1, <DATA> chunk
28.
        eval '$Config = 
{
        Domain => {
                servers => (),
                status => Enabled,
        },
        Log => {
                easLogLevel => Standard,
                log => On,
        },
        Restart => {
                Condition => 0,
                Enabled => NO,
                RestartTime => {
                        CalenderFormat => "%d/%m/%Y %I:%M %p",
                        FromTime => "18/07/1997 03:00 AM",
                        ToTime => "18/07/1997 06:00 AM",
                },
                Schedule => 0,
        },
        Security => {
                administrator => Administrators,
                domain => MINE,
                manager => Users,
                security => Off,
                user => Users,
        },
}
;'
         ^---- here

It's that last comma. This works...

#!/usr/local/bin/perl -w

# use strict;
# Damn, can't use strict with barewords (in DATA)!

undef $/; # what's that sucking sound?

my $line = <DATA>;
$line =~ s/=/=>/g;
$line =~ s/\;/\,/g;
$line =~ s/}\s*,\s*}/}}/;  # changes '}, }' to '}}'
$line .= ";";
my $Config = eval $line;

__DATA__
blah
__END__

...but I'm not sure that chopping a comma out from between two closing
braces will be enough (or it could go horribly wrong depending on what's
in the data).

-- 
  Andrew Sweger <andy at n2h2.com>  |  N2H2, Incorporated
  v=206.336.2947 f=206.336.1541  |  900 Fourth Avenue, Suite 3400
     Advanced Technologies       |  Seattle WA 98164-1059
          Development            |  http://www.n2h2.com/


 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
    POST TO: spug-list at pm.org        PROBLEMS: owner-spug-list at pm.org
 Seattle Perl Users Group (SPUG) Home Page: http://www.halcyon.com/spug/
 SUBSCRIBE/UNSUBSCRIBE: Replace ACTION below by subscribe or unsubscribe
        Email to majordomo at pm.org: ACTION spug-list your_address


 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
    POST TO: spug-list at pm.org        PROBLEMS: owner-spug-list at pm.org
 Seattle Perl Users Group (SPUG) Home Page: http://www.halcyon.com/spug/
 SUBSCRIBE/UNSUBSCRIBE: Replace ACTION below by subscribe or unsubscribe
        Email to majordomo at pm.org: ACTION spug-list your_address





More information about the spug-list mailing list