I ran into a minor snag using Data::Alias -- I'm not sure if it's a bug, or a limitation that won't be easily fixable ...
>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.
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=(undef,undef,undef); alias my ($a, $b, $c) = @local_vars; $b=1; '
--- 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...
#!/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;
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.

I.e. This doesn't work:

#!/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;
To make the above work, I throw out the "$#localspace = $len_local_varnames-1;" line, and change the one above it to:
my @localspace = eval "local @_=($local_varnames)";
...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...

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

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.