27

I'm developing script, that is reusing some really old piece of perl code.

This line gives me still the error Using a hash as a reference is deprecated .

  %hash->{$_[$counter]} = $_[$counter+1];

How I have to refactor this code, so that I will be not receiving the error.

2
  • 8
    It's also useful to know that Perl's diagnostic messages are documented in perldiag, which can be accessed here (perldoc.perl.org/perldiag.html) or directly on the command-line (perldoc perldiag).
    – FMc
    Jun 7, 2011 at 13:27
  • 3
    The warning is the result of that line does not behaving as documented. The LHS of -> should be a reference, which is a scalar, and %hash in scalar context shouldn't return a reference to self.
    – ikegami
    Jun 7, 2011 at 17:34

2 Answers 2

32

Try

$hash{$_[$counter]} = $_[$counter+1];
24

What's to the left of ->{ should be a hash reference, not a hash. If you have a hash, omit the -> and just say $hash{.

Pedantically, %hash->{...} should do what (my $temp=%hash)->{...} does: get the scalar value of %hash (e.g. "1/8", indicating 1 of 8 buckets used) and use that as a symbolic hash reference (failing with an error under use strict "refs"). But due to an accident, it was quietly reinterpreted as $hash{...} instead. This bug will be fixed some day, but in the meantime people are being warned to change their incorrect code.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.