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.
|
---|
Replies are listed 'Best First'. | |
---|---|
Re^5: What does my @x= (undef)x7; do?
by AnomalousMonk (Archbishop) on Nov 09, 2015 at 18:34 UTC | |
Re^5: What does my @x= (undef)x7; do?
by BrowserUk (Patriarch) on Nov 10, 2015 at 04:55 UTC | |
by betterworld (Curate) on Nov 10, 2015 at 09:04 UTC | |
by BrowserUk (Patriarch) on Nov 10, 2015 at 11:34 UTC | |
by AnomalousMonk (Archbishop) on Nov 10, 2015 at 17:46 UTC | |
by BrowserUk (Patriarch) on Nov 10, 2015 at 17:58 UTC | |
by AnomalousMonk (Archbishop) on Nov 10, 2015 at 18:42 UTC | |
by BrowserUk (Patriarch) on Nov 10, 2015 at 19:37 UTC | |
|
In Section
Seekers of Perl Wisdom