Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Re: Aliasing values, not variables (indirecter)

by tye (Sage)
on Jul 18, 2004 at 17:42 UTC ( [id://375407]=note: print w/replies, xml ) Need Help??


in reply to Aliasing values, not variables

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

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://375407]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others lurking in the Monastery: (8)
As of 2024-04-19 07:42 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found