Alias
view release on metacpan or search on metacpan
{
local @Ten;
my $ten = [1..10];
alias Ten => $ten; # local @Ten
}
const pi => 3.14, ten => 10;
package Foo;
use Alias;
sub new { bless {foo => 1, _bar => [2, 3]}, $_[0] }
sub a_method {
my $s = attr shift;
# $foo, @_bar are now local aliases for
# $_[0]{foo}, @{$_[0]{_bar}} etc.
}
sub b_method {
local $Alias::KeyFilter = "_";
local $Alias::AttrPrefix = "main::";
my $s = attr shift;
=head1 DESCRIPTION
Provides general mechanisms for aliasing perl data for convenient access.
This module works by putting some values on the symbol table with
user-supplied names. Values that are references will get dereferenced into
their base types. This means that a value of C<[1,2,3]> with a name of
"foo" will be made available as C<@foo>, not C<$foo>.
The exception to this rule is the default behavior of the C<attr> function,
which will not dereference values which are blessed references (aka
objects). See L<$Alias::Deref> for how to change this default behavior.
=head2 Functions
=over 4
=item alias
Given a list of name => value pairs, declares aliases
in the C<caller>s namespace. If the value supplied is a reference, the
error.
Aliases can be dynamically scoped by pre-declaring the target variable as
C<local>. Using C<attr> for this purpose is more convenient, and
recommended.
=item attr
Given a hash reference, aliases the values of the hash to the names that
correspond to the keys. It always returns the supplied value. The aliases
are local to the enclosing block. If any of the values are unblessed
references, they are available as their dereferenced types. Thus the action
is similar to saying:
alias %{$_[0]}
but, in addition, also localizes the aliases, and does not dereference
objects. Dereferencing of objects can be forced by setting the C<Deref>
option. See L<$Alias::Deref>.
This can be used for convenient access to hash values and hash-based object
the key, and the result will determine the full name of the attribute. The
value can have embedded package delimiters ("::" or "'"), which cause the
attributes to be interned in that namespace instead of the C<caller>s own
namespace. For example, setting it to "main::" makes C<use strict 'vars';>
somewhat more palatable (since we can refer to the attributes as C<$::foo>,
etc., without actually declaring the attributes).
=item $Alias::Deref
Controls the implicit dereferencing behavior of C<attr>. If it is set to
"" or 0, C<attr> will not dereference blessed references. If it is a true
value (anything but "", 0, or a CODE reference), all references will be
made available as their dereferenced types, including values that may be
objects. The default is "".
This option can be used as a filter if it is set to a CODE reference, in
which case it will be called with the key and the value (whenever the value
happens to be a reference), and the boolean return value will determine if
that particular reference must be dereferenced.
$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
T(NAMEDCLOSURE() eq "pqrs");
package Foo;
# "in life", my moma always said, "you gotta pass those tests
# before you can be o' some use to yerself" :-)
*T = \&main::T;
use Alias;
sub new { bless { foo => 1,
bar => [2,3],
buz => { a => 4},
fuz => *easy,
privmeth => sub { "private" },
easymeth => sub { die "to recurse or to die, is the question" },
},
$_[0];
}
sub easy { "gulp" }
sub new {
my $s = { _foo => 1,
_bar => [2,3],
buz => { a => 4},
fuz => *easy,
_privmeth => sub { "private" },
_easymeth => sub { "recursion" },
};
$s->{_this} = $s;
return bless $s, $_[0];
}
sub easy { "gulp" }
sub s_easymeth {
my $s = attr shift; # localizes $s_foo, @s_bar, and %s_buz
T($s and ref($s) eq 'Bar');
print "# |$s_this|$s|\n";
T(defined($s_this) and $s_this eq $s);
T(not defined &fuz);
T(not defined &s_fuz);
return $r
};
$Alias::AttrPrefix = sub {
local $_ = shift; s/^_(.+)_$/$1/;
return "s_$_" if /meth$/;
return "main::s_$_"
};
$Alias::Deref = sub { my($n, $v) = @_; ; my $r = $n ne "_this_"; print "# |$n, $v, $r|\n"; return $r};
sub new {
my $s = bless { _foo_ => 1,
_bar_ => [2,3],
buz_ => { a => 4},
fuz_ => *easy,
_privmeth_ => sub { "private" },
_easymeth_ => sub { "recursion" },
},
$_[0];
$s->{_this_} = $s;
$s->{_other_} = $s;
return $s;
( run in 1.925 second using v1.01-cache-2.11-cpan-de7293f3b23 )