📄 ch06_02.htm
字号:
<HTML><HEAD><TITLE>Recipe 6.1. Copying and Substituting Simultaneously (Perl Cookbook)</TITLE><METANAME="DC.title"CONTENT="Perl Cookbook"><METANAME="DC.creator"CONTENT="Tom Christiansen & Nathan Torkington"><METANAME="DC.publisher"CONTENT="O'Reilly & Associates, Inc."><METANAME="DC.date"CONTENT="1999-07-02T01:33:38Z"><METANAME="DC.type"CONTENT="Text.Monograph"><METANAME="DC.format"CONTENT="text/html"SCHEME="MIME"><METANAME="DC.source"CONTENT="1-56592-243-3"SCHEME="ISBN"><METANAME="DC.language"CONTENT="en-US"><METANAME="generator"CONTENT="Jade 1.1/O'Reilly DocBook 3.0 to HTML 4.0"><LINKREV="made"HREF="mailto:online-books@oreilly.com"TITLE="Online Books Comments"><LINKREL="up"HREF="ch06_01.htm"TITLE="6. Pattern Matching"><LINKREL="prev"HREF="ch06_01.htm"TITLE="6.0. Introduction"><LINKREL="next"HREF="ch06_03.htm"TITLE="6.2. Matching Letters"></HEAD><BODYBGCOLOR="#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="Perl Cookbook"><area shape="rect" coords="629,-11,726,25" href="jobjects/fsearch.htm" alt="Search this book" /></map><div class="navbar"><p><TABLEWIDTH="684"BORDER="0"CELLSPACING="0"CELLPADDING="0"><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="228"><ACLASS="sect1"HREF="ch06_01.htm"TITLE="6.0. Introduction"><IMGSRC="../gifs/txtpreva.gif"ALT="Previous: 6.0. Introduction"BORDER="0"></A></TD><TDALIGN="CENTER"VALIGN="TOP"WIDTH="228"><B><FONTFACE="ARIEL,HELVETICA,HELV,SANSERIF"SIZE="-1"><ACLASS="chapter"REL="up"HREF="ch06_01.htm"TITLE="6. Pattern Matching"></A></FONT></B></TD><TDALIGN="RIGHT"VALIGN="TOP"WIDTH="228"><ACLASS="sect1"HREF="ch06_03.htm"TITLE="6.2. Matching Letters"><IMGSRC="../gifs/txtnexta.gif"ALT="Next: 6.2. Matching Letters"BORDER="0"></A></TD></TR></TABLE></DIV><DIVCLASS="sect1"><H2CLASS="sect1"><ACLASS="title"NAME="ch06-chap06_copying_0">6.1. Copying and Substituting Simultaneously</A></H2><DIVCLASS="sect2"><H3CLASS="sect2"><ACLASS="title"NAME="ch06-pgfId-231">Problem</A></H3><PCLASS="para"><ACLASS="indexterm"NAME="ch06-idx-1000007513-0"></A><ACLASS="indexterm"NAME="ch06-idx-1000007513-1"></A><ACLASS="indexterm"NAME="ch06-idx-1000007513-2"></A><ACLASS="indexterm"NAME="ch06-idx-1000007513-3"></A>You're tired of constantly using two separate statements with redundant information, one to copy and another to substitute.</P></DIV><DIVCLASS="sect2"><H3CLASS="sect2"><ACLASS="title"NAME="ch06-pgfId-237">Solution</A></H3><PCLASS="para">Instead of:</P><PRECLASS="programlisting">$dst = $src;$dst =~ s/this/that/;</PRE><PCLASS="para">use:</P><PRECLASS="programlisting">($dst = $src) =~ s/this/that/;</PRE></DIV><DIVCLASS="sect2"><H3CLASS="sect2"><ACLASS="title"NAME="ch06-pgfId-251">Discussion</A></H3><PCLASS="para">Sometimes what you wish you could have is the new string, but you don't care to write it in two steps.</P><PCLASS="para">For example:</P><PRECLASS="programlisting"># strip to basename($progname = $0) =~ s!^.*/!!;# Make All Words Title-Cased($capword = $word) =~ s/(\w+)/\u\L$1/g;# /usr/man/man3/foo.1 changes to /usr/man/cat3/foo.1($catpage = $manpage) =~ s/man(?=\d)/cat/;</PRE><PCLASS="para">You can even use this technique on an entire array:</P><PRECLASS="programlisting">@bindirs = qw( /usr/bin /bin /usr/local/bin );for (@libdirs = @bindirs) { s/bin/lib/ }print "@libdirs\n";<CODECLASS="userinput"><B><CODECLASS="replaceable"><I>/usr/lib /lib /usr/local/lib</I></CODE></B></CODE></PRE><PCLASS="para">The parentheses are required when combining an assignment if you wish to change the result in the leftmost variable. Normally, the result of a substitution is its success: either <CODECLASS="literal">""</CODE> for failure, or the number of times the substitution was done. Contrast this with the preceding examples where the parentheses surround the assignment itself. For example:</P><PRECLASS="programlisting">($a = $b) =~ s/x/y/g; # copy $b and then change $a $a = ($b =~ s/x/y/g); # change $b, count goes in $a<ACLASS="indexterm"NAME="ch06-idx-1000007515-0"></A><ACLASS="indexterm"NAME="ch06-idx-1000007515-1"></A><ACLASS="indexterm"NAME="ch06-idx-1000007515-2"></A><ACLASS="indexterm"NAME="ch06-idx-1000007515-3"></A></PRE></DIV><DIVCLASS="sect2"><H3CLASS="sect2"><ACLASS="title"NAME="ch06-pgfId-291">See Also</A></H3><PCLASS="para">The <ACLASS="olink"HREF="../prog/ch02_03.htm#PERL2-CH-2-SECT-3.1">"Variables"</A> section of <ACLASS="olink"HREF="../prog/ch02_01.htm">Chapter 2</A> of <ACLASS="citetitle"HREF="../prog/index.htm"TITLE="Programming Perl"><CITECLASS="citetitle">Programming Perl</CITE></A>, and the "Assignment Operators" section of <ICLASS="filename">perlop </I>(1)</P></DIV></DIV><DIVCLASS="htmlnav"><P></P><HRALIGN="LEFT"WIDTH="684"TITLE="footer"><TABLEWIDTH="684"BORDER="0"CELLSPACING="0"CELLPADDING="0"><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="228"><ACLASS="sect1"HREF="ch06_01.htm"TITLE="6.0. Introduction"><IMGSRC="../gifs/txtpreva.gif"ALT="Previous: 6.0. Introduction"BORDER="0"></A></TD><TDALIGN="CENTER"VALIGN="TOP"WIDTH="228"><ACLASS="book"HREF="index.htm"TITLE="Perl Cookbook"><IMGSRC="../gifs/txthome.gif"ALT="Perl Cookbook"BORDER="0"></A></TD><TDALIGN="RIGHT"VALIGN="TOP"WIDTH="228"><ACLASS="sect1"HREF="ch06_03.htm"TITLE="6.2. Matching Letters"><IMGSRC="../gifs/txtnexta.gif"ALT="Next: 6.2. Matching Letters"BORDER="0"></A></TD></TR><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="228">6.0. Introduction</TD><TDALIGN="CENTER"VALIGN="TOP"WIDTH="228"><ACLASS="index"HREF="index/index.htm"TITLE="Book Index"><IMGSRC="../gifs/index.gif"ALT="Book Index"BORDER="0"></A></TD><TDALIGN="RIGHT"VALIGN="TOP"WIDTH="228">6.2. Matching Letters</TD></TR></TABLE><HRALIGN="LEFT"WIDTH="684"TITLE="footer"><FONTSIZE="-1"></DIV<!-- LIBRARY NAV BAR --> <img src="../gifs/smnavbar.gif" usemap="#library-map" border="0" alt="Library Navigation Links"><p> <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 + -