Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

\$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":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others romping around the Monastery: (3)
As of 2024-04-26 02:22 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found