Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Re^4: Best Multidimensional Hash Practices?

by almut (Canon)
on Oct 13, 2009 at 01:18 UTC ( [id://800831]=note: print w/replies, xml ) Need Help??


in reply to Re^3: Best Multidimensional Hash Practices?
in thread Best Multidimensional Hash Practices?

I never said it's the exists itself that autovivifies...  sure it's the dereferencing. And the behaviour is all nice and dandy, if it does make sense to actually access the last element in the chain (and create intermediate structures as required), as is the case with your example where you're assigning a value.  It just doesn't make much sense when there's no real need for complete dereferencing, such as when testing for existence or definedness.

In other words, why not take the shortcut and just not dereference the entire structure when it's clear right in the beginning that this isn't going to lead to any new conclusions with respect to the existence of the final key?  I mean some special handling could be done for the sake of DWIM, just like it does happen elsewhere in Perl.

Replies are listed 'Best First'.
Re^5: Best Multidimensional Hash Practices?
by muba (Priest) on Oct 13, 2009 at 02:22 UTC

    I'd like to start this reply with saying that I might be completely wrong. I merely try to state why I think it works the way it works.

    when it's clear right in the beginning that this isn't going to lead to any new conclusions with respect to the existence of the final key

    Is it, though? If you see operators as functions with funny syntax (which is what operators are), you could say that the underlying function for -> is deref(HASHREF, KEY). Having that established, it is of importance to note that normal rules of precedence

    So the simple case of exists $hashRef->{key1}->{key2} boils down to

    exists( deref( deref( $hashRef, "key1" ), "key2" ) )
    Considering the order of precedence, the very first call that is made, is the innermost thing: deref($hashRef, "key1"). It would be weird if, at this point, perl would break in and said, wait! Before we're doing anything, let's find out why we're doing it anyway! and would analyze the whole statement to see if it's doing an assignment or simply an exists/defined check.

    Is it really worth the hassle? Or is it simpler to just learn the side effect of the -> operator?

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others examining the Monastery: (5)
As of 2024-04-24 10:39 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found