ActivePerl Documentation
|
NAMEalias - declare symbolic aliases for perl data attr - auto-declare hash attributes for convenient access const - define compile-time scalar constants
SUPPORTED PLATFORMS
SYNOPSIS
use Alias qw(alias const attr);
alias TEN => $ten, Ten => \$ten, Ten => \&ten,
Ten => \@ten, Ten => \%ten, TeN => \*ten;
{
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;
# local @::_bar is now available, ($foo, $::foo are not)
}
sub c_method {
local $Alias::KeyFilter = sub { $_ = shift; return (/^_/ ? 1 : 0) };
local $Alias::AttrPrefix = sub {
$_ = shift;
s/^_(.+)$/main::$1/;
return $_
};
my $s = attr shift;
# local @::bar is now available, ($foo, $::foo are not)
}
DESCRIPTIONProvides 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 The exception to this rule is the default behavior of the
Functions
Configuration VariablesThe following configuration variables can be used to control the behavior of
the
Exports
EXAMPLESRun these code snippets and observe the results to become more familiar with the features of this module.
use Alias qw(alias const attr);
$ten = 10;
alias TEN => $ten, Ten => \$ten, Ten => \&ten,
Ten => \@ten, Ten => \%ten;
alias TeN => \*ten; # same as *TeN = *ten
# aliasing basic types
$ten = 20;
print "$TEN|$Ten|$ten\n"; # should print "20|20|20"
sub ten { print "10\n"; }
@ten = (1..10);
%ten = (a..j);
&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 $@;
NOTESIt is worth repeating that the aliases created by 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 Any occurrence of 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.
BUGS
Tied variables cannot be aliased properly, yet.
VERSIONVersion 2.32 30 Apr 1999
AUTHORGurusamy Sarathy gsar@umich.edu Copyright (c) 1995-99 Gurusamy Sarathy. All rights reserved. This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
SEE ALSO
|