I'm working on a module named Data::Alias to provide a comprehensive set of aliasing operations. For those who don't know, aliasing is getting two expressions to identify exactly the same thing. So while setting $x to $y will make $x and $y the same, aliasing $x to $y will make \$x and \$y the same (Diagram).

Its interface is simple: alias LIST or alias BLOCK. The function itself is essentially a no-op, but the entire argument list is evaluated with altered semantics where aliasing occurs wheever copying would normally be done. This is achieved by diddling the optree, not by using a source filter.

This means you can use natural perl syntax to do all sorts of aliasing:

alias my ($x, $y) = @_;Named aliases for sub arguments
alias @x = @{$self->{Items}};Alias for some referenced array
$x = alias [$y, $z];An anonymous array of aliases
(just like $x = sub{\@_}->($y, $z))
alias push @x, $y;Push an alias onto an array

Currently it supports aliasing assignment to variables, array/hash elements, dereferencing, and globs, including conditional assignment (&&=, ||=). Also push, unshift, splice, and anonymous array/hash constructors. do-blocks return aliases (in fact, they are lvalues). Finally you can return aliases from sub or eval, either implicitly by placing the sub/eval inside alias, or explicitly by using alias return LIST;

UPDATE: Putting a conditional expr ( ? :) on the left-side of assignment didn't work. I've added support for it and uploaded a new snapshot.

So the big question is... have I overlooked anything? Are there more operations that deserve to receive aliasing semantics inside alias?

A minor second issue what to do with aliasing elements of tied arrays and hashes. Currently I just give an error, but I considered checking for special methods like ALIAS_STORE, ALIAS_PUSH, etc. Would this be of any utility at all?

And any other sort of feedback is welcome too of course :-)


In reply to RFC: Is there more to alias? by xmath

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • 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, details, 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, summary, 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.