Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Re: %h->{foo} vs. $h{foo}..

by smferris (Beadle)
on May 30, 2003 at 05:59 UTC ( [id://261773]=note: print w/replies, xml ) Need Help??


in reply to %h->{foo} vs. $h{foo}..

Documented or not, it was implemented.. So, I guess, that's two people that didn't think it was totally unreasonable. I'm also guessing that person was an experienced perl coder that implemented it.. Their patch WAS accepted afterall. ;D (For quite awhile I might add.. 5.005 through 5.6.1, at least)

Anyway.. You both make good points.. I'm to focussed on whats to the left of the arrow, where I should be focussing on the arrow itself. In that case, it makes perfect sense why it's wrong.

Good thing I just stumbled on this, feature!? 8D I haven't used it much. Thanks for the discussion!

Replies are listed 'Best First'.
•Re: Re: %h->{foo} vs. $h{foo}..
by merlyn (Sage) on May 30, 2003 at 15:08 UTC
    Documented or not, it was implemented.
    No, this is an unfair statement. There was no extra code added to make it work. Instead, there wasn't enough code to make sure it was rejected as illegal.

    As others have pointed out, this code to make sure it's rejected as illegal is being slowly introduced into the implementation, so as not to break anyone's code that took advantage of this bug in the existing implementation.

    Amongst the developers, there is no controversy that @array->[$element] and %hash->{$element} has always been a bug.

    -- Randal L. Schwartz, Perl hacker
    Be sure to read my standard disclaimer if this is a reply.

      Sorry.. Not trying to beat a dead horse here.. But I don't think it's fair to call it a bug either.. not if something similar is being implemented in Perl6. %foo.{key} is different from %foo->{key}? Both let you access a hash as a hashref, rather than as a hash. (I'm assuming this is the source of my confusion. something I don't know about the Perl6 implementation, perhaps?)

      Un-Intentional, perhaps.. but a bug? Of course, I've never looked at more than a few lines of the source.. so I'm no-one to argue. 8) and please don't take it as such.. I really am just trying to see the big picture here.

      I love Perl and being bashed for something stupid I've done only makes me better.. right? ;D (provided I learn from it, of course) But.. Just to clear my point of view a little further: (Bare with me, this is long, I apologize)

      For instance, take a 1000 new Perl coders.. Explain that $ represents a scalar, and % represents a hash structure.. (Incomplete, I will not disagree.. but still a true statement) Now give them the code:

      1> $foo='bar'; 2> $foo{'bar'}='baz!'; 3> %foo=('calvin','hobbes');

      Ask them what foo represents on each line. (a hash or scalar) For the experienced perl coder, no problem. For the unexperienced, I'll bet the number of wrong answers isn't zero. (Line 2 being the one likely confused of course) Now give them the code:

      1> $foo='bar'; 2> %foo->{'bar'}='baz!'; 3> %foo=('calvin','hobbes');

      Ask again.. I'll bet their accuracy is a bit better.. My opinion is, the unexperienced perl coder could probably go their whole life w/o knowing about, or at least understanding refs and still write code that worked for them. Perfect example... back in the Perl 4 days.. A collegue of mine showed ME what a hash was.. Some 7-8 years later, I find myself trying to explain a hashref to them because they're trying to copy a hash (HOH) with $a{1}=$b{1} and they can't figure out why they can't modify $b{1} w/o affecting $a{1}.. But yet.. It's taken them almost 8 years to discover it. 8) (And I know they're still confused by them) I know they've used modules before and have had to instantiate them, so, indirectly they knew about 'em.. They were just unaware of them.

      As for the experienced, it may not look right, but I'll bet the number of wrong answers is closer to 0.. or at least much closer than the newb people with the initial code.

      Heck, Exegesis 2 is the reason I even tried %foo->{blah} to begin with.. I thought, Hmmm, what if that was available now, in 5.6.1? Let's try it.. Get used to using it.. and, Wow.. whataya know. it worked!

        But I don't think it's fair to call it a bug either.. not if something similar is being implemented in Perl6.
        No, that doesn't fly with me. There are hundreds of new syntax forms in Perl6.

        In Perl5, the closest interpretation you could apply to %foo->{$bar} is "treat %foo in a scalar context" (which returns the N-over-M bucket usage string) "then dereference that" (as a string, it'd have to be a soft reference) "as a hash reference, and take the element with the key of $bar".

        Sorry, that's just next to useless. And the implementers agreed. The only reason it worked is because %foo left the reference to foo on the stack for things like hash functions (keys, each, etc), and then the dereference arrow couldn't tell whether it was a reference from a real referencing, or from the literal name.

        So, it worked only because it wasn't disallowed, as it should have been disallowed.

        Please stop bringing irrelevant items into the discussion. Perl6 syntax is Perl6 syntax. Perl5 syntax had a meaning for that syntax, except the implementation was broken, and the meaning was almost always not what people accidentally typed. Hence, it is now just becoming forbidden to deal with all possible mistakes.

        -- Randal L. Schwartz, Perl hacker
        Be sure to read my standard disclaimer if this is a reply.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others goofing around in the Monastery: (6)
As of 2024-05-29 08:06 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found