SPUG: derefencing hash reference / parsing config file (con't)

Todd Wells toddw at wrq.com
Sat Jan 8 14:14:52 CST 2000


Okay, now I have another question related to this nice routine you guys
suggested.  Basically, I ended up using a variation of this to process the
variables which can be nested, in this example I'm using "models".  

I have a feeling my problem is related to Camel p. 256 and Cookbook p.148,
but I haven't figured it out.

In the following code, assume that 
$config{models} = "{CCS = { name = CCS; pool = 1; max = 1; }; };" (without
the quotes)

Excerpted Code:

        my $line = $config{models};
        $line =~ s/=/=>/g;      # replace "=" with "=>"
        $line =~ s#\;#\,#g;     # change ";" to ","
        $line .= ";";           # add a ; to the end so it's Perl
        $models = eval $line;   # create a models hash

        print "\nmodels\n=======\n";
        foreach $var (keys %models)
        {
                my $value = $models{$var};
                print "$var is $value\n";
        }

        print "models is $models\n";

My output was only:

models
=======
models is HASH(0x15ba60)


So, like I said above, I think this is related to Camel p. 256 and Cookbook
p.148, hard references don't work as hash keys.  The cookbook says to solve
this problem using Tie::RefHash, so at the begining of my code I put:

	use Tie::RefHash;
	tie %models, "Tie::RefHash";

but my output did not change!  What am I doing wrong here?

I'm assuming Tie::RefHash is part of a standard Perl distribution?  I'm not
getting any warnings about it, so I'm assuming that it's there and
"working".  I'm running this script under Solaris.

Thanks for any help you can give,

-Todd


-----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