in reply to Error using Data::Alias (how to allocate array w/o filling it in?) and help avoiding 'constant' len?
The above is more a question of 'why'
$a, $b and $c are being aliased to The undef (PL_sv_undef) rather than an undef (a scalar that happens to be undefined).
$ perl -MDevel::Peek -e'Dump(undef); Dump(my $x);' SV = NULL(0x0) at 0x8168784 REFCNT = 2147483616 FLAGS = (READONLY) SV = NULL(0x0) at 0x817bc18 REFCNT = 1 FLAGS = (PADMY)
It uses less memory to initially map elements to an existing scalar than to create a bunch of undefined scalars.
The solution is to reverse the mapping.
alias my @array = my ($x, $y, $z);
If that syntax doesn't work ( Tested: It does work ), you can achieve the intended as follows:
sub map_scalars_onto_array(\@@) { my $array = shift; alias $array->[$_] = $_[0] for 0..$#_; } map_scalars_onto_array my @local_array, my ($x, $y, $z);
By the way, you can force the vivification of an array element by taking a reference to it.
$ perl -E'$#a=5; for (1..2) { say exists($a[3])?1:0; \$a[3]; }' 0 1
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Error using Data::Alias (how to allocate array w/o filling it in?) and help avoiding 'constant' len?
by perl-diddler (Chaplain) on Sep 23, 2010 at 16:47 UTC | |
by ikegami (Patriarch) on Sep 23, 2010 at 18:20 UTC | |
|
Re^2: Error using Data::Alias (how to allocate array w/o filling it in?) and help avoiding 'constant' len?
by perl-diddler (Chaplain) on Sep 23, 2010 at 17:47 UTC | |
by ikegami (Patriarch) on Sep 23, 2010 at 18:24 UTC |