Data-Alias
view release on metacpan or search on metacpan
lib/Data/Alias.pm view on Meta::CPAN
alias $x = $y; # alias $x to $y
alias @x = @y; # alias @x to @y
alias $x[0] = $y; # similar for array and hash elements
alias push @x, $y; # push alias to $y onto @x
$x = alias [ $y, $z ]; # construct array of aliases
alias my ($x, $y) = @_; # named aliases to arguments
alias { ($x, $y) = ($y, $x) }; # swap $x and $y
alias { my @t = @x; @x = @y; @y = @t }; # swap @x and @y
use Data::Alias qw/ alias copy /;
alias { copy $x = $y }; # force copying inside alias-BLOCK
use Data::Alias qw/ deref /;
my @refs = (\$x, \@y, \%z);
foo(deref @refs) # same as foo($x, @y, %z)
=head1 DESCRIPTION
Aliasing is the phenomenon where two different expressions actually refer to
the same thing. Modifying one will modify the other, and if you take a
reference to both, the two values are the same.
Aliasing occurs in Perl for example in for-loops and sub-calls:
for $var ($x) {
# here $var is an alias to $x
}
foo($y);
sub foo {
# here $_[0] is an alias to $y
}
Data::Alias is a module that allows you to apply "aliasing semantics" to a
section of code, causing aliases to be made wherever Perl would normally make
copies instead. You can use this to improve efficiency and readability, when
compared to using references.
The exact details of aliasing semantics are below under L</DETAILS>.
Perl 5.22 added some support for aliasing to the Perl core. It has a
different syntax, and a different set of operations, from that supplied by
this module; see L<perlref/Assigning to References>. The core's aliasing
facilities are implemented more robustly than this module and are better
supported. If you can rely on having a sufficiently recent Perl version,
you should prefer to use the core facility rather than use this module.
If you are already using this module and are now using a sufficiently
recent Perl, you should attempt to migrate to the core facility.
=head1 SYNTAX
=head2 alias I<EXPR> | alias I<BLOCK>
Exported by default.
Enables aliasing semantics within the expression or block. Returns an alias
to the expression, or the block's return value.
C<alias> is context-transparent, meaning that whichever context it is placed in
(list, scalar, void), the expression/block is evaluated in the same context.
=head2 copy I<EXPR> | copy I<BLOCK>
Restores normal (copying) semantics within the expression or block, and
makes a copy of the result value (unless in void context).
Like C<alias>, C<copy> is context-transparent.
=head2 deref I<LIST>
Accepts a list of references to scalars, arrays, or hashes. Applies the
applicable dereferencing operator to each. This means that:
deref $scalarref, $arrayref, $hashref
behaves like:
$$scalarref, @$arrayref, %$hashref
Where an array or hash reference is given, the returned list does not
include the array or hash as an lvalue; the array/hash is expanded and
the list includes its elements. Scalars, including the elements of an
array/hash, I<are> treated as lvalues, and can be enreferenced using
the C<\> operator or aliased to using the C<alias> operator. This is
slightly different from what you'd get using the built-in dereference
operators: C<@$arrayref> references the array as an lvalue, so C<\>
or C<alias> can operate on the array itself rather than just its elements.
=head1 EXAMPLES
A common usage of aliasing is to make an abbreviation for an expression, to
avoid having to repeat that (possibly verbose or ugly) expression over and
over:
alias my $fi = $self->{FrobnitzIndex};
$fi = $fi > 0 ? $fi - $adj : $fi + $adj;
sub rc4 {
alias my ($i, $j, $S) = @_;
my $a = $S->[($i += 1) &= 255];
my $b = $S->[($j += $S->[$i]) &= 255];
$S->[(($S->[$j] = $a) + ($S->[$i] = $b)) & 255]
}
In the second example, the rc4 function updates its first two arguments (two
state values) in addition to returning a value.
Aliasing can also be used to avoid copying big strings. This example would
work fine without C<alias> but would be much slower when passed a big string:
sub middlesection ($) {
alias my $s = shift;
substr $s, length($s)/4, length($s)/2
}
You can also apply aliasing semantics to an entire block. Here this is used to
swap two arrays in O(1) time:
alias {
my @temp = @x;
@x = @y;
@y = @temp;
};
The C<copy> function is typically used to temporarily reinstate normal
semantics, but can also be used to explicitly copy a value when perl would
normally not do so:
( run in 2.283 seconds using v1.01-cache-2.11-cpan-39bf76dae61 )