Beefy Boxes and Bandwidth Generously Provided by pair Networks Cowboy Neal with Hat
laziness, impatience, and hubris
 
PerlMonks  

Re: The Bad, the Ugly, and the Good of autovivification

by Roy Johnson (Monsignor)
on Apr 08, 2005 at 02:31 UTC ( #445931=note: print w/ replies, xml ) Need Help??


in reply to The Bad, the Ugly, and the Good of autovivification

Yes, autovivification is capricious. It would be a great improvement if it only happened in lvalue contexts (ordinary value contexts could simply short-circuit and return null). If I may add an adjective to your canonical list, I'd like to present The Simple: if Perl has to go through (dereference) a reference to get to something, autovivification happens (obviously, it also happens if the reference is itself used in lvalue context).

The subject does, from time to time, come up here, as in Looping through a hash reference is creating a key...?.


Caution: Contents may have been coded under pressure.


Comment on Re: The Bad, the Ugly, and the Good of autovivification
Re^2: The Bad, the Ugly, and the Good of autovivification
by tlm (Prior) on Apr 08, 2005 at 03:31 UTC

    If I may add an adjective to your canonical list, I'd like to present The Simple: if Perl has to go through (dereference) a reference to get to something, autovivification happens...

    Yes, this is simple for the experienced programmer who is comfortable with the whole notion of references, but not so simple for the programmer who is just beginning to work with them. I know from helping newbies at work that references don't come easy to many people for some reason. And even in the best of cases, one first has to become sensitized to the possibility of autovivification-mediated trouble before one develops an eye for unintended autovivification. This is true for just about any class of bugs. (perlreftut, perlref, and perltrap need to do more towards alerting programmers to autovivification bugs.)

    And even with a bit of experience, something like this:

    my @good_ones = grep $_->stars == 5, @dvds{ qw( Ray Alexnader Sideways Catwoman Avia +tor ) };
    can silently trip you. Or, while it's clear that
    if ( exists $h{ wild_guess }->{ ssn } ) { ... }
    is dereferencing, and hence autovivifying, $h{ wild_guess }, it is less clear (or at least it was so to me) that
    keys %{ $h{ wild_guess } }
    is also autovivifying $h{ wild_guess } even though apparently there is no dereferencing going on (if by "dereferencing" one means getting the value at a given address). That's why mMy version of your Simple is "any time that perl is asked to interpret an undef as if it were a hash ref (or array ref, or scalar ref), it will turn the undef into a ref to an empty hash (or empty array, or undef)."

    Update: What am I saying?! Of course there is dereferencing in %{ $h{ wild_guess } }. So "asked to interpret an undef as if it were..." is just a wordier way to say "dereference". I confess that I find it more intuitive somehow, but I'd still say that your Simple is much simpler than mine.

    Update: Added perltrap to the docs list above.

    the lowliest monk

      perlreftut and perlref need to do more towards alerting programmers to autovivification bugs.
      Really, like what?

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (10)
As of 2014-04-17 04:03 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    April first is:







    Results (439 votes), past polls