linuxperlhashref

Can't use string ("6/16") as a HASH ref while "strict refs" in use


I have a script which has code something like below.

$shelf->print("\nStarted syncing from \"${%Family::MEMBERS}{$member}\" \n");

When I attempt to run it locally on CentOS 7.0 with perl(v5.8.8) it works fine, however the same code on same OS with perl(v5.16.3) it gives me below error.

Can't use string ("6/16") as a HASH ref while "strict refs" in use at

I'd appreciate any advise on what changes might have caused this problem, and what the best way would be to fix the script to work correctly in both versions.


Solution

  • $BLOCK{EXPR} accesses an element of the hash referenced by the expression returned by BLOCK. Since a reference is expected, the block is evaluated in scalar context. %Familly::MEMBERS can't possible result in a reference in scalar context. (It results in a false value or a string describing statistics about the hash.)


    What you asked:

    To print $member, you can use

    print('$member'."\n")
      -or-
    print("\$member\n")
    

    What you could have meant:

    To print the value of $member, you can use

    print("$member\n")
    

    What I think you want:

    To access the element of %Familly::MEMBERS whose key is the value of $member:

    $Familly::MEMBERS{$member}
    

    The fact that you use a fully-qualified name doesn't change anything; the syntax is still $NAME{EXPR}.


    By the way, there's only one "L" in "family".