📄 ch32_39.htm
字号:
<html><head><title>Symbol (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="Symbol"><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="ch32_38.htm"><img src="../gifs/txtpreva.gif" alt="Previous" border="0"></a></td><td align="center" valign="top" width="171"><a href="ch32_01.htm">Chapter 32: Standard Modules</a></td><td align="right" valign="top" width="172"><a href="ch32_40.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">32.39. Symbol</h2><p><blockquote><pre class="programlisting">use Symbol "delete_package";delete_package("Foo::Bar");print "deleted\n" unless exists $Foo::{"Bar::"};use Symbol "gensym";$sym1 = getsym(); # Returns new, anonymous typeglob.$sym2 = getsym(); # Yet another new, anonymous typeglob.package Demo;use Symbol "qualify";$sym = qualify("x"); # "Demo::x"$sym = qualify("x", "Foo"); # "Foo::x"$sym = qualify("Bar::x"); # "Bar::x"$sym = qualify("Bar::x", "Foo"); # "Bar::x"use Symbol "qualify_to_ref";sub pass_handle(*) { my $fh = qualify_to_ref(shift, caller); ...}# Now you can call pass_handle with FH, "FH", *FH, or \*FH.</pre></blockquote>The <tt class="literal">Symbol</tt> module provides functions to help manipulate globalnames: typeglobs, format names, filehandles, package symbol tables, andanything else you might want to name via a symbol table. The<tt class="literal">delete_package</tt> function completely clears out a package's namespace(effectively anonymizing any extra references to the symbol table'sreferents, including references from precompiled code). The <tt class="literal">gensym</tt>function returns an anonymous typeglob each time it is called. (Thisfunction isn't used so much these days, now that undefined scalarsautovivify into proper filehandles when used as arguments to <tt class="literal">open</tt>,<tt class="literal">pipe</tt>, <tt class="literal">socket</tt>, and the like).</p><p>The <tt class="literal">qualify</tt> function takes a name that may or may not be completelypackage-qualified, and returns a name that is. If it needs to prependthe package name, it will use the name specified via the second argument(or if omitted, your current package name). The <tt class="literal">qualify_to_ref</tt>function works similarly, but produces a reference to the typeglob thesymbol would represent. This is important in functions that acceptfilehandles, directory handles, or format names as arguments but don't require these to be passed by reference. For example, functionsprototyped with a typeglob accept any of these forms, but don'tautomatically convert barewords to symbol table references. Byconverting that argument with <tt class="literal">qualify_to_ref</tt>, you can now use thesupplied handle even with <tt class="literal">strict refs</tt> in effect. You may also<tt class="literal">bless</tt> it into objectdom, since it's a proper reference.</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="ch32_38.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="ch32_40.htm"><img src="../gifs/txtnexta.gif" alt="Next" border="0"></a></td></tr><tr><td align="left" valign="top" width="172">32.38. Socket</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">32.40. Sys::Hostname</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 + -