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

I have a script that does something like this:

${%$Hash_reference}{$values[0]} = \@values;

When I attempt to run it locally (where I have Perl 5.10.1) it gives me a "Can't use string as a HASH ref while "strict refs" in use" error. It's apparently converting the hash reference to a string giving me values like "0" or "6/16".

The part that confuses me is that it runs fine on the server. The only difference that I've been able to find is that the server appears to be using Perl 5.8.9. I tried looking through the version deltas to see what could have caused the incompatibility but couldn't find anything.

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.

Replies are listed 'Best First'.
Re: Hash dereference breaks from 5.8.9 to 5.10.1
by ikegami (Patriarch) on Dec 06, 2011 at 20:48 UTC

    There was a bug in Perl where

    ${%$Hash_reference}

    was equivalent to

    ${\%$Hash_reference}

    instead of evaluating the hash in scalar context. Apparently, 5.8.9 suffered from this bug, but 5.10.1 did not. The proper syntax is

    ${ ... expression that returns a hash reference ... }{ ... }

    so the following makes no sense:

    ${ %$Hash_reference }{ ... }

    You should have been using

    ${$Hash_reference}{$values[0]} = \@values;

    which is clearer when written as follows:

    $Hash_reference->{$values[0]} = \@values;

      That just made my day. This is a legacy script that everybody hates, and this gives us yet another thing for which to mock it.

Re: Hash dereference breaks from 5.8.9 to 5.10.1
by SuicideJunkie (Vicar) on Dec 06, 2011 at 20:17 UTC

    I don't see why that would work at all.

    I think you want something like: $Hash_reference->{$values[0]} = \@values;

Re: Hash dereference breaks from 5.8.9 to 5.10.1
by jwkrahn (Abbot) on Dec 06, 2011 at 20:19 UTC
    what the best way would be to fix the script to work correctly in both versions.

    Yes, dereference the hash reference correctly, thusly:

    $Hash_reference->{$values[0]} = \@values;