home *** CD-ROM | disk | FTP | other *** search
/ Chip 2000 May / Chip_2000-05_cd1.bin / zkuste / Perl / ActivePerl-5.6.0.613.msi / 䆊䌷䈹䈙䏵-䞅䞆䞀㡆䞃䄦䠥 / _86c8a1b7764eb114efc57b32b93df248 < prev    next >
Text File  |  2000-03-23  |  15KB  |  345 lines

  1.  
  2. <HTML>
  3. <HEAD>
  4. <TITLE>alias - declare symbolic aliases for perl data</TITLE>
  5. <LINK REL="stylesheet" HREF="../../Active.css" TYPE="text/css">
  6. <LINK REV="made" HREF="mailto:">
  7. </HEAD>
  8.  
  9. <BODY>
  10. <TABLE BORDER=0 CELLPADDING=0 CELLSPACING=0 WIDTH=100%>
  11. <TR><TD CLASS=block VALIGN=MIDDLE WIDTH=100% BGCOLOR="#cccccc">
  12. <STRONG><P CLASS=block> alias - declare symbolic aliases for perl data</P></STRONG>
  13. </TD></TR>
  14. </TABLE>
  15.  
  16. <A NAME="__index__"></A>
  17. <!-- INDEX BEGIN -->
  18.  
  19. <UL>
  20.  
  21.     <LI><A HREF="#name">NAME</A></LI><LI><A HREF="#supportedplatforms">SUPPORTED PLATFORMS</A></LI>
  22.  
  23.     <LI><A HREF="#synopsis">SYNOPSIS</A></LI>
  24.     <LI><A HREF="#description">DESCRIPTION</A></LI>
  25.     <UL>
  26.  
  27.         <LI><A HREF="#functions">Functions</A></LI>
  28.         <LI><A HREF="#configuration variables">Configuration Variables</A></LI>
  29.         <LI><A HREF="#exports">Exports</A></LI>
  30.     </UL>
  31.  
  32.     <LI><A HREF="#examples">EXAMPLES</A></LI>
  33.     <LI><A HREF="#notes">NOTES</A></LI>
  34.     <LI><A HREF="#bugs">BUGS</A></LI>
  35.     <LI><A HREF="#version">VERSION</A></LI>
  36.     <LI><A HREF="#author">AUTHOR</A></LI>
  37.     <LI><A HREF="#see also">SEE ALSO</A></LI>
  38. </UL>
  39. <!-- INDEX END -->
  40.  
  41. <HR>
  42. <P>
  43. <H1><A NAME="name">NAME</A></H1>
  44. <P>alias - declare symbolic aliases for perl data</P>
  45. <P>attr  - auto-declare hash attributes for convenient access</P>
  46. <P>const - define compile-time scalar constants</P>
  47. <P>
  48. <HR>
  49. <H1><A NAME="supportedplatforms">SUPPORTED PLATFORMS</A></H1>
  50. <UL>
  51. <LI>Linux</LI>
  52. <LI>Solaris</LI>
  53. <LI>Windows</LI>
  54. </UL>
  55. <HR>
  56. <H1><A NAME="synopsis">SYNOPSIS</A></H1>
  57. <PRE>
  58.     use Alias qw(alias const attr);
  59.     alias TEN => $ten, Ten => \$ten, Ten => \&ten,
  60.           Ten => \@ten, Ten => \%ten, TeN => \*ten;
  61.     {
  62.        local @Ten;
  63.        my $ten = [1..10];
  64.        alias Ten => $ten;   # local @Ten
  65.     }</PRE>
  66. <PRE>
  67.     const pi => 3.14, ten => 10;</PRE>
  68. <PRE>
  69.     package Foo;
  70.     use Alias;
  71.     sub new { bless {foo => 1, _bar => [2, 3]}, $_[0] }
  72.     sub a_method {
  73.        my $s = attr shift;
  74.        # $foo, @_bar are now local aliases for
  75.        # $_[0]{foo}, @{$_[0]{_bar}} etc.
  76.     }</PRE>
  77. <PRE>
  78.     sub b_method {
  79.       local $Alias::KeyFilter = "_";
  80.       local $Alias::AttrPrefix = "main::";
  81.       my $s = attr shift;
  82.       # local @::_bar is now available, ($foo, $::foo are not)
  83.     }</PRE>
  84. <PRE>
  85.     sub c_method {
  86.       local $Alias::KeyFilter = sub { $_ = shift; return (/^_/ ? 1 : 0) };
  87.       local $Alias::AttrPrefix = sub {
  88.                                        $_ = shift;
  89.                                        s/^_(.+)$/main::$1/;
  90.                                        return $_
  91.                                      };
  92.       my $s = attr shift;
  93.       # local @::bar is now available, ($foo, $::foo are not)
  94.     }</PRE>
  95. <P>
  96. <HR>
  97. <H1><A NAME="description">DESCRIPTION</A></H1>
  98. <P>Provides general mechanisms for aliasing perl data for convenient access.</P>
  99. <P>This module works by putting some values on the symbol table with
  100. user-supplied names.  Values that are references will get dereferenced into
  101. their base types.  This means that a value of <CODE>[1,2,3]</CODE> with a name of
  102. ``foo'' will be made available as <CODE>@foo</CODE>, not <CODE>$foo</CODE>.</P>
  103. <P>The exception to this rule is the default behavior of the <A HREF="#item_attr"><CODE>attr</CODE></A> function,
  104. which will not dereference values which are blessed references (aka
  105. objects).  See <A HREF="../../$Alias/Deref.html">the $Alias::Deref manpage</A> for how to change this default behavior.</P>
  106. <P>
  107. <H2><A NAME="functions">Functions</A></H2>
  108. <DL>
  109. <DT><STRONG><A NAME="item_alias">alias</A></STRONG><BR>
  110. <DD>
  111. Given a list of name => value pairs, declares aliases
  112. in the <A HREF="../../lib/Pod/perlfunc.html#item_caller"><CODE>caller</CODE></A>s namespace. If the value supplied is a reference, the
  113. alias is created for the underlying value instead of the reference
  114. itself (there is no need to use this module to alias references--they
  115. are automatically ``aliased'' on assignment).  This allows the user to
  116. alias most of the basic types.
  117. <P>If the value supplied is a scalar compile-time constant, the aliases 
  118. become read-only. Any attempt to write to them will fail with a run time
  119. error.</P>
  120. <P>Aliases can be dynamically scoped by pre-declaring the target variable as
  121. <A HREF="../../lib/Pod/perlfunc.html#item_local"><CODE>local</CODE></A>.  Using <A HREF="#item_attr"><CODE>attr</CODE></A> for this purpose is more convenient, and
  122. recommended.</P>
  123. <P></P>
  124. <DT><STRONG><A NAME="item_attr">attr</A></STRONG><BR>
  125. <DD>
  126. Given a hash reference, aliases the values of the hash to the names that
  127. correspond to the keys.  It always returns the supplied value.  The aliases
  128. are local to the enclosing block. If any of the values are unblessed
  129. references, they are available as their dereferenced types.  Thus the action
  130. is similar to saying:
  131. <PRE>
  132.     alias %{$_[0]}</PRE>
  133. <P>but, in addition, also localizes the aliases, and does not dereference
  134. objects.  Dereferencing of objects can be forced by setting the <CODE>Deref</CODE>
  135. option.  See <A HREF="../../$Alias/Deref.html">the $Alias::Deref manpage</A>.</P>
  136. <P>This can be used for convenient access to hash values and hash-based object
  137. attributes.</P>
  138. <P>Note that this makes available the semantics of <A HREF="../../lib/Pod/perlfunc.html#item_local"><CODE>local</CODE></A> subroutines and
  139. methods.  That makes for some nifty possibilities.  We could make truly
  140. private methods by putting anonymous subs within an object.  These subs
  141. would be available within methods where we use <A HREF="#item_attr"><CODE>attr</CODE></A>, and will not
  142. be visible to the outside world as normal methods.  We could forbid 
  143. recursion in methods by always putting an empty sub in the object hash 
  144. with the same key as the method name. This would be useful where a method 
  145. has to run code from other modules, but cannot be certain whether that 
  146. module will call it back again.</P>
  147. <P>The default behavior is to create aliases for all the entries in the hash,
  148. in the callers namespace.  This can be controlled by setting a few options.
  149. See <A HREF="#configuration variables">Configuration Variables</A> for details.</P>
  150. <P></P>
  151. <DT><STRONG><A NAME="item_const">const</A></STRONG><BR>
  152. <DD>
  153. This is simply a function alias for <A HREF="#item_alias"><CODE>alias</CODE></A>, described above.  Provided on
  154. demand at <A HREF="../../lib/Pod/perlfunc.html#item_use"><CODE>use</CODE></A> time, since it reads better for constant declarations.
  155. Note that hashes and arrays cannot be so <A HREF="#item_const"><CODE>const</CODE></A>rained.
  156. <P></P></DL>
  157. <P>
  158. <H2><A NAME="configuration variables">Configuration Variables</A></H2>
  159. <P>The following configuration variables can be used to control the behavior of
  160. the <A HREF="#item_attr"><CODE>attr</CODE></A> function.  They are typically set after the <CODE>use Alias;</CODE>
  161. statement.  Another typical usage is to <A HREF="../../lib/Pod/perlfunc.html#item_local"><CODE>local</CODE></A>ize them in a block so that
  162. their values are only effective within that block.</P>
  163. <DL>
  164. <DT><STRONG><A NAME="item_%24Alias%3A%3AKeyFilter">$Alias::KeyFilter</A></STRONG><BR>
  165. <DD>
  166. Specifies the key prefix used for determining which hash entries will be
  167. interned by <A HREF="#item_attr"><CODE>attr</CODE></A>.  Can be a CODE reference, in which case it will be
  168. called with the key, and the boolean return value will determine if
  169. that hash entry is a candidate attribute.
  170. <P></P>
  171. <DT><STRONG><A NAME="item_%24Alias%3A%3AAttrPrefix">$Alias::AttrPrefix</A></STRONG><BR>
  172. <DD>
  173. Specifies a prefix to prepend to the names of localized attributes created
  174. by <A HREF="#item_attr"><CODE>attr</CODE></A>.  Can be a CODE reference, in which case it will be called with
  175. the key, and the result will determine the full name of the attribute.  The
  176. value can have embedded package delimiters (``::'' or ``'''), which cause the
  177. attributes to be interned in that namespace instead of the <A HREF="../../lib/Pod/perlfunc.html#item_caller"><CODE>caller</CODE></A>s own
  178. namespace. For example, setting it to ``main::'' makes <CODE>use strict 'vars';</CODE>
  179. somewhat more palatable (since we can refer to the attributes as <CODE>$::foo</CODE>,
  180. etc., without actually declaring the attributes).
  181. <P></P>
  182. <DT><STRONG><A NAME="item_%24Alias%3A%3ADeref">$Alias::Deref</A></STRONG><BR>
  183. <DD>
  184. Controls the implicit dereferencing behavior of <A HREF="#item_attr"><CODE>attr</CODE></A>.  If it is set to
  185. ``'' or 0, <A HREF="#item_attr"><CODE>attr</CODE></A> will not dereference blessed references.  If it is a true
  186. value (anything but ``'', 0, or a CODE reference), all references will be
  187. made available as their dereferenced types, including values that may be
  188. objects.  The default is ``''.
  189. <P>This option can be used as a filter if it is set to a CODE reference, in
  190. which case it will be called with the key and the value (whenever the value
  191. happens to be a reference), and the boolean return value will determine if
  192. that particular reference must be dereferenced.</P>
  193. <P></P></DL>
  194. <P>
  195. <H2><A NAME="exports">Exports</A></H2>
  196. <DL>
  197. <DT><STRONG>alias</STRONG><BR>
  198. <DD>
  199. <DT><STRONG>attr</STRONG><BR>
  200. <DD>
  201. </DL>
  202. <P>
  203. <HR>
  204. <H1><A NAME="examples">EXAMPLES</A></H1>
  205. <P>Run these code snippets and observe the results to become more familiar
  206. with the features of this module.</P>
  207. <PRE>
  208.     use Alias qw(alias const attr);
  209.     $ten = 10;
  210.     alias TEN => $ten, Ten => \$ten, Ten => \&ten,
  211.           Ten => \@ten, Ten => \%ten;
  212.     alias TeN => \*ten;  # same as *TeN = *ten</PRE>
  213. <PRE>
  214.     # aliasing basic types
  215.     $ten = 20;   
  216.     print "$TEN|$Ten|$ten\n";   # should print "20|20|20"
  217.     sub ten { print "10\n"; }
  218.     @ten = (1..10);
  219.     %ten = (a..j);
  220.     &Ten;                       # should print "10"
  221.     print @Ten, "|", %Ten, "\n";</PRE>
  222. <PRE>
  223.     # this will fail at run time
  224.     const _TEN_ => 10;
  225.     eval { $_TEN_ = 20 };
  226.     print $@ if $@;</PRE>
  227. <PRE>
  228.     # dynamically scoped aliases
  229.     @DYNAMIC = qw(m n o);
  230.     {
  231.        my $tmp = [ qw(a b c d) ];
  232.        local @DYNAMIC;
  233.        alias DYNAMIC => $tmp, PERM => $tmp;</PRE>
  234. <PRE>
  235.        $DYNAMIC[2] = 'zzz';
  236.        # prints "abzzzd|abzzzd|abzzzd"
  237.        print @$tmp, "|", @DYNAMIC, "|", @PERM, "\n";</PRE>
  238. <PRE>
  239.        @DYNAMIC = qw(p q r);
  240.        # prints "pqr|pqr|pqr"
  241.        print @$tmp, "|", @DYNAMIC, "|", @PERM, "\n";
  242.     }</PRE>
  243. <PRE>
  244.     # prints "mno|pqr"
  245.     print @DYNAMIC, "|", @PERM, "\n";</PRE>
  246. <PRE>
  247.     # named closures
  248.     my($lex) = 'abcd';
  249.     $closure = sub { print $lex, "\n" };
  250.     alias NAMEDCLOSURE => \&$closure;
  251.     NAMEDCLOSURE();            # prints "abcd"
  252.     $lex = 'pqrs';
  253.     NAMEDCLOSURE();            # prints "pqrs"</PRE>
  254. <PRE>
  255.     # hash/object attributes
  256.     package Foo;
  257.     use Alias;
  258.     sub new { 
  259.       bless 
  260.         { foo => 1, 
  261.           bar => [2,3], 
  262.           buz => { a => 4},
  263.           privmeth => sub { "private" },
  264.           easymeth => sub { die "to recurse or to die, is the question" },
  265.         }, $_[0]; 
  266.     }</PRE>
  267. <PRE>
  268.     sub easymeth {
  269.       my $s = attr shift;    # localizes $foo, @bar, %buz etc with values
  270.       eval { $s->easymeth }; # should fail
  271.       print $@ if $@;</PRE>
  272. <PRE>
  273.       # prints "1|2|3|a|4|private|"
  274.       print join '|', $foo, @bar, %buz, $s->privmeth, "\n";
  275.     }</PRE>
  276. <PRE>
  277.     $foo = 6;
  278.     @bar = (7,8);
  279.     %buz = (b => 9);
  280.     Foo->new->easymeth;       # this will not recurse endlessly</PRE>
  281. <PRE>
  282.     # prints "6|7|8|b|9|"
  283.     print join '|', $foo, @bar, %buz, "\n";</PRE>
  284. <PRE>
  285.     # this should fail at run-time
  286.     eval { Foo->new->privmeth };
  287.     print $@ if $@;</PRE>
  288. <P>
  289. <HR>
  290. <H1><A NAME="notes">NOTES</A></H1>
  291. <P>It is worth repeating that the aliases created by <A HREF="#item_alias"><CODE>alias</CODE></A> and <A HREF="#item_const"><CODE>const</CODE></A> will
  292. be created in the <A HREF="../../lib/Pod/perlfunc.html#item_caller"><CODE>caller</CODE></A>s namespace (we can use the <CODE>AttrPrefix</CODE> option to
  293. specify a different namespace for <A HREF="#item_attr"><CODE>attr</CODE></A>).  If that namespace happens to be
  294. <A HREF="../../lib/Pod/perlfunc.html#item_local"><CODE>local</CODE></A>ized, the aliases created will be local to that block.  <A HREF="#item_attr"><CODE>attr</CODE></A>
  295. localizes the aliases for us.</P>
  296. <P>Remember that references will be available as their dereferenced types.</P>
  297. <P>Aliases cannot be lexical, since, by neccessity, they live on the
  298. symbol table.</P>
  299. <P>Lexicals can be aliased. Note that this provides a means of reversing the
  300. action of anonymous type generators <CODE>\</CODE>, <CODE>[]</CODE> and <CODE>{}</CODE>.  This allows us
  301. to anonymously construct data or code and give it a symbol-table presence
  302. when we choose.</P>
  303. <P>Any occurrence of <CODE>::</CODE> or <CODE>'</CODE> in names will be treated as package
  304. qualifiers, and the value will be interned in that namespace.</P>
  305. <P>Remember that aliases are very much like references, only we don't
  306. have to dereference them as often.  Which means we won't have to
  307. pound on the dollars so much.</P>
  308. <P>We can dynamically make subroutines and named closures with this scheme.</P>
  309. <P>It is possible to alias packages, but that might be construed as
  310. abuse.</P>
  311. <P>Using this module will dramatically reduce noise characters in 
  312. object-oriented perl code.</P>
  313. <P>
  314. <HR>
  315. <H1><A NAME="bugs">BUGS</A></H1>
  316. <P><CODE>use strict 'vars';</CODE> is not very usable, since we <STRONG>depend</STRONG> so much
  317. on the symbol table.  You can declare the attributes with <CODE>use vars</CODE> to
  318. avoid warnings.  Setting <A HREF="#item_%24Alias%3A%3AAttrPrefix"><CODE>$Alias::AttrPrefix</CODE></A> to ``main::'' is one way
  319. to avoid <CODE>use vars</CODE> and frustration.</P>
  320. <P>Tied variables cannot be aliased properly, yet.</P>
  321. <P>
  322. <HR>
  323. <H1><A NAME="version">VERSION</A></H1>
  324. <P>Version 2.32       30 Apr 1999</P>
  325. <P>
  326. <HR>
  327. <H1><A NAME="author">AUTHOR</A></H1>
  328. <P>Gurusamy Sarathy                <A HREF="mailto:gsar@umich.edu">gsar@umich.edu</A></P>
  329. <P>Copyright (c) 1995-99 Gurusamy Sarathy. All rights reserved.
  330. This program is free software; you can redistribute it and/or
  331. modify it under the same terms as Perl itself.</P>
  332. <P>
  333. <HR>
  334. <H1><A NAME="see also">SEE ALSO</A></H1>
  335. <P><CODE>perl(1)</CODE></P>
  336. <TABLE BORDER=0 CELLPADDING=0 CELLSPACING=0 WIDTH=100%>
  337. <TR><TD CLASS=block VALIGN=MIDDLE WIDTH=100% BGCOLOR="#cccccc">
  338. <STRONG><P CLASS=block> alias - declare symbolic aliases for perl data</P></STRONG>
  339. </TD></TR>
  340. </TABLE>
  341.  
  342. </BODY>
  343.  
  344. </HTML>
  345.