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

Hi Andy,

I got a question concerning dereferencing hashes and use strict;.
I have a reference to a hash, derived from a reference to a nested hash:

my $hashref = $nested->{"key_to_hash"};

When I dereference that $hashref via

line 35:   my %realhash = %{$hashref};

and when I "use strict" I get the following message:

Can't use an undefined value as a HASH reference at /home/user/programming/perl/perlscript.pl line 35 (#1) (F) A value used as either a hard reference or a symbolic referenc +e must be a defined value. This helps to delurk some insidious errors.

Line 35 is the line where I dereference from $hashref.
When I don't use strict everything works fine...I got no idea what means "defined value" here. All my refs and vars are defined, or am I wrong?

Have a nice day and thanks for the tips yesterday!
Micha

Replies are listed 'Best First'.
Re: Re: dereferencing syntax Re: Getting keys/values from a referenced hash
by andye (Curate) on Nov 28, 2001 at 16:02 UTC
    Hi again Misha, here's my $.2,

    Well, this line (which I think is equivalent) works ok:

    perl -e 'my $nested = { key_to_hash => {data => "here"}}; print %{$nes +ted->{key_to_has h}};'
    The error message simply means that $hashref is undefined, i.e. the scalar called 'hashref' contains no value. So this means that there is a problem with $nested->{key_to_hash} - maybe the key key_to_hash doesn't exist, or the value is undefined. Maybe $nested is pointing to the wrong hash.

    An easy way to see what's gone wrong is to use the module Data::Dumper - which is standard, so you probably have it. Do this:

    use Data::Dumper; print Dumper($nested);
    and you'll see the contents of the hash referred to by $nested.

    If you can't see what's up with it, post the code where you set up $nested.

    Have a nice day and thanks for the tips yesterday!
    That's ok, you too.

    andy.

      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

        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

      (whoops! I think I started a new thread accidentally by changing the subject line...)

      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. ;-)
      As output from ::Dumper I get: $results: $VAR1 = undef;
      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

        Yep, first think, then post. Stupid me.
        The posts just didn't show up coz I restricted the note depth...I noticed now, sorry for that!

        Micha