use Lexical::Alias;
my( $this, $that );
alias $that, $this;
# $this is now an alias for $that.
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:
alias @x, @y;
alias %x, %y;
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.
use Tie::Alias;
my( $this, $that );
tie $this, 'Tie::Alias', \$that;
# $this is now an alias for $that.
|