Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

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

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


Description:

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!
  • 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
  • Outside of code tags, you may need to use entities for some characters:
            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?
    Username:
    Password:

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

    How do I use this? | Other CB clients
    Other Users?
    Others meditating upon the Monastery: (10)
    As of 2014-09-23 19:25 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      How do you remember the number of days in each month?











      Results (239 votes), past polls