\$array[0] gives you an alias to the value stored in that slot of the array, which is half of what you need. To make $array[1] an alias to that value, you need to get a pointer to the slot $array[1], which isn't a concept that is exposed to Perl scripts. It is almost like you want \\$array[1] except that that gives you a reference to a temporary scalar that points to the same value rather than a reference to the array slot (Perl doesn't support references to array slots).

Lexical::Alias finds the extra layer of indirection by searching through internal structures (where the lexical variables are kept track of) until it finds a lexical that points to the same data that you passed it a reference to.

There are other modules that can do this for arrays and hashes. So you can write XS code that you pass a reference to the array, the index to be made an alias, and a reference to (or alias of) the value to be aliased (for example).

I know that demerphq's Data::Dump::Streamer needed this same functionality so I went to look up which module it used. But it looks like demerphq decided to roll his own replacements before he released the module (at least under that name). But you can just install Data::Dump::Streamer and use its alias functions:

alias_av(@Array,$index,$var); uses prototypes to get a reference to the array you pass it and uses the fact that function parameters are aliases to the corresponding (scalar) arguments to get a reference to $var. Then it sets $Array[$index] to be an alias to $var.

- tye        


In reply to Re: Aliasing values, not variables (indirecter) by tye
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.