Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

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


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 vrk

Perl 5.22 introduced the refalias feature. The simplest solution to the example is

use v5.22; use warnings; use feature 'refalias'; # Silence warnings if desired: no warnings 'experimental::refaliasing'; my ($x, $y); \$x = \$y; # alias here $x = 5; say $y; # prints 5
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.

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

Caution re above: see also [Possible Problems in [id://586121] posted as SOPW (based on 1 line of advice from a "wiser head."

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?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and all is quiet...

    How do I use this? | Other CB clients
    Other Users?
    Others drinking their drinks and smoking their pipes about the Monastery: (6)
    As of 2017-04-23 10:12 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?
      I'm a fool:











      Results (430 votes). Check out past polls.