📄 ch02_10.htm
字号:
<html><head><title>Typeglobs and Filehandles (Programming Perl)</title><!-- STYLESHEET --><link rel="stylesheet" type="text/css" href="../style/style1.css"><!-- METADATA --><!--Dublin Core Metadata--><meta name="DC.Creator" content=""><meta name="DC.Date" content=""><meta name="DC.Format" content="text/xml" scheme="MIME"><meta name="DC.Generator" content="XSLT stylesheet, xt by James Clark"><meta name="DC.Identifier" content=""><meta name="DC.Language" content="en-US"><meta name="DC.Publisher" content="O'Reilly & Associates, Inc."><meta name="DC.Source" content="" scheme="ISBN"><meta name="DC.Subject.Keyword" content=""><meta name="DC.Title" content="Typeglobs and Filehandles"><meta name="DC.Type" content="Text.Monograph"></head><body><!-- START OF BODY --><!-- TOP BANNER --><img src="gifs/smbanner.gif" usemap="#banner-map" border="0" alt="Book Home"><map name="banner-map"><AREA SHAPE="RECT" COORDS="0,0,466,71" HREF="index.htm" ALT="Programming Perl"><AREA SHAPE="RECT" COORDS="467,0,514,18" HREF="jobjects/fsearch.htm" ALT="Search this book"></map><!-- TOP NAV BAR --><div class="navbar"><table width="515" border="0"><tr><td align="left" valign="top" width="172"><a href="ch02_09.htm"><img src="../gifs/txtpreva.gif" alt="Previous" border="0"></a></td><td align="center" valign="top" width="171"><a href="ch02_01.htm">Chapter 2: Bits and Pieces</a></td><td align="right" valign="top" width="172"><a href="ch02_11.htm"><img src="../gifs/txtnexta.gif" alt="Next" border="0"></a></td></tr></table></div><hr width="515" align="left"><!-- SECTION BODY --><h2 class="sect1">2.10. Typeglobs and Filehandles</h2><p><a name="INDEX-723"></a><a name="INDEX-724"></a><a name="INDEX-725"></a><a name="INDEX-726"></a><a name="INDEX-727"></a>Perl uses an special type called a <em class="emphasis">typeglob</em> to hold an entire symboltable entry. (The symbol table entry <tt class="literal">*foo</tt> contains the values of<tt class="literal">$foo</tt>, <tt class="literal">@foo</tt>, <tt class="literal">%foo</tt>, <tt class="literal">&foo</tt>, and severalinterpretations of plain old <tt class="literal">foo</tt>.) The type prefix of a typeglob isa <tt class="literal">*</tt> because it represents all types.</p><p><a name="INDEX-728"></a>One use of typeglobs (or references thereto) is for passing orstoring filehandles. If you want to save away a filehandle, do itthis way:<blockquote><pre class="programlisting">$fh = *STDOUT;</pre></blockquote>or perhaps as a real reference, like this:<blockquote><pre class="programlisting">$fh = \*STDOUT;</pre></blockquote><a name="INDEX-729"></a><a name="INDEX-730"></a></p><p>This is also the way to create a local filehandle. For example:<blockquote><pre class="programlisting">sub newopen { my $path = shift; local *FH; # not my() nor our() open(FH, $path) or return undef; return *FH; # not \*FH!}$fh = newopen('/etc/passwd');</pre></blockquote>See the <tt class="literal">open</tt> function for other ways to generate new filehandles.</p><p><a name="INDEX-731"></a><a name="INDEX-732"></a>The main use of typeglobs nowadays is to alias one symbol tableentry to another symbol table entry. Think of an alias as anickname. If you say:<blockquote><pre class="programlisting">*foo = *bar;</pre></blockquote>it makes everything named "<tt class="literal">foo</tt>" a synonym for every correspondingthing named "<tt class="literal">bar</tt>". You can alias just one variable from atypeglob by assigning a reference instead:<blockquote><pre class="programlisting">*foo = \$bar;</pre></blockquote>makes <tt class="literal">$foo</tt> an alias for <tt class="literal">$bar</tt>,but doesn't make <tt class="literal">@foo</tt> an alias for<tt class="literal">@bar</tt>, or <tt class="literal">%foo</tt> an alias for<tt class="literal">%bar</tt>. All these affect global (package) variablesonly; lexicals cannot be accessed through symbol table entries.Aliasing global variables like this may seem like a silly thing towant to do, but it turns out that the entire module export/importmechanism is built around this feature, since there's nothing thatsays the symbol you're aliasing has to be in your namespace. This:<blockquote><pre class="programlisting">local *Here::blue = \$There::green;</pre></blockquote>temporarily makes <tt class="literal">$Here::blue</tt> an alias for<tt class="literal">$There::green</tt>, but doesn't make<tt class="literal">@Here::blue</tt> an alias for<tt class="literal">@There::green</tt>, or <tt class="literal">%Here::blue</tt> analias for <tt class="literal">%There::green</tt>. Fortunately, all thesecomplicated typeglob manipulations are hidden away where you don'thave to look at them. See the sections <a href="ch08_02.htm#ch08-sect-hr">Section 2.2.4, "Handle References"</a> and<a href="ch08_02.htm#ch08-sect-str">Section 2.2.5, "Symbol Table References"</a> in <a href="ch08_01.htm">Chapter 8, "References"</a>,the section <a href="ch10_01.htm#ch10-sect-st">Section 2.1, "Symbol Tables"</a> in <a href="ch10_01.htm">Chapter 10, "Packages"</a>,and <a href="ch11_01.htm">Chapter 11, "Modules"</a>, for more discussion ontypeglobs and importation.</p><!-- BOTTOM NAV BAR --><hr width="515" align="left"><div class="navbar"><table width="515" border="0"><tr><td align="left" valign="top" width="172"><a href="ch02_09.htm"><img src="../gifs/txtpreva.gif" alt="Previous" border="0"></a></td><td align="center" valign="top" width="171"><a href="index.htm"><img src="../gifs/txthome.gif" alt="Home" border="0"></a></td><td align="right" valign="top" width="172"><a href="ch02_11.htm"><img src="../gifs/txtnexta.gif" alt="Next" border="0"></a></td></tr><tr><td align="left" valign="top" width="172">2.9. Hashes</td><td align="center" valign="top" width="171"><a href="index/index.htm"><img src="../gifs/index.gif" alt="Book Index" border="0"></a></td><td align="right" valign="top" width="172">2.11. Input Operators</td></tr></table></div><hr width="515" align="left"><!-- LIBRARY NAV BAR --><img src="../gifs/smnavbar.gif" usemap="#library-map" border="0" alt="Library Navigation Links"><p><font size="-1"><a href="copyrght.htm">Copyright © 2001</a> O'Reilly & Associates. All rights reserved.</font></p><map name="library-map"> <area shape="rect" coords="2,-1,79,99" href="../index.htm"><area shape="rect" coords="84,1,157,108" href="../perlnut/index.htm"><area shape="rect" coords="162,2,248,125" href="../prog/index.htm"><area shape="rect" coords="253,2,326,130" href="../advprog/index.htm"><area shape="rect" coords="332,1,407,112" href="../cookbook/index.htm"><area shape="rect" coords="414,2,523,103" href="../sysadmin/index.htm"></map><!-- END OF BODY --></body></html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -