Alias
view release on metacpan or search on metacpan
&Ten; # should print "10"
print @Ten, "|", %Ten, "\n";
# this will fail at run time
const _TEN_ => 10;
eval { $_TEN_ = 20 };
print $@ if $@;
# dynamically scoped aliases
@DYNAMIC = qw(m n o);
{
my $tmp = [ qw(a b c d) ];
local @DYNAMIC;
alias DYNAMIC => $tmp, PERM => $tmp;
$DYNAMIC[2] = 'zzz';
# prints "abzzzd|abzzzd|abzzzd"
print @$tmp, "|", @DYNAMIC, "|", @PERM, "\n";
@DYNAMIC = qw(p q r);
# prints "pqr|pqr|pqr"
print @$tmp, "|", @DYNAMIC, "|", @PERM, "\n";
}
# prints "mno|pqr"
print @DYNAMIC, "|", @PERM, "\n";
# named closures
my($lex) = 'abcd';
$closure = sub { print $lex, "\n" };
alias NAMEDCLOSURE => \&$closure;
NAMEDCLOSURE(); # prints "abcd"
$lex = 'pqrs';
NAMEDCLOSURE(); # prints "pqrs"
# hash/object attributes
package Foo;
use Alias;
sub new {
bless
{ foo => 1,
bar => [2,3],
buz => { a => 4},
privmeth => sub { "private" },
easymeth => sub { die "to recurse or to die, is the question" },
}, $_[0];
}
sub easymeth {
my $s = attr shift; # localizes $foo, @bar, %buz etc with values
eval { $s->easymeth }; # should fail
print $@ if $@;
# prints "1|2|3|a|4|private|"
print join '|', $foo, @bar, %buz, $s->privmeth, "\n";
}
$foo = 6;
@bar = (7,8);
%buz = (b => 9);
Foo->new->easymeth; # this will not recurse endlessly
# prints "6|7|8|b|9|"
print join '|', $foo, @bar, %buz, "\n";
# this should fail at run-time
eval { Foo->new->privmeth };
print $@ if $@;
=head1 NOTES
It is worth repeating that the aliases created by C<alias> and C<const> will
be created in the C<caller>s namespace (we can use the C<AttrPrefix> option to
specify a different namespace for C<attr>). If that namespace happens to be
C<local>ized, the aliases created will be local to that block. C<attr>
localizes the aliases for us.
Remember that references will be available as their dereferenced types.
Aliases cannot be lexical, since, by neccessity, they live on the
symbol table.
Lexicals can be aliased. Note that this provides a means of reversing the
action of anonymous type generators C<\>, C<[]> and C<{}>. This allows us
to anonymously construct data or code and give it a symbol-table presence
when we choose.
Any occurrence of C<::> or C<'> in names will be treated as package
qualifiers, and the value will be interned in that namespace.
Remember that aliases are very much like references, only we don't
have to dereference them as often. Which means we won't have to
pound on the dollars so much.
We can dynamically make subroutines and named closures with this scheme.
It is possible to alias packages, but that might be construed as
abuse.
Using this module will dramatically reduce noise characters in
object-oriented perl code.
=head1 BUGS
C<use strict 'vars';> is not very usable, since we B<depend> so much
on the symbol table. You can declare the attributes with C<use vars> to
avoid warnings. Setting C<$Alias::AttrPrefix> to "main::" is one way
to avoid C<use vars> and frustration.
Tied variables cannot be aliased properly, yet.
=head1 VERSION
Version 2.32 30 Apr 1999
=head1 AUTHOR
( run in 1.174 second using v1.01-cache-2.11-cpan-acf6aa7dc9e )