Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hi, I want to use something like this in a script of mine :
print keys %{ hash_ref -> { key } }; #( Hash of Hash )
But when I try to execute this with 'strict' on, I get an error telling me that this is a symbolic reference.
Seems I haven't really understood what a sym ref is because I always thought that that would be something like this :
print keys %{ hash_ref -> { "key" } }; #( Hash of Hash )
or
print keys %{ "hash_ref -> { key }" }; #( Hash of Hash )
How should I rewrite my first line to make it work with 'strict' turned on. Thanks.

Replies are listed 'Best First'.
Re: Why is this a symbolic reference?
by btrott (Parson) on Mar 05, 2001 at 00:08 UTC
    You're missing a $. Your code should look like this:
    print keys %{ $hash_ref -> { key } };
    The symbolic reference error comes from your code trying to dereference a string:
    hash_ref->{key}
    hash_ref isn't a hash ref, it's a string. $hash_ref is the hash ref.
Re: Why is this a symbolic reference?
by japhy (Canon) on Mar 05, 2001 at 00:09 UTC
    It's a symbolic reference because you're doing: hash_ref->{key}, which is like "hash_ref"->{key}, which is ending up being $hash_ref{key}, but in a round-about way.

    Perhaps you meant $hash_ref->{key}.

    japhy -- Perl and Regex Hacker

Re (tilly) 1: Why is this a symbolic reference?
by tilly (Archbishop) on Mar 05, 2001 at 02:21 UTC
    Even if you put the dollar signs in, your placement of quotes in the second version would make it a symbolic reference. And in the first version it might arise still if some of the values in %hash_ref are not hashes. (That is the kind of error that can be very, very hard to track down without strict on.)

    Still try this:

    print keys %{ $hash_ref->{$key} };
    and it will work as long as your data structure is what you think it is.

    BTW for inspecting a data structure I usually use Data::Dumper.