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

📄 ch01_11.htm

📁 By Tom Christiansen and Nathan Torkington ISBN 1-56592-243-3 First Edition, published August 1998
💻 HTM
字号:
<HTML><HEAD><TITLE>Recipe 1.10. Interpolating Functions and Expressions Within Strings (Perl Cookbook)</TITLE><METANAME="DC.title"CONTENT="Perl Cookbook"><METANAME="DC.creator"CONTENT="Tom Christiansen &amp; Nathan Torkington"><METANAME="DC.publisher"CONTENT="O'Reilly &amp; Associates, Inc."><METANAME="DC.date"CONTENT="1999-07-02T01:29:08Z"><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="ch01_01.htm"TITLE="1. Strings"><LINKREL="prev"HREF="ch01_10.htm"TITLE="1.9. Controlling Case"><LINKREL="next"HREF="ch01_12.htm"TITLE="1.11. Indenting Here Documents"></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="ch01_10.htm"TITLE="1.9. Controlling Case"><IMGSRC="../gifs/txtpreva.gif"ALT="Previous: 1.9. Controlling Case"BORDER="0"></A></TD><TDALIGN="CENTER"VALIGN="TOP"WIDTH="228"><B><FONTFACE="ARIEL,HELVETICA,HELV,SANSERIF"SIZE="-1"><ACLASS="chapter"REL="up"HREF="ch01_01.htm"TITLE="1. Strings"></A></FONT></B></TD><TDALIGN="RIGHT"VALIGN="TOP"WIDTH="228"><ACLASS="sect1"HREF="ch01_12.htm"TITLE="1.11. Indenting Here Documents"><IMGSRC="../gifs/txtnexta.gif"ALT="Next: 1.11. Indenting Here Documents"BORDER="0"></A></TD></TR></TABLE></DIV><DIVCLASS="sect1"><H2CLASS="sect1"><ACLASS="title"NAME="ch01-40187">1.10. Interpolating Functions and Expressions Within Strings</A></H2><DIVCLASS="sect2"><H3CLASS="sect2"><ACLASS="title"NAME="ch01-pgfId-1067">Problem <ACLASS="indexterm"NAME="ch01-idx-1000010279-0"></A><ACLASS="indexterm"NAME="ch01-idx-1000010279-1"></A><ACLASS="indexterm"NAME="ch01-idx-1000010279-2"></A></A></H3><PCLASS="para">You want a function call or expression to expand within a string. This lets you construct more complex templates than with simple scalar variable interpolation.</P></DIV><DIVCLASS="sect2"><H3CLASS="sect2"><ACLASS="title"NAME="ch01-pgfId-1073">Solution</A></H3><PCLASS="para">You can break up your expression into distinct concatenated pieces:</P><PRECLASS="programlisting">$answer = $var1 . func() . $var2;   # scalar only</PRE><PCLASS="para">Or you can use the slightly sneaky <CODECLASS="literal">@{[</CODE> <CODECLASS="literal">LIST</CODE> <CODECLASS="literal">EXPR</CODE> <CODECLASS="literal">]}</CODE> or <CODECLASS="literal">${</CODE> <CODECLASS="literal">\(SCALAR</CODE> <CODECLASS="literal">EXPR</CODE> <CODECLASS="literal">)</CODE> <CODECLASS="literal">}</CODE> expansions:</P><PRECLASS="programlisting">$answer = &quot;STRING @{[ LIST EXPR ]} MORE STRING&quot;;$answer = &quot;STRING ${\( SCALAR EXPR )} MORE STRING&quot;;</PRE></DIV><DIVCLASS="sect2"><H3CLASS="sect2"><ACLASS="title"NAME="ch01-pgfId-1087">Discussion</A></H3><PCLASS="para">This code shows both techniques. The first line shows concatenation; the second shows the expansion trick:</P><PRECLASS="programlisting">$phrase = &quot;I have &quot; . ($n + 1) . &quot; guanacos.&quot;;$phrase = &quot;I have ${\($n + 1)} guanacos.&quot;;</PRE><PCLASS="para">The first technique builds the final string by concatenating smaller strings, avoiding interpolation but achieving the same end. Because <CODECLASS="literal">print</CODE> effectively concatenates its entire argument list, if we were going to <CODECLASS="literal">print</CODE> <CODECLASS="literal">$phrase</CODE>, we could have just said:</P><PRECLASS="programlisting">print &quot;I have &quot;,  $n + 1, &quot; guanacos.\n&quot;;</PRE><PCLASS="para">When you absolutely must have interpolation, you need the punctuation-riddled interpolation from the Solution. Only <CODECLASS="literal">@</CODE>, <CODECLASS="literal">$</CODE>, and <CODECLASS="literal">\</CODE> are special within double quotes and most backquotes. (As with <CODECLASS="literal">m//</CODE> and <CODECLASS="literal">s///</CODE>, the <CODECLASS="literal">qx()</CODE> synonym is not subject to double-quote expansion if its delimiter is single quotes! <CODECLASS="literal">$home</CODE> <CODECLASS="literal">=</CODE> <CODECLASS="literal">qx'echo</CODE> <CODECLASS="literal">home</CODE> <CODECLASS="literal">is</CODE> <CODECLASS="literal">$HOME';</CODE> would get the shell <CODECLASS="literal">$HOME</CODE> variable, not one in Perl.) So, the only way to force arbitrary expressions to expand is by expanding a <CODECLASS="literal">${}</CODE> or <CODECLASS="literal">@{}</CODE> whose block contains a reference.</P><PCLASS="para">You can do more than simply assign to a variable after interpolation. It's a general mechanism that can be used in any double-quoted string. For instance, this example will build a string with an interpolated expression and pass the result to a function:</P><PRECLASS="programlisting">some_func(&quot;What you want is @{[ split /:/, $rec ]} items&quot;);</PRE><PCLASS="para">You can interpolate into a here document, as by:</P><PRECLASS="programlisting">die &quot;Couldn't send mail&quot; unless send_mail(&lt;&lt;&quot;EOTEXT&quot;, $target);To: $naughtyFrom: Your BankCc: @{ get_manager_list($naughty) }Date: @{[ do { my $now = `date`; chomp $now; $now } ]} (today)Dear $naughty,Today, you bounced check number @{[ 500 + int rand(100) ]} to us.Your account is now closed.Sincerely,the managementEOTEXT</PRE><PCLASS="para">Expanding <ACLASS="indexterm"NAME="ch01-idx-1000010285-0"></A><ACLASS="indexterm"NAME="ch01-idx-1000010285-1"></A>backquotes (<CODECLASS="literal">``</CODE>) is particularly challenging because you would normally end up with spurious newlines. By creating a braced block following the <CODECLASS="literal">@</CODE> within the <CODECLASS="literal">@{[]}</CODE> anonymous array dereference, as we did in the last example, you can create private variables.</P><PCLASS="para">Although these techniques work, simply breaking your work up into several steps or storing everything in temporary variables is almost always clearer to the reader.</P><PCLASS="para">In version 5.004 of Perl, <CODECLASS="literal">${\</CODE> <CODECLASS="literal">EXPR</CODE> <CODECLASS="literal">}</CODE> wrongly evaluates <CODECLASS="literal">EXPR</CODE> in list instead of scalar context. This bug is fixed in version 5.005.</P></DIV><DIVCLASS="sect2"><H3CLASS="sect2"><ACLASS="title"NAME="ch01-pgfId-1143">See Also</A></H3><PCLASS="para"><EMCLASS="emphasis">perlref </EM>(1) and the <ACLASS="olink"HREF="../prog/ch04_03.htm#PERL2-CH-4-SECT-3.5">"Other Tricks You Can Do with Hard References"</A> section of <ACLASS="olink"HREF="../prog/ch04_01.htm">Chapter 4</A> of <ACLASS="citetitle"HREF="../prog/index.htm"TITLE="Programming Perl"><CITECLASS="citetitle">Programming Perl</CITE></A><ACLASS="indexterm"NAME="ch01-idx-1000010281-0"></A><ACLASS="indexterm"NAME="ch01-idx-1000010281-1"></A><ACLASS="indexterm"NAME="ch01-idx-1000010281-2"></A></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="ch01_10.htm"TITLE="1.9. Controlling Case"><IMGSRC="../gifs/txtpreva.gif"ALT="Previous: 1.9. Controlling Case"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="ch01_12.htm"TITLE="1.11. Indenting Here Documents"><IMGSRC="../gifs/txtnexta.gif"ALT="Next: 1.11. Indenting Here Documents"BORDER="0"></A></TD></TR><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="228">1.9. Controlling Case</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">1.11. Indenting Here Documents</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 &copy; 2002</a> O'Reilly &amp; 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 + -