📄 ch09_03.htm
字号:
<html><head><title>The Binding Operator, =~ (Learning Perl, 3rd Edition)</title><link rel="stylesheet" type="text/css" href="../style/style1.css" /><meta name="DC.Creator" content="Randal L. Schwartz and Tom Phoenix" /><meta name="DC.Format" content="text/xml" scheme="MIME" /><meta name="DC.Language" content="en-US" /><meta name="DC.Publisher" content="O'Reilly & Associates, Inc." /><meta name="DC.Source" scheme="ISBN" content="0596001320L" /><meta name="DC.Subject.Keyword" content="stuff" /><meta name="DC.Title" content="Learning Perl, 3rd Edition" /><meta name="DC.Type" content="Text.Monograph" /></head><body bgcolor="#ffffff"><img alt="Book Home" border="0" src="gifs/smbanner.gif" usemap="#banner-map" /><map name="banner-map"><area shape="rect" coords="1,-2,616,66" href="index.htm" alt="Learning Perl, 3rd Edition" /><area shape="rect" coords="629,-11,726,25" href="jobjects/fsearch.htm" alt="Search this book" /></map><div class="navbar"><table width="684" border="0"><tr><td align="left" valign="top" width="228"><a href="ch09_02.htm"><img alt="Previous" border="0" src="../gifs/txtpreva.gif" /></a></td><td align="center" valign="top" width="228"><a href="index.htm"></a></td><td align="right" valign="top" width="228"><a href="ch09_04.htm"><img alt="Next" border="0" src="../gifs/txtnexta.gif" /></a></td></tr></table></div><h2 class="sect1">9.3. The Binding Operator, =~</h2><p>Matching against <tt class="literal">$_</tt> is merely the default; the<em class="firstterm">binding operator</em><a name="INDEX-590" /> <a name="INDEX-591" /><a name="INDEX-592" /> <a name="INDEX-593" /> (<tt class="literal">=~</tt>) tells Perl tomatch the pattern on the right against the string on the left,instead of matching against <tt class="literal">$_</tt>.<a href="#FOOTNOTE-195">[195]</a> For example:</p><blockquote class="footnote"><a name="FOOTNOTE-195" /><p>[195]The binding operator is also used with some other operationsbesides the pattern match, as we'll see later.</p></blockquote><blockquote><pre class="code">my $some_other = "I dream of betty rubble.";if ($some_other =~ /\brub/) { print "Aye, there's the rub.\n";}</pre></blockquote><p>The first time you see it, the binding operator looks like some kindof assignment operator. But it's no such thing! It is simplysaying, "this pattern match which would attach to<tt class="literal">$_</tt> by default -- make it work with this stringon the left instead." If there's no binding operator, theexpression is using <tt class="literal">$_</tt> by default.</p><p>In the (somewhat unusual) example below,<tt class="literal">$likes_perl</tt> is set to a Boolean value according towhat the user typed at the prompt. This is a little on thequick-and-dirty side, because the line of input itself is discarded.This code reads the line of input, tests that string against thepattern, then discards the line of input.<a href="#FOOTNOTE-196">[196]</a> Itdoesn't use or change <tt class="literal">$_</tt> at all.</p><blockquote class="footnote"> <a name="FOOTNOTE-196" /><p>[196]Remember,the line of input is not automatically stored into<tt class="literal">$_</tt>unless the line-input operator(<tt class="literal"><STDIN></tt>) is all alone in the conditionalexpression of a <tt class="literal">while</tt> loop.</p> </blockquote><blockquote><pre class="code">print "Do you like Perl? ";my $likes_perl = (<STDIN> =~ /\byes\b/i);... # Time passes...if ($likes_perl) { print "You said earlier that you like Perl, so...\n"; ...}</pre></blockquote><p>The parentheses around the pattern-test expression aren'trequired, so the following line does the same thing as the oneabove -- it stores the result of the test (and not the line ofinput) into the variable:</p><blockquote><pre class="code">my $likes_perl = <STDIN> =~ /\byes\b/i;</pre></blockquote><hr width="684" align="left" /><div class="navbar"><table width="684" border="0"><tr><td align="left" valign="top" width="228"><a href="ch09_02.htm"><img alt="Previous" border="0" src="../gifs/txtpreva.gif" /></a></td><td align="center" valign="top" width="228"><a href="index.htm"><img alt="Home" border="0" src="../gifs/txthome.gif" /></a></td><td align="right" valign="top" width="228"><a href="ch09_04.htm"><img alt="Next" border="0" src="../gifs/txtnexta.gif" /></a></td></tr><tr><td align="left" valign="top" width="228">9.2. Option Modifiers</td><td align="center" valign="top" width="228"><a href="index/index.htm"><img alt="Book Index" border="0" src="../gifs/index.gif" /></a></td><td align="right" valign="top" width="228">9.4. Interpolating into Patterns</td></tr></table></div><hr width="684" align="left" /><img alt="Library Navigation Links" border="0" src="../gifs/navbar.gif" usemap="#library-map" /><p><p><font size="-1"><a href="copyrght.htm">Copyright © 2002</a> O'Reilly & Associates. All rights reserved.</font></p><map name="library-map"><area shape="rect" coords="1,0,85,94" href="../index.htm"><area shape="rect" coords="86,1,178,103" href="../lwp/index.htm"><area shape="rect" coords="180,0,265,103" href="../lperl/index.htm"><area shape="rect" coords="267,0,353,105" href="../perlnut/index.htm"><area shape="rect" coords="354,1,446,115" href="../prog/index.htm"><area shape="rect" coords="448,0,526,132" href="../tk/index.htm"><area shape="rect" coords="528,1,615,119" href="../cookbook/index.htm"><area shape="rect" coords="617,0,690,135" href="../pxml/index.htm"></map></body></html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -