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

RE: RE: Re: A Not so Simple Append

by clemburg (Curate)
on Oct 03, 2000 at 12:18 UTC ( [id://35073]=note: print w/replies, xml ) Need Help??


in reply to RE: Re: A Not so Simple Append
in thread A Not so Simple Append

Thanks for pointing this out! I did not know that this is possible - all the documentation about autovivification mentions it in the context of "chain" dereferencing, so I thought (or rather, "feeled") that it was connected to this operation exclusively. Is there any statement in the docs or the source somewhere that defines when autovivification will happen?

Christian Lemburg
Brainbench MVP for Perl
http://www.brainbench.com

Replies are listed 'Best First'.
Autovivification
by merlyn (Sage) on Oct 03, 2000 at 17:52 UTC
    It's pretty simple. I don't know how it's documented, since I learned it mostly by folklore (and by watching the response to bug reports on P5P {grin}).

    When a variable containing undef (or a new element of an array or hash) is used in an "lvalue-context" (on the left side of an assignment, for example) as a reference, a reference to the appropriate anonymous thingy is inserted and the operation continued, rather than throwing an error for an attempt to dereference undef.

    So, push @$x, 35 works even if $x is undef, because we're about to dereference $x while trying to store something.

    It's really just an extension of the principle that

    $last_name{"fred"} = "flintstone";
    works, even though I didn't originally create the element with that key.

    -- Randal L. Schwartz, Perl hacker

      Thanks for saving me the hours to watch the p5p list ... and for the crisp explanation!

      Christian Lemburg
      Brainbench MVP for Perl
      http://www.brainbench.com

RE (tilly) 4 (autovivify): A Not so Simple Append
by tilly (Archbishop) on Oct 03, 2000 at 18:33 UTC
    merlyn and I disagree on whether it is documented.

    Look at perlref, item 6 on how to create a reference is to dereference them in a context that assumes they exist. That is why chained lookups autovivify, to continue down the chain the previous lookups were dereferenced in a context that assumed they existed. Likewise if you start assigning to stuff, it autovivifies. That holds for hashes and variables.

    Now merlyn certainly knows this piece of documentation, so why do we disagree on whether it is documented? Well to his eyes this requires serious "reading between the lines" and you cannot expect people to figure it out. To mine, well the reader's attention is not drawn to the point, but once I saw that this is the rule that applies, I have not since run into an autovivification situation which I had trouble understanding...

        Because we discussed this specific piece of documentation in chatter once, talking about whether or not chained lookups were documented to autovivify. :-)

        Cheers,
        Ben

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others exploiting the Monastery: (3)
As of 2024-04-19 20:04 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found