Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine

How to force one variable to be an alias to another?

by jdporter (Canon)
on Nov 26, 2006 at 16:23 UTC ( #586121=categorized question: print w/replies, xml ) Need Help??
Contributed by jdporter on Nov 26, 2006 at 16:23 UTC
Q&A  > references


For example,

my $x; my $y; make_alias( $x, $y ); $x = 5; print $y; # prints 5

Answer: How to force one variable to be an alias to another?
contributed by jdporter

One way is to use the Lexical::Alias module:

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.

Answer: How to force one variable to be an alias to another?
contributed by LanX

Data::Alias is now preferred over Lexical::Alias, as it works with globals (package variables, array elements, hash values, etc.) as well as lexicals.

Please (register and) log in if you wish to add an answer

  • 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, 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, 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.
  • Log In?

    What's my password?
    Create A New User
    and the rats come out to play...

    How do I use this? | Other CB clients
    Other Users?
    Others having an uproarious good time at the Monastery: (10)
    As of 2017-02-22 16:15 GMT
    Find Nodes?
      Voting Booth?
      Before electricity was invented, what was the Electric Eel called?

      Results (331 votes). Check out past polls.