Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Array element automatically set to undef won't change when aliased?

Tags:

perl

Relating to this question, what's the difference between a manually set to undef list element and one that was set by Perl when growing the size of that list by assigning to a bigger index than the list size, when being aliased? For example, considering this code:

@a = (undef, 0);
@b = ();
$b[1] = 0;

print Dumper(@a), "\n", Dumper(@b);

It prints (as expected, I think):

$VAR1 = undef;
$VAR2 = 0;

$VAR1 = undef;
$VAR2 = 0;

So, aren't the arrays the same? Apparently not:

sub change { $_[0] = 1 }
change(@a); change(@b);
print Dumper(@a), "\n", Dumper(@b);

Which prints:

$VAR1 = 1;
$VAR2 = 0;

$VAR1 = undef;
$VAR2 = 0;
like image 359
sidyll Avatar asked Aug 06 '13 21:08

sidyll


2 Answers

You found a fascinating edge case.

When you explicitly set an element, it first springs into existence. If an array is extended so that multiple indices fall into range of this array, no scalars are implicitly initialized at these positions. Example:

my @array;
$array[2] = undef; # this extends the array
# now elements 0–2 report as `undef`, but only #2 was initalized

When we ask whether these elements exist, we get:

say "index $_ ", exists $array[$_] ? "exists" : "doesn't exist" for 0 .. 4;

Output:

index 0 doesn't exist
index 1 doesn't exist
index 2 exists
index 3 doesn't exist
index 4 doesn't exist

This optimization saves you from allocating unused scalars at these positions; the array access code just returns an undef when there isn't anything there stating otherwise.

Now this squares badly with function calls. When a subroutine is called, a flat list of scalars is put on the stack, which is then accessible as @_. No copying takes place here, so this is call by alias. Now when the $_[0] element is accessed in your sub, there is no scalar here, so it creates a new one in @_:

sub crazy {
  say 1*exists $_[0];
  $_[0] = 1;
  say 1*exists $_[0];
}

my @array; $array[2] = 0;
crazy @array;

say 1*exists $array[0];

Output:

0
1
0

Internally, a scalar is a pointer to an SV struct. These pointers are copied onto the stack, so this makes actual modification of the original @array impossible.

like image 156
amon Avatar answered Nov 15 '22 20:11

amon


One difference is that exists $a[0] is true and exists $b[0] is false.

like image 38
mob Avatar answered Nov 15 '22 19:11

mob