develooper Front page | perl.perl5.porters | Postings from November 2003

Question about rules for autovivification

Thread Next
From:
Orton, Yves
Date:
November 6, 2003 10:49
Subject:
Question about rules for autovivification
Message ID:
71B318898201D311845C0008C75DAD1C0896137B@defra1ex2
Hi,

Im trying to figure out an aspect of the autovivification. Specifically why
the construct being used changes whether autovivification occurs or not. Ie
for the deref @{EXPR}, whether autovivification occurs is dependent on both
the type of EXPR and the context within which @{EXPR} is used.

For instance 

  D:\>perl -Mstrict -wle "my $f = { }; my @a=map { $_ } @{ $f->{foo} }"

  D:\>perl -Mstrict -wle "my $f = { }; print 'ok' for @{ $f->{foo} }"

  D:\>perl -Mstrict -wle "my $f = { }; my @a=@{ $f->{foo} }"
  Can't use an undefined value as an ARRAY reference at -e line 1.

  D:\>perl -Mstrict -wle "my $f = { }; print 'ok' if @{ $f->{foo} }"
  Can't use an undefined value as an ARRAY reference at -e line 1.

  D:\>perl -Mstrict -wle "my $f = { }; print 'ok' for @{ undef() }"
  Can't use an undefined value as an ARRAY reference at -e line 1.

Now im well aware that @{EXPR} autovivifies when its directly or indirectly
used as STORE, ie:

  D:\>perl -Mstrict -wle "my $f = { }; push @{ $f->{foo} },'foo'"

But why does it autovivify in a FETCH here:

  D:\>perl -Mstrict -wle "my $f = { }; pop @{ $f->{foo} }"

but not here

  D:\>perl -Mstrict -wle "my $f = { }; print 'ok' if @{ $f->{foo} }"
  Can't use an undefined value as an ARRAY reference at -e line 1.

Any wisdom regarding this would be welcome.

Yves

Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About