[Pdx-pm] defined $var->{key} changes $var in to a hashref?

Michael G Schwern schwern at pobox.com
Mon Feb 22 16:21:15 PST 2010

benh wrote:
> ok I guess I should rephrase, I understand ~why~ it's doing this
> ($var->{} builds out the hash, not defined), but it's not quite what
> my ~expectation~ was. What I was thinking was going to happen was more
> along the lines of:
> grep{$x = $_; $x->{key}} $var
> so that $var was not touched... though I can't think of a good way for
> defined to effectively clone in place as the transformation would have
> already happened as far as defined was concerned.

Welcome to the Wonderful World of Autovivification.  Generally considered to 
be a bug, or an overzealous feature, in cases where it vivifies just to check 
if something exists.  Unfortunately, it was ill defined and is considered too 
ingrained into Perl to change.

autovivification.pm can control this behavior lexically.  perl5i comes with 
"no autovivification" built in.

54. "Napalm sticks to kids" is *not* a motivational phrase.
     -- The 213 Things Skippy Is No Longer Allowed To Do In The U.S. Army

More information about the Pdx-pm-list mailing list