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

📄 ch16_04.htm

📁 by Randal L. Schwartz and Tom Phoenix ISBN 0-596-00132-0 Third Edition, published July 2001. (See
💻 HTM
📖 第 1 页 / 共 2 页
字号:
<p>We already know about much of the diamond's magic -- it willautomatically open and close a series of files for you, or read fromthe standard-input stream if there aren't any filenames given.But when there's a string in <tt class="literal">$^I</tt>, thatstring is used as a backup filename's extension. Let'ssee that in action.</p><p>Let's say it's time for the diamond to open our file<em class="filename">fred03.dat</em>. It opens it like before, but now itrenames it, calling it <em class="filename">fred03.dat.bak</em>.<a href="#FOOTNOTE-357">[357]</a> We'vestill got the same file open, but now it has a different name on thedisk. Next, the diamond creates a new file and gives it the name<em class="filename">fred03.dat</em>. That's okay; we weren'tusing that name any more. And now the diamond selects the new file asthe default for output, so that anything that we print will go intothat file.<a href="#FOOTNOTE-358">[358]</a></p><blockquote class="footnote"><a name="FOOTNOTE-357" /><p>[357]Some of the details of this procedure will vary on non-Unixsystems, but the end result should be nearly the same. See therelease notes for your port of Perl.</p> </blockquote><blockquote class="footnote"> <a name="FOOTNOTE-358" /><p>[358]The diamond also tries to duplicate theoriginal file's permission and ownership settings as much aspossible; for example, if the old one was world-readable, the new oneshould be, as well.</p> </blockquote><p>So now the <tt class="literal">while</tt> loop will read a line from theold file, update that, and print it out to the new file. This programcan update hundreds of files in a few seconds on a typical machine.Pretty powerful, huh?</p><p>Once the program has finished, what does the user see? The user says,"Ah, I see what happened! Perl edited my file<em class="filename">fred03.dat</em>, making the changes I needed, andsaved me a copy of the original in the <a name="INDEX-1075" />backup file<em class="filename">fred03.dat.bak</em> just to be helpful!" But wenow know the truth: Perl didn't really edit any file. It made amodified copy, said "Abracadabra!", and switched thefiles around while we were watching sparks come out of the magicwand. Tricky.</p><p>Some folks use a <a name="INDEX-1076" /><a name="INDEX-1077" />tilde("<tt class="literal">~</tt>") as the value for<tt class="literal">$^I</tt>, since that resembles what<i class="command">emacs</i> does for backup files. Another possiblevalue for <tt class="literal">$^I</tt> is the empty string. This enablesin-place editing, but doesn't save the original data in abackup file. But since a small typo in your pattern could wipe outall of the old data, using the empty string is recommended only ifyou want to find out how good your backup tapes are. It's easyenough to delete the backup files when you're done. And whensomething goes wrong and you need to rename the backup files to theiroriginal names, you'll be glad that you know how to use Perl todo that (see the multiple-file rename example in <a href="ch13_01.htm">Chapter 13, "Manipulating Files and Directories"</a>).</p><a name="lperl3-CHP-16-SECT-4.1" /><div class="sect2"><h3 class="sect2">16.4.1. In-place Editing from the Command Line</h3><p><a name="INDEX-1078" /> <a name="INDEX-1079" />A program like the examplefrom the previous section is fairly easy to write. But Larry decidedit wasn't easy enough.</p><p>Imagine that you need to update hundreds of files that have themisspelling <tt class="literal">Randal</tt> instead of theone-<tt class="literal">l</tt> name <tt class="literal">Randal</tt>. You couldwrite a program like the one in the previous section. Or you could doit all with a one-line program, right on the command line:</p><blockquote><pre class="code">$ <tt class="userinput"><b>perl -p -i.bak -w -e 's/Randal/Randal/g' fred*.dat</b></tt></pre></blockquote><p>Perl has a whole slew of command-line options that can be used tobuild a complete program in a few keystrokes.<a href="#FOOTNOTE-359">[359]</a>Let's see what these few do.</p><blockquote class="footnote"> <a name="FOOTNOTE-359" /><p>[359]Seethe<a name="INDEX-1080" /> <em class="emphasis">perlrun</em>manpage for the complete list.</p> </blockquote><p>Starting the command with <tt class="literal">perl</tt> does something likeputting <tt class="literal">#!/usr/bin/perl</tt> at the top of a file does:it says to use the program <em class="filename">perl</em> to process whatfollows.</p><p>The <tt class="literal">-p</tt><a name="INDEX-1081" /> option tells Perl to write a program foryou. It's not much of a program, though; it looks somethinglike this:<a href="#FOOTNOTE-360">[360]</a></p><blockquote class="footnote"> <a name="FOOTNOTE-360" /><p>[360]Actually, the <tt class="literal">print</tt>occurs in a <tt class="literal">continue</tt> block. See the<em class="emphasis">perlsyn</em>and<em class="emphasis">perlrun</em>manpages for moreinformation.</p> </blockquote><blockquote><pre class="code">while (&lt;&gt;) { print; }. </pre></blockquote><p>If you want even less, you could use<tt class="literal">-n</tt><a name="INDEX-1082" /> instead; that leaves out the<tt class="literal">print</tt> statement. (Fans of <i class="command">awk</i>will recognize <tt class="literal">-p</tt> and <tt class="literal">-n</tt>.)Again, it's not much of a program, but it's pretty goodfor the price of a few keystrokes.</p><p>The next option is<tt class="literal">-i.bak</tt><a name="INDEX-1083" />, which you might have guessed sets<tt class="literal">$^I</tt> to <tt class="literal">".bak"</tt> before theprogram starts. If you don't want a backup file, you can use<tt class="literal">-i</tt> alone, with no extension.</p><p>We've seen <tt class="literal">-w</tt><a name="INDEX-1084" /> before -- it turns on warnings.</p><p>The <tt class="literal">-e</tt><a name="INDEX-1085" /> option says "executable codefollows." That means that the<tt class="literal">s/Randal/Randal/g</tt> string is treated as Perl code.Since we've already got a <tt class="literal">while</tt> loop (fromthe <tt class="literal">-p</tt> option), this code is put inside the loop,before the <tt class="literal">print</tt>. For technical reasons, the lastsemicolon in the <tt class="literal">-e</tt> code is optional. But if youhave more than one <tt class="literal">-e</tt>, and thus more than onechunk of code, only the semicolon at the end of the last one maysafely be omitted.</p><p>The last command-line parameter is <tt class="literal">fred*.dat</tt>,which says that <tt class="literal">@ARGV</tt> should hold the list offilenames that match that glob. Put the pieces all together, andit's as if we had written a program like this:</p><blockquote><pre class="code">#!/usr/bin/perl -w@ARGV = glob "fred*.dat";$^I = ".bak";while (&lt;&gt;) {  s/Randal/Randal/g;  print;}</pre></blockquote><p>Compare this program to the one we used in the previous section.It's pretty similar. These command-line options are prettyhandy, aren't they?<a name="INDEX-1086" /> <a name="INDEX-1087" /> <a name="INDEX-1088" /></p></div><hr width="684" align="left" /><div class="navbar"><table width="684" border="0"><tr><td align="left" valign="top" width="228"><a href="ch16_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="ch16_05.htm"><img alt="Next" border="0" src="../gifs/txtnexta.gif" /></a></td></tr><tr><td align="left" valign="top" width="228">16.3. Fixed-length Random-access Databases</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">16.5. Exercises</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 &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 + -