Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid

Re: hash reference doubt

by davorg (Chancellor)
on Jul 20, 2009 at 09:46 UTC ( #781576=note: print w/ replies, xml ) Need Help??

in reply to hash reference question

If you get a warning from Perl that you don't understand then it's always worth checking the full description in perldiag. In this case, it says:

Using a hash as a reference is deprecated
(D deprecated) You tried to use a hash as a reference, as in %foo->{"bar"} or %$ref->{"hello"} . Versions of perl <= 5.6.1 used to allow this syntax, but shouldn't have. It is now deprecated, and will be removed in a future version.

I assume that the fact it used to work has something to do with the internal representation of hashes (and also arrays) as something similar works in C.

But as the warning says, this usage is deprecated. And if something is deprecated, you shouldn't use it.

p.s. It's a "question", not a "doubt".


See the Copyright notice on my home node.

Perl training courses

Comment on Re: hash reference doubt
Re^2: hash reference doubt
by targetsmart (Curate) on Jul 20, 2009 at 09:51 UTC
    I know that it is deprecated, I never used this %hash->{key}, I came across some code like that, wanted to understand how it works; for my knowledge.
    my question is, I can understand what is going on when I do $hash{key}, but I can't understand what is going when I do %hash->{key}, kindly explain.


      What is going on is that Perl is exposing more of its internals than it really should be. The name of the hash is being interpreted like a reference when you use this syntax.

      %hash->{key} should never have worked, but some people noticed that it did and (wrongly) started to use it. And that's why it's just deprecated and hasn't been removed from the language yet.


      See the Copyright notice on my home node.

      Perl training courses

      There is nothing to understand or explain, other than that %hash->{key} should have triggered a syntax error in Perl. Some older Perls didn't and therefore this error was downgraded to a warning to avoid breaking existing scripts.

      A reference should always be a scalar (which can be part of an array, a hash or indeed an object or the return value of a subroutine, but not the array, hash, ... itself.

      BTW you will trigger a similar warning with the following:

      @array = qw /1 2 3 4 5/; print @array->[1];
      It really is the same issue.



Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://781576]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (6)
As of 2014-12-28 08:20 GMT
Find Nodes?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?

    Results (179 votes), past polls