in reply to Re: Re: dereferencing syntax Re: Getting keys/values from a referenced hash
in thread Getting keys/values from a referenced hash

Hi Andy!

I found out now (using Dumper-mod) that my reference to the nested hash is undefined (but only when I use use strict;)
When I don't use strict the reference ($results) holds all the data I want and is dumped correctly. Here's the way I get the nested hash from a file (the only possibility I have):

my $filename = $ARGV[0]; open (_filehandle_, "<$filename") or die "Meeeeep! $filename does not +exist!\n"; my @file = <_filehandle_>; close (_filehandle_); my $line = join "", @file; my $results = eval ($line); print '$results: '.Dumper($results); my $colpattern = $results->{"col_pattern"};

That's pretty straight and ineffective code, but I better first try how things work and squeeze it up later then. ;-)
The fact that $results is undef tells me sth. goes wrong with eval().
Can it be that the use strict-statement affects or somehow impairs the evaluation of the file's contents (which is in $line now)?

Greetz, Micha

Replies are listed 'Best First'.
Re: Re: dereferencing stuff
by blakem (Monsignor) on Nov 28, 2001 at 18:26 UTC
    Looks like the code you are evaling isn't strict compatible. You should check the value of $@ right after your eval. For instance, try this script with and without strict:
    #!/usr/bin/perl -wT use strict; my $c; my $code = '$global = 10; $c = 6'; eval $code; die $@ if $@; print "\$c = $c\n"; __END__ =head1 OUTPUT with strict Global symbol "$global" requires explicit package name at (eval 1) lin +e 1. =head2 OUTPUT w/o strict $c = 6
    Also, your slurp-and-eval would be cleaner as a require or a do instead.

    -Blake

      Seems to be the code from that file to be the error:

      Global symbol "$res_g2" requires explicit package name at (eval 2) line 1.

      Var $res_g2 holds the nested array I need to evaluate. Since I have no influence on the code written to the file I think I have to live without strict for that part.
      Thanks a lot!

      Micha

        Looks like you've got two options at this point. The first is just to turn strict off around that block:
        { no strict; eval $code; }
        Or you can attempt to pick off the errors one at a time, outside the eval;
        my $res_g2; eval $code; die $@ if $@;
        I'd recommend seeing how far you can get with the second method, though its quite possible that you'll encounter an error that cant be fixed in this manner.

        -Blake