📄 ch11_07.htm
字号:
<html><head><title>Exercises (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="ch11_06.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="ch12_01.htm"><img alt="Next" border="0" src="../gifs/txtnexta.gif" /></a></td></tr></table></div><h2 class="sect1">11.7. Exercises</h2><p>See <a href="appa_10.htm#lperl3-APP-A-SECT-10">Section A.10, "Answers to Chapter 11 Exercises"</a> for answers to the followingexercises:</p><ol><li><p>[20] Make a program which asks the user for a source file name, adestination file name, a search pattern, and a replacement string.(Be sure to ask the user interactively for these; don't getthem from the command-line arguments.) Your program should read thesource file and write it out as the destination file, replacing thesearch pattern with the replacement string wherever it appears. Thatis, the destination file will be a modified duplicate of the sourcefile. Can you overwrite an existing file (not the same as the inputfile)? Can you use regular expression metacharacters in the searchpattern? (That is, can you enter <tt class="literal">(fred|wilma)flintstone</tt> to search for either name?) Can you use thememory variables and backslash escapes in the replacement string?(That is, can you use <tt class="literal">\u\L$1\E Flintstone</tt> as thereplacement string to properly capitalize the names of Fred andWilma?) Don't worry if you can't accomplish each of thesethings; it's more important simply to see what happens when youtry.</p></li><li><p>[15] Make a program which takes a list of files named on the commandline and reports for each one whether it's readable, writable,executable, or doesn't exist. (Hint: It may be helpful to havea function which will do all of the file tests for one file at atime.) What does it report about a file which has been<i class="command">chmod</i>'ed to <tt class="literal">0</tt>? (That is,if you're on a Unix system, use the command <i class="command">chmod 0some_file</i> to mark that file as neither being readable,writable, nor executable.) In most shells, use a star as the argumentto mean all of the normal files in the current directory. That is,you could type something like <tt class="literal">./ex11-2 *</tt> to askthe program for the attributes of many files at once.</p></li><li><p>[10] Make a program to identify the oldest file named on the commandline and report its age in days. What does it do if the list isempty? (That is, if no files are mentioned on the command line.)</p></li></ol><hr width="684" align="left" /><div class="navbar"><table width="684" border="0"><tr><td align="left" valign="top" width="228"><a href="ch11_06.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="ch12_01.htm"><img alt="Next" border="0" src="../gifs/txtnexta.gif" /></a></td></tr><tr><td align="left" valign="top" width="228">11.6. File Tests</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">12. Directory Operations</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 + -