Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked

hash reference question

by targetsmart (Curate)
on Jul 20, 2009 at 09:28 UTC ( #781575=perlquestion: print w/replies, xml ) Need Help??
targetsmart has asked for the wisdom of the Perl Monks concerning the following question:

here is one code
perl -e "%package = ( 'zips' => {1,2,3,4} ); print %package->{zips};"
other code
perl -e "%package = ( 'zips' => {1,2,3,4} ); print $package{zips};"
both the one liners print

but when I enable warning(-w) the former produces

Using a hash as a reference is deprecated at -e line 1.

I thought only the later should work, the former should fail, but it is also working but outputs along with a warning. can anyone tell me what happens if I do like %package->{key} considering the above example.

-- 'I' am not the body, 'I' am the 'soul', which has no beginning or no end, no attachment or no aversion, nothing to attain or lose.

Replies are listed 'Best First'.
Re: hash reference doubt
by davorg (Chancellor) on Jul 20, 2009 at 09:46 UTC

    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

      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.

      -- 'I' am not the body, 'I' am the 'soul', which has no beginning or no end, no attachment or no aversion, nothing to attain or lose.

        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

        but I can't understand what is going when I do %hash->{key}, kindly explain
        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.


        A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

Re: hash reference doubt
by jethro (Monsignor) on Jul 20, 2009 at 09:50 UTC
    The warning normally would be an error, but there was a bug in some old perl versions so that using %x->{} would be accepted. Since a lot of scripts inadvertently had used this bug (i.e. made a mistake that couldn't be found because the interpreter still did the right thing) the bug only received a warning so that those old scripts don't break.
Re: hash reference question
by cdarke (Prior) on Jul 20, 2009 at 12:40 UTC

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://781575]
Approved by Corion
[Your Mother]: Stick with what you know. Or post a SOPW and if it's not complicated I guarantee someone will answer; me if it's straightforward and no one else gets it better first.
[1nickt]: ++YourMother (Grammar Vigilante of Bristol)
[Lady_Aleena]: Um, how does grep do multiline? print "$file:$_" if grep { /get_(array|hash| data)\(.+\)/ } $_; only returns the same 18 lines the command line grep does.
[Lady_Aleena]: my scratchpad has what I have so far.
[choroba]: what do you mean by multiline?
[Discipulus]: thanks 1nickt!
[choroba]: grep is a filter, it selects elements from a list based on a boolean condition
[moritz]: you need to read the file in one go to do a multiline regex match
[erix]: ha ha, I like the Vigilante
[Lady_Aleena]: Forget it. I'm getting angry with it.

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (11)
As of 2017-05-23 19:51 GMT
Find Nodes?
    Voting Booth?