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

📄 ch03_06.htm

📁 编程珍珠,里面很多好用的代码,大家可以参考学习呵呵,
💻 HTM
字号:
<html><head><title>Binding Operators (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="Binding Operators"><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="ch03_05.htm"><img src="../gifs/txtpreva.gif" alt="Previous" border="0"></a></td><td align="center" valign="top" width="171"><a href="ch03_01.htm">Chapter 3: Unary and Binary Operators</a></td><td align="right" valign="top" width="172"><a href="ch03_07.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">3.6. Binding Operators</h2><p><a name="INDEX-840"></a><a name="INDEX-841"></a>Binary <tt class="literal">=~</tt> binds astring expression to a pattern match, substitution, or transliteration(loosely called translation).  These operations would otherwise searchor modify the string contained in <tt class="literal">$_</tt> (the defaultvariable).  The string you want to bind is put on the left, while theoperator itself is put on the right.  The return value indicates thesuccess or failure of the operator on the right, since the bindingoperator doesn't really do anything on its own.</p><p>If the right argument is an expression rather than a pattern match,substitution, or transliteration, it will be interpreted as a searchpattern at run time.  That is to say, <tt class="literal">$_ =~ $pat</tt> isequivalent to <tt class="literal">$_ =~ /$pat/</tt>.  This is less efficientthan an explicit search, since the pattern must be checked andpossibly recompiled every time the expression is evaluated.  You canavoid this recompilation by precompiling the original pattern usingthe <tt class="literal">qr//</tt> (quote regex) operator.</p><p><a name="INDEX-842"></a>Binary <tt class="literal">!~</tt> is just like <tt class="literal">=~</tt> except the return value is negated logically.  The following expressions are functionallyequivalent:<blockquote><pre class="programlisting">$string !~ /pattern/not $string =~ /pattern/</pre></blockquote>We said that the return value indicates success, but there are manykinds of success.  Substitutions return the number of successfulmatches, as do transliterations.  (In fact, the transliterationoperator is often used to count characters.)  Since any nonzero resultis true, it all works out.  The most spectacular kind of true value isa list assignment of a pattern: in a list context, pattern matches canreturn substrings matched by the parentheses in the pattern.  Butagain, according to the rules of list assignment, the list assignmentitself will return true if anything matched and was assigned, andfalse otherwise.  So you sometimes see things like:<blockquote><pre class="programlisting">if ( ($k,$v) = $string =~ m/(\w+)=(\w*)/ ) {    print "KEY $k VALUE $v\n";}</pre></blockquote>Let's pick that apart.  The <tt class="literal">=~</tt> has precedence over<tt class="literal">=</tt>, so <tt class="literal">=~</tt> happens first.  The<tt class="literal">=~</tt> binds <tt class="literal">$string</tt> to the patternmatch on the right, which is scanning for occurrences of things thatlook like<em class="replaceable">KEY</em><tt class="literal">=</tt><em class="replaceable">VALUE</em>in your string.  It's in a list context because it's on the right sideof a list assignment.  If the pattern matches, it returns a list to beassigned to <tt class="literal">$k</tt> and <tt class="literal">$v</tt>.  The listassignment itself is in a scalar context, so it returns<tt class="literal">2</tt>, the number of values on the right side of theassignment.  And <tt class="literal">2</tt> happens to be true, since ourscalar context is also a Boolean context.  When the match fails, novalues are assigned, which returns 0, which is false.</p><p>For more on the politics of patterns, see <a href="ch05_01.htm">Chapter 5, "Pattern Matching"</a>.</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="ch03_05.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="ch03_07.htm"><img src="../gifs/txtnexta.gif" alt="Next" border="0"></a></td></tr><tr><td align="left" valign="top" width="172">3.5. Ideographic Unary Operators</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">3.7. Multiplicative 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 &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 + -