⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ch09_05.htm

📁 编程珍珠,里面很多好用的代码,大家可以参考学习呵呵,
💻 HTM
字号:
<html><head><title>Hashes of Functions (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 &amp; Associates, Inc."><meta name="DC.Source" content="" scheme="ISBN"><meta name="DC.Subject.Keyword" content=""><meta name="DC.Title" content="Hashes of Functions"><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="ch09_04.htm"><img src="../gifs/txtpreva.gif" alt="Previous" border="0"></a></td><td align="center" valign="top" width="171"><a href="ch09_01.htm">Chapter 9: Data Structures</a></td><td align="right" valign="top" width="172"><a href="ch09_06.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">9.5. Hashes of Functions</h2><p><a name="INDEX-2156"></a><a name="INDEX-2157"></a><a name="INDEX-2158"></a>When writing a complex application or network service in Perl, youmight want to make a large number of commands available to your users.Such a program might have code like this to examine the user's selectionand take appropriate action:<blockquote><pre class="programlisting">if    ($cmd =~ /^exit$/i)     { exit }elsif ($cmd =~ /^help$/i)     { show_help() }elsif ($cmd =~ /^watch$/i)    { $watch = 1 }elsif ($cmd =~ /^mail$/i)     { mail_msg($msg) }elsif ($cmd =~ /^edit$/i)     { $edited++; editmsg($msg); }elsif ($cmd =~ /^delete$/i)   { confirm_kill() }else {    warn "Unknown command: `$cmd'; Try `help' next time\n";}</pre></blockquote><a name="INDEX-2159"></a>You can also store references to functions in your data structures, justas you can store references to arrays or hashes:<blockquote><pre class="programlisting">%HoF = (                           # Compose a hash of functions    exit    =&gt;  sub { exit },    help    =&gt;  \&amp;show_help,    watch   =&gt;  sub { $watch = 1 },    mail    =&gt;  sub { mail_msg($msg) },    edit    =&gt;  sub { $edited++; editmsg($msg); },    delete  =&gt;  \&amp;confirm_kill,);if   ($HoF{lc $cmd}) { $HoF{lc $cmd}-&gt;() }   # Call functionelse { warn "Unknown command: `$cmd'; Try `help' next time\n" }</pre></blockquote><a name="INDEX-2160"></a>In the second to last line, we check whether the specified commandname (in lowercase) exists in our "dispatch table",<tt class="literal">%HoF</tt>.  If so, we invoke the appropriate command bydereferencing the hash value as a function and pass that function anempty argument list.  We could also have dereferenced it as<tt class="literal">&amp;{ $HoF{lc $cmd} }()</tt>, or, as of the 5.6 releaseof Perl, simply <tt class="literal">$HoF{lc $cmd}()</tt>.</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="ch09_04.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="ch09_06.htm"><img src="../gifs/txtnexta.gif" alt="Next" border="0"></a></td></tr><tr><td align="left" valign="top" width="172">9.4. Hashes of 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">9.6. More Elaborate Records</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 &copy; 2001</a> O'Reilly &amp; 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 + -