Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

autovivication gonna make me lose my mind

by dreadpiratepeter (Priest)
on Sep 16, 2008 at 15:22 UTC ( [id://711721]=perlquestion: print w/replies, xml ) Need Help??

dreadpiratepeter has asked for the wisdom of the Perl Monks concerning the following question:

Apparently, I have been doing something wrong for years, or I missed a change to Perl recently. I saw this code in an answer today:
#!/usr/local/bin/perl use strict; use warnings; use Data::Dumper; $Data::Dumper::Indent = 1; my @array1 = qw{rose orange green}; my @array2 = qw{blue red}; my %hash; push @{$hash{first}}, \@array1, \@array2; print Dumper \%hash;
I was under the impression that this code will not run. Specifically, because of the array deref. $hash{first} should return undef, and I did not think you could dereference an undefined value and magically get an array reference, but the code runs. I had always used:
$hash{first} ||= []; push @{ $hash{first} },'foo';
I am sure (in my own mind, at least) that I would get run-time errors if I left out the initialization.
Can someone enlighten me as to where my cognitive breakdown lies?


-pete
"Worry is like a rocking chair. It gives you something to do, but it doesn't get you anywhere."

Replies are listed 'Best First'.
Re: autovivication gonna make me lose my mind
by kyle (Abbot) on Sep 16, 2008 at 15:52 UTC

    If you're dereferencing an element as an lvalue (i.e., in a kind of assignment), it will autovivify. If you're dereferencing otherwise, it will fail.

    For example:

    $ perl -le 'use strict;my %h;push @{$h{first}}, "hello";print $h{first +}' ARRAY(0x8153d78) $ perl -e 'use strict;my %h;print @{$h{first}}, "hello";' Can't use an undefined value as an ARRAY reference at -e line 1.
      A ha, that's my disconnect. I got the error for an rvalue at some point and drew the wrong conclusion (I generalized it to lvalues too). So I have cargo-culted myself for the last few years. Time to simplify some code.
      Thank you so much for dispelling my confusion


      -pete
      "Worry is like a rocking chair. It gives you something to do, but it doesn't get you anywhere."
Re: autovivication gonna make me lose my mind
by Fletch (Bishop) on Sep 16, 2008 at 15:42 UTC

    Dereferencing an undefined value to (magically) get an appropriate reference is kind of the definition of autovivification; wherein lies the confusion?

    The cake is a lie.
    The cake is a lie.
    The cake is a lie.

Re: autovivication gonna make me lose my mind
by tilly (Archbishop) on Sep 16, 2008 at 16:51 UTC
    As far as I know, Perl's autovivification behaviour last significantly changed in Perl 5.004. So Perl has had its current behaviour for a decade or so.

    However if you use Perl and any other language, then your belief was probably a carryover from other languages. Autovivification is a fairly rare feature, for instance it is not found in any of Ruby, Python, JavaScript or Java.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others avoiding work at the Monastery: (3)
As of 2024-04-26 05:48 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found