Come for the quick hacks, stay for the epiphanies. | |
PerlMonks |
Re: How to force one variable to be an alias to another?by jdporter (Paladin) |
on Nov 26, 2006 at 21:33 UTC ( [id://586160]=note: print w/replies, xml ) | Need Help?? |
One way is to use the Lexical::Alias module: Here, $this is made to be an alias of $that. Whatever $this contained prior to the alias call (e.g. the 2 in the above example) is released, much the same as if $this had simply gone out of scope. (That is, its ref count is decremented.) Similarly, the reference count of whatever $that contains is incremented by this operation. You can also create aliases for array and hash variables this way:
Note that this technique specifically only works for lexical variables; it does not work for package variables or other globals, such as array elements and hash values. Another, similar approach is to use Tie::Alias. It has the advantage that it's pure Perl, but it's both slower and (currently) only works for scalars.
In Section
Seekers of Perl Wisdom
|
|