in reply to Re: push undefined and vivification
in thread push undefined and vivification

in use strict it's right. but without, the code:
use Data::Dumper; @a=('bar'); print Dumper(\@a); push @{$a[0]}, "foo"; print Dumper(\@a); print Dumper(\@bar);
actually append 'foo' to @bar:
$VAR1 = [ 'bar' ]; $VAR1 = [ 'bar' ]; $VAR1 = [ 'foo' ];

in that case, so, perl do one of three things:

  • append to the array named by the value of $a[0], or
  • append to the array-ref in $a[0], or
  • create an anonymous array and put the reference in $a[0]
    in use strict; the behavior of push is predictable, but without -- and using a string in $a[0] -- the behavior is not predictable.

    this may be a good reason to use strict, without perl may use the @{$something} as array-ref or variable name... a bit confusing, doesn't it?

  • Replies are listed 'Best First'.
    Re: Re: Re: push undefined and vivification
    by Anonymous Monk on Jan 18, 2004 at 12:53 UTC
      in use strict; the behavior of push is predictable, but without -- and using a string in $a[0] -- the behavior is not predictable
      Wrong. The behaviour is perfectly predictable. Observe
      use Data::Dumper; @a = 'bar'; @bar = 6; warn push @{$a[0]}, 7; warn Dumper( \@a, \@bar ); warn push @{"$a[0]"}, 8; warn Dumper( \@a, \@bar ); warn push @{"bar"}, 9; warn Dumper( \@a, \@bar ); undef $a[0]; warn push @{$a[0]}, 11; warn Dumper( \@a, \@bar ); __END__ 2 at - line 4. $VAR1 = [ 'bar' ]; $VAR2 = [ 6, 7 ]; 3 at - line 7. $VAR1 = [ 'bar' ]; $VAR2 = [ 6, 7, 8 ]; 4 at - line 10. $VAR1 = [ 'bar' ]; $VAR2 = [ 6, 7, 8, 9 ]; 1 at - line 13. $VAR1 = [ [ 11 ] ]; $VAR2 = [ 6, 7, 8, 9 ];

      without perl may use the @{$something} as array-ref or variable name... a bit confusing, doesn't it?
      Why? Observe
      @a = 'bar'; @bar = 6; warn 'PUSHING ONTO @BAR' if defined $a[0]; push @{$a[0]}, 'bar'; undef $a[0]; warn 'PUSHING ONTO $a[0]' if not defined $a[0]; push @{$a[0]}, 'bar'; use Data::Dumper; warn Dumper( \@a, \@bar ); __END__ PUSHING ONTO @BAR at - line 4. PUSHING ONTO $a[0] at - line 10. $VAR1 = [ [ 'bar' ] ]; $VAR2 = [ 6, 'bar' ];