.\" Automatically generated by Pod::Man 4.07 (Pod::Simple 3.32) .\" .\" Standard preamble: .\" ======================================================================== .de Sp \" Vertical space (when we can't use .PP) .if t .sp .5v .if n .sp .. .de Vb \" Begin verbatim text .ft CW .nf .ne \\$1 .. .de Ve \" End verbatim text .ft R .fi .. .\" Set up some character translations and predefined strings. \*(-- will .\" give an unbreakable dash, \*(PI will give pi, \*(L" will give a left .\" double quote, and \*(R" will give a right double quote. \*(C+ will .\" give a nicer C++. Capital omega is used to do unbreakable dashes and .\" therefore won't be available. \*(C` and \*(C' expand to `' in nroff, .\" nothing in troff, for use with C<>. .tr \(*W- .ds C+ C\v'-.1v'\h'-1p'\s-2+\h'-1p'+\s0\v'.1v'\h'-1p' .ie n \{\ . ds -- \(*W- . ds PI pi . if (\n(.H=4u)&(1m=24u) .ds -- \(*W\h'-12u'\(*W\h'-12u'-\" diablo 10 pitch . if (\n(.H=4u)&(1m=20u) .ds -- \(*W\h'-12u'\(*W\h'-8u'-\" diablo 12 pitch . ds L" "" . ds R" "" . ds C` "" . ds C' "" 'br\} .el\{\ . ds -- \|\(em\| . ds PI \(*p . ds L" `` . ds R" '' . ds C` . ds C' 'br\} .\" .\" Escape single quotes in literal strings from groff's Unicode transform. .ie \n(.g .ds Aq \(aq .el .ds Aq ' .\" .\" If the F register is >0, we'll generate index entries on stderr for .\" titles (.TH), headers (.SH), subsections (.SS), items (.Ip), and index .\" entries marked with X<> in POD. Of course, you'll have to process the .\" output yourself in some meaningful fashion. .\" .\" Avoid warning from groff about undefined register 'F'. .de IX .. .if !\nF .nr F 0 .if \nF>0 \{\ . de IX . tm Index:\\$1\t\\n%\t"\\$2" .. . if !\nF==2 \{\ . nr % 0 . nr F 2 . \} .\} .\" ======================================================================== .\" .IX Title "Alias 3pm" .TH Alias 3pm "2015-11-21" "perl v5.24.1" "User Contributed Perl Documentation" .\" For nroff, turn off justification. Always turn off hyphenation; it makes .\" way too many mistakes in technical documents. .if n .ad l .nh .SH "NAME" alias \- declare symbolic aliases for perl data .PP attr \- auto\-declare hash attributes for convenient access .PP const \- define compile\-time scalar constants .SH "SYNOPSIS" .IX Header "SYNOPSIS" .Vb 8 \& use Alias qw(alias const attr); \& alias TEN => $ten, Ten => \e$ten, Ten => \e&ten, \& Ten => \e@ten, Ten => \e%ten, TeN => \e*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) \& } .Ve .SH "DESCRIPTION" .IX Header "DESCRIPTION" Provides general mechanisms for aliasing perl data for convenient access. .PP 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 \f(CW\*(C`[1,2,3]\*(C'\fR with a name of \&\*(L"foo\*(R" will be made available as \f(CW@foo\fR, not \f(CW$foo\fR. .PP The exception to this rule is the default behavior of the \f(CW\*(C`attr\*(C'\fR function, which will not dereference values which are blessed references (aka objects). See \f(CW$Alias::Deref\fR for how to change this default behavior. .SS "Functions" .IX Subsection "Functions" .IP "alias" 4 .IX Item "alias" Given a list of name => value pairs, declares aliases in the \f(CW\*(C`caller\*(C'\fRs namespace. If the value supplied is a reference, the alias is created for the underlying value instead of the reference itself (there is no need to use this module to alias references\*(--they are automatically \*(L"aliased\*(R" on assignment). This allows the user to alias most of the basic types. .Sp If the value supplied is a scalar compile-time constant, the aliases become read-only. Any attempt to write to them will fail with a run time error. .Sp Aliases can be dynamically scoped by pre-declaring the target variable as \&\f(CW\*(C`local\*(C'\fR. Using \f(CW\*(C`attr\*(C'\fR for this purpose is more convenient, and recommended. .IP "attr" 4 .IX 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: .Sp .Vb 1 \& alias %{$_[0]} .Ve .Sp but, in addition, also localizes the aliases, and does not dereference objects. Dereferencing of objects can be forced by setting the \f(CW\*(C`Deref\*(C'\fR option. See \f(CW$Alias::Deref\fR. .Sp This can be used for convenient access to hash values and hash-based object attributes. .Sp Note that this makes available the semantics of \f(CW\*(C`local\*(C'\fR subroutines and methods. That makes for some nifty possibilities. We could make truly private methods by putting anonymous subs within an object. These subs would be available within methods where we use \f(CW\*(C`attr\*(C'\fR, and will not be visible to the outside world as normal methods. We could forbid recursion in methods by always putting an empty sub in the object hash with the same key as the method name. This would be useful where a method has to run code from other modules, but cannot be certain whether that module will call it back again. .Sp The default behavior is to create aliases for all the entries in the hash, in the callers namespace. This can be controlled by setting a few options. See \*(L"Configuration Variables\*(R" for details. .IP "const" 4 .IX Item "const" This is simply a function alias for \f(CW\*(C`alias\*(C'\fR, described above. Provided on demand at \f(CW\*(C`use\*(C'\fR time, since it reads better for constant declarations. Note that hashes and arrays cannot be so \f(CW\*(C`const\*(C'\fRrained. .SS "Configuration Variables" .IX Subsection "Configuration Variables" The following configuration variables can be used to control the behavior of the \f(CW\*(C`attr\*(C'\fR function. They are typically set after the \f(CW\*(C`use Alias;\*(C'\fR statement. Another typical usage is to \f(CW\*(C`local\*(C'\fRize them in a block so that their values are only effective within that block. .ie n .IP "$Alias::KeyFilter" 4 .el .IP "\f(CW$Alias::KeyFilter\fR" 4 .IX Item "$Alias::KeyFilter" Specifies the key prefix used for determining which hash entries will be interned by \f(CW\*(C`attr\*(C'\fR. Can be a \s-1CODE\s0 reference, in which case it will be called with the key, and the boolean return value will determine if that hash entry is a candidate attribute. .ie n .IP "$Alias::AttrPrefix" 4 .el .IP "\f(CW$Alias::AttrPrefix\fR" 4 .IX Item "$Alias::AttrPrefix" Specifies a prefix to prepend to the names of localized attributes created by \f(CW\*(C`attr\*(C'\fR. Can be a \s-1CODE\s0 reference, in which case it will be called with the key, and the result will determine the full name of the attribute. The value can have embedded package delimiters (\*(L"::\*(R" or \*(L"'\*(R"), which cause the attributes to be interned in that namespace instead of the \f(CW\*(C`caller\*(C'\fRs own namespace. For example, setting it to \*(L"main::\*(R" makes \f(CW\*(C`use strict \*(Aqvars\*(Aq;\*(C'\fR somewhat more palatable (since we can refer to the attributes as \f(CW$::foo\fR, etc., without actually declaring the attributes). .ie n .IP "$Alias::Deref" 4 .el .IP "\f(CW$Alias::Deref\fR" 4 .IX Item "$Alias::Deref" Controls the implicit dereferencing behavior of \f(CW\*(C`attr\*(C'\fR. If it is set to "" or 0, \f(CW\*(C`attr\*(C'\fR will not dereference blessed references. If it is a true value (anything but "\*(L", 0, or a \s-1CODE\s0 reference), all references will be made available as their dereferenced types, including values that may be objects. The default is \*(R"". .Sp This option can be used as a filter if it is set to a \s-1CODE\s0 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. .SS "Exports" .IX Subsection "Exports" .IP "alias" 4 .IX Item "alias" .PD 0 .IP "attr" 4 .IX Item "attr" .PD .SH "EXAMPLES" .IX Header "EXAMPLES" Run these code snippets and observe the results to become more familiar with the features of this module. .PP .Vb 5 \& use Alias qw(alias const attr); \& $ten = 10; \& alias TEN => $ten, Ten => \e$ten, Ten => \e&ten, \& Ten => \e@ten, Ten => \e%ten; \& alias TeN => \e*ten; # same as *TeN = *ten \& \& # aliasing basic types \& $ten = 20; \& print "$TEN|$Ten|$ten\en"; # should print "20|20|20" \& sub ten { print "10\en"; } \& @ten = (1..10); \& %ten = (a..j); \& &Ten; # should print "10" \& print @Ten, "|", %Ten, "\en"; \& \& # 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] = \*(Aqzzz\*(Aq; \& # prints "abzzzd|abzzzd|abzzzd" \& print @$tmp, "|", @DYNAMIC, "|", @PERM, "\en"; \& \& @DYNAMIC = qw(p q r); \& # prints "pqr|pqr|pqr" \& print @$tmp, "|", @DYNAMIC, "|", @PERM, "\en"; \& } \& \& # prints "mno|pqr" \& print @DYNAMIC, "|", @PERM, "\en"; \& \& # named closures \& my($lex) = \*(Aqabcd\*(Aq; \& $closure = sub { print $lex, "\en" }; \& alias NAMEDCLOSURE => \e&$closure; \& NAMEDCLOSURE(); # prints "abcd" \& $lex = \*(Aqpqrs\*(Aq; \& 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 \*(Aq|\*(Aq, $foo, @bar, %buz, $s\->privmeth, "\en"; \& } \& \& $foo = 6; \& @bar = (7,8); \& %buz = (b => 9); \& Foo\->new\->easymeth; # this will not recurse endlessly \& \& # prints "6|7|8|b|9|" \& print join \*(Aq|\*(Aq, $foo, @bar, %buz, "\en"; \& \& # this should fail at run\-time \& eval { Foo\->new\->privmeth }; \& print $@ if $@; .Ve .SH "NOTES" .IX Header "NOTES" It is worth repeating that the aliases created by \f(CW\*(C`alias\*(C'\fR and \f(CW\*(C`const\*(C'\fR will be created in the \f(CW\*(C`caller\*(C'\fRs namespace (we can use the \f(CW\*(C`AttrPrefix\*(C'\fR option to specify a different namespace for \f(CW\*(C`attr\*(C'\fR). If that namespace happens to be \&\f(CW\*(C`local\*(C'\fRized, the aliases created will be local to that block. \f(CW\*(C`attr\*(C'\fR localizes the aliases for us. .PP Remember that references will be available as their dereferenced types. .PP Aliases cannot be lexical, since, by neccessity, they live on the symbol table. .PP Lexicals can be aliased. Note that this provides a means of reversing the action of anonymous type generators \f(CW\*(C`\e\*(C'\fR, \f(CW\*(C`[]\*(C'\fR and \f(CW\*(C`{}\*(C'\fR. This allows us to anonymously construct data or code and give it a symbol-table presence when we choose. .PP Any occurrence of \f(CW\*(C`::\*(C'\fR or \f(CW\*(C`\*(Aq\*(C'\fR in names will be treated as package qualifiers, and the value will be interned in that namespace. .PP 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. .PP We can dynamically make subroutines and named closures with this scheme. .PP It is possible to alias packages, but that might be construed as abuse. .PP Using this module will dramatically reduce noise characters in object-oriented perl code. .SH "BUGS" .IX Header "BUGS" \&\f(CW\*(C`use strict \*(Aqvars\*(Aq;\*(C'\fR is not very usable, since we \fBdepend\fR so much on the symbol table. You can declare the attributes with \f(CW\*(C`use vars\*(C'\fR to avoid warnings. Setting \f(CW$Alias::AttrPrefix\fR to \*(L"main::\*(R" is one way to avoid \f(CW\*(C`use vars\*(C'\fR and frustration. .PP Tied variables cannot be aliased properly, yet. .SH "VERSION" .IX Header "VERSION" Version 2.32 30 Apr 1999 .SH "AUTHOR" .IX Header "AUTHOR" Gurusamy Sarathy gsar@umich.edu .PP 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. .SH "SEE ALSO" .IX Header "SEE ALSO" \&\fIperl\fR\|(1)