http://qs1969.pair.com?node_id=1147267


in reply to Re^3: What does my @x= (undef)x7; do?
in thread What does my @x= (undef)x7; do?

Perl code would treat the two the as the same, but user-written XS code makes assumptions.

When I read this, I felt an urge to find Perl code that behaves differently for these arrays :)

Turns out that what this API function is doing would require explicit undefs even if the function was written in Perl. This is a subroutine that modifies its input parameters. In Perl you might write it like this:

use Data::Dumper; my @a; @a = (undef) x 5; my @b; $#b = 4; sub modify { $_ = 5 for @_; } modify(@a); print Dumper \@a; modify(@b); print Dumper \@b;

Here the subroutine "modify" behaves similar to GetVolumeInformation as it modifies the scalars in the array passed to it.

So the arrays @a and @b should be identical. However, modify() will replace @a with five times 5, but it cannot write to @b:

$VAR1 = [ 5, 5, 5, 5, 5 ]; Modification of a read-only value attempted at script.pl line 12.

Update: Since this seems to depend on the version: I am using Perl 5.20.2 on Debian.