Lexical::Alias is not suited for this task. Try Array::RefElem instead:

use Array::RefElem 'av_store'; my @array; av_store @array, 1, $array[0]; $array[1] = 2; print "@array";

If you're bent on using Lexical::Alias, you can try the following trick for your particular situation, though it's of little use for generic array element aliasing. It's an answer to the first question from How's your Perl?, adapted to Lexical::Alias.

use Lexical::Alias 'alias_a'; my @array; { my $t; alias_a @{sub {\@_}->($t, $t)}, @array; }

Warning: code untested!. I don't have my laptop which has my custom Perl install with Lexical::Alias and Array::RefElem. Particularly the second example might not work as advertised.

update: a monk confirms that code works on CB


In reply to Re: Aliasing values, not variables by calin
in thread Aliasing values, not variables by nothingmuch

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.