Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: added example of read-only array growth to first code snippet

...

Code Block
langperl
my @array = (1, 2, 3);            # array initialized
print "Array size is $#array\n";  # 2
$array[5] = 0;                    # array grows so that reference is valid
print "Array size is $#array\n";  # 5
my $value = $array[7];            # array unchanged + uninitialized value warning
$value = $array[-7];              # array unchanged + uninitialized value warning
if (exists $array[9]) {           # false, array unchanged
  print "That's a big array.\n";
}
print "Array size is $#array\n";  # still 5
$value = $array[10][0];           # reading a value in list context grows array
print "Array size is $#array\n";  # 10!

This automatic growth occurs only if the index provided is positive and the array value is being written, not read, and not passed to a testing function like exists() or defined().

...