Why isn't it auto-vivifying the storage? A 'poor' workaround is to manually assign 'undef' to each member in local_vars, i.e. This works:>perl -e ' use Data::Alias; my @local_vars; $#local_vars=5; alias my ($a, $b, $c) = @local_vars; $b=1; ' Modification of a read-only value attempted at -e line 5.
--- The above is more a question of 'why', as in trying to move forward, I found a _SLIGHTLY_ more reasonable workaround, but still not ideal...> perl -e ' use Data::Alias; my @local_vars=(undef,undef,undef); alias my ($a, $b, $c) = @local_vars; $b=1; '
The above works -- though I'd prefer to just take the length of the newly instantiated array enclosed by parens rather than having to do an assignment, which will (I believe?) create a run-time execution charge for the assignment. While it's not a big deal in the 'setup' at the top of the package, in the subroutines where it is used, that means I have to do the same assignment, each time I want to use Data::Alias.#!/usr/bin/perl -w package mypackage; use Data::Alias; our $local_varnames = 'qw( $a $b $c )'; our $len_local_varnames; { # prefer-> $len_local_varnames= wantarray (eval"($local_varnames)"); +OR # $len_local_varnames= 0+eval"($local_varnames)"; $len_local_varname = eval "local @_=($local_varnames)"; } printf "len_local_varnames=%d\n",$len_local_varnames;
I.e. This doesn't work:
To make the above work, I throw out the "$#localspace = $len_local_varnames-1;" line, and change the one above it to:#!/usr/bin/perl -w package mypackage; use Data::Alias; our $local_varnames = 'qw( $a $b $c )'; our $len_local_varnames; { $len_local_varnames= eval "local @_=($local_varnames)" } sub mysub { my $this=shift; my @localspace; $#localspace = $len_local_varnames-1; # <-doesn't work to pre-all +ocate array $this->{localvars}= \@localspace; alias my ($a, $b, $c) = @localspace; ($a, $b, $c) = (1, 2, 3); # (fails because no space a +lloc'ed to localspace) #both print wrong/bad values printf "lsp[1]=%d\n",$localspace[1]; printf "b=%d\n",$this->{localvars}->[1]; } &mysub;
...thus requiring my assignment to be done in each package sub where I want to access my local variables; somewhat inefficient to say the least. The next problem I have is related, but I feel I really need to split it, or this post/question will get really 'unclear', so please forgive me for splitting apart a related problem, but when I tried putting it all in one post, it got very confusing (and my writing is often confusing enough for other people, when I think it is clear -- goddess forbid I post something with hope for understanding that _I_ think is confusing...my @localspace = eval "local @_=($local_varnames)";
So for this question -- the real question is how to get the array to "pre-allocate" without assigning to each member. I thought assigning to the last index would do it, but apparently not. Most frustrating. Would this be considered a bug in 'Data::Alias'?, Or is it likely, due to some perl internals, that they aren't going to be able to fix it without themselves, actually initializing each element (which might look cleaner, but would still be as inefficient, I think...
Sorry for the arcane questions...but if it was easy, I wouldn't need the wisdom of perlmonks! ;-)
In reply to Error using Data::Alias (how to allocate array w/o filling it in?) and help avoiding 'constant' len? by perl-diddler
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |