SPUG: parsing a config file

Andrew Sweger andy at n2h2.com
Fri Jan 7 21:31:09 CST 2000


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





More information about the spug-list mailing list