Beefy Boxes and Bandwidth Generously Provided by pair Networks DiBona
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Autovivification

by merlyn (Sage)
on Oct 03, 2000 at 17:52 UTC ( #35095=note: print w/ replies, xml ) Need Help??


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

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


Comment on Autovivification
Download Code
RE: Autovivification
by clemburg (Curate) on Oct 03, 2000 at 20:24 UTC

    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

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (9)
As of 2014-04-25 09:26 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    April first is:







    Results (586 votes), past polls