📄 ch09_04.htm
字号:
<html><head><title>Interpolating into Patterns (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_03.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_05.htm"><img alt="Next" border="0" src="../gifs/txtnexta.gif" /></a></td></tr></table></div><h2 class="sect1">9.4. Interpolating into Patterns</h2><p>The regular expression is double-quote interpolated, just as if itwere a double-quoted string. This allows us to write a quick<i class="command">grep</i><a name="INDEX-594" />-like program like this:</p><blockquote><pre class="code">#!/usr/bin/perl -wmy $what = "larry";while (<>) { if (/^($what)/) { # pattern is anchored at beginning of string print "We saw $what in beginning of $_"; }}</pre></blockquote><p>The pattern will be built up out of whatever's in<tt class="literal">$what</tt> when we run the pattern match. In this case,it's the same as if we had written<tt class="literal">/^(larry)/</tt>, looking for <tt class="literal">larry</tt>at the start of each line.</p><p>But we didn't have to get the value of <tt class="literal">$what</tt>from a literal string; we could have gotten it instead from thecommand-line arguments in <tt class="literal">@ARGV</tt>:</p><blockquote><pre class="code">my $what = shift @ARGV;</pre></blockquote><p>Now, if the first command-line argument were<tt class="literal">fred|barney</tt>, the pattern becomes<tt class="literal">/^(fred|barney)/</tt>, looking for<tt class="literal">fred</tt> or <tt class="literal">barney</tt> at the start ofeach line.<a href="#FOOTNOTE-197">[197]</a> The parentheses(which weren't really necessary when searching for<tt class="literal">larry</tt>) are important, now, because without themwe'd be matching <tt class="literal">fred</tt> at the start or<tt class="literal">barney</tt> anywhere in the string.</p><blockquote class="footnote"> <a name="FOOTNOTE-197" /><p>[197]The astute reader will know that youcan't generally type <tt class="literal">fred|barney</tt> as anargument at the command line because the vertical bar is a shellmetacharacter. See the documentation to your shell to learn about howto quote command-line arguments.</p> </blockquote><p>With that line changed to get the pattern from<tt class="literal">@ARGV</tt>, this program resembles the Unix<i class="command">grep</i> command. But we have to watch out formetacharacters in the string. If <tt class="literal">$what</tt> contains<tt class="literal">'fred(barney'</tt>, the pattern would look like<tt class="literal">/^(fred(barney)/</tt>, and you know that can'twork right -- it'll crash your program with an invalidregular expression error. With some advanced techniques,<a href="#FOOTNOTE-198">[198]</a> you can trap this kind of error (orprevent the magic of the metacharacters in the first place) so thatit won't crash your program. But for now, just know that if yougive your users the power of regular expressions, they'll alsoneed the responsibility to use them correctly.</p><blockquote class="footnote"><a name="FOOTNOTE-198" /><p>[198]In this case, you would use an <tt class="literal">eval</tt> block totrap the error, or you would quote the interpolated text using<tt class="literal">quotemeta</tt> (or its <tt class="literal">\Q</tt>equivalent form) so that it's no longer treated as a regularexpression.</p> </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_03.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_05.htm"><img alt="Next" border="0" src="../gifs/txtnexta.gif" /></a></td></tr><tr><td align="left" valign="top" width="228">9.3. The Binding Operator, =~</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.5. The Match Variables</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 + -