appa_11.htm
来自「by Randal L. Schwartz and Tom Phoenix I」· HTM 代码 · 共 149 行
HTM
149 行
<html><head><title>Answers to Chapter 12 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="appa_10.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="appa_12.htm"><img alt="Next" border="0" src="../gifs/txtnexta.gif" /></a></td></tr></table></div><h2 class="sect1">A.11. Answers to Chapter 12 Exercises</h2><ol><li><p>Here's one way to do it, with a glob:</p><blockquote><pre class="code">print "Which directory? (Default is your home directory) ";chomp(my $dir = <STDIN>);if ($dir =~ /^\s*$/) { # A blank line chdir or die "Can't chdir to your home directory: $!"; } else { chdir $dir or die "Can't chdir to '$dir': $!";}my @files = <*>;foreach (@files) { print "$_\n";}</pre></blockquote><p>First, we show a simple prompt, and read the desired directory,chomping it as needed. (Without a chomp, we'd be trying to headfor a directory that ends in a newline -- legal in Unix, andtherefore cannot be presumed to simply be extraneous by the<tt class="literal">chdir</tt> function.)</p><p>Then, if the directory name is nonempty, we'll change to thatdirectory, aborting on a failure. If empty, the home directory isselected instead.</p><p>Finally, a glob on "star" pulls up all the names in the(new) working directory, automatically sorted to alphabetical order,and they're printed one at a time.</p></li><li><p>Here's one way to do it:</p><blockquote><pre class="code">print "Which directory? (Default is your home directory) ";chomp(my $dir = <STDIN>);if ($dir =~ /^\s*$/) { # A blank line chdir or die "Can't chdir to your home directory:$!"; } else { chdir $dir or die "Can't chdir to '$dir': $!";}my @files = <.* *>; ## now includes .*foreach (sort @files) { ## now sorts print "$_\n";}</pre></blockquote><p>Two differences from previous one: first, the glob now includes"dot star", which matches all the names that<em class="emphasis">do</em> begin with a dot. And second, we now mustsort the resulting list, because some of the names that begin with adot must be interleaved appropriately either before or after the listof things without a beginning dot.</p></li><li><p>Here's one way to do it:</p><blockquote><pre class="code">print "Which directory? (Default is your home directory) ";chomp(my $dir = <STDIN>);if ($dir =~ /^\s*$/) { # A blank line chdir or die "Can't chdir to your home directory:$!"; } else { chdir $dir or die "Can't chdir to '$dir': $!";}opendir DOT, "." or die "Can't opendir dot: $!";foreach (sort readdir DOT) { # next if /^\./; ## if we were skipping dot files print "$_\n";}</pre></blockquote><p>Again, same structure as the previous two programs, but nowwe've chosen to open a directory handle. Once we'vechanged the working directory, we want to open the current directory,and we've shown that as the <tt class="literal">DOT</tt> directoryhandle.</p><p>Why <tt class="literal">DOT</tt>? Well, if the user asks for an absolutedirectory name, like <tt class="literal">/etc</tt>, there's noproblem opening it. But if the name is relative, like<tt class="literal">fred</tt>, let's see what would happen. First, we<tt class="literal">chdir</tt> to <tt class="literal">fred</tt>, and then we wantto use <tt class="literal">opendir</tt> to open it. But that would open<tt class="literal">fred</tt> in the new directory, not<tt class="literal">fred</tt> in the original directory. The only name wecan be sure will mean "the current directory" is"<tt class="literal">.</tt>", which always has that meaning (onUnix and similar systems, at least).</p><p>The <tt class="literal">readdir</tt> function pulls up all the names ofthe directory, which are then sorted, and displayed. If we had donethe first exercise this way, we would have skipped over the dotfiles, and that's handled by the uncommenting the commented-outline in the <tt class="literal">foreach</tt> loop.</p><p>You may find yourself asking, "Why did we<tt class="literal">chdir</tt> first? You can use<tt class="literal">readdir</tt> and friends on any directory, not merelyon the current directory." Primarily, we wanted to give theuser the convenience of being able to get to their home directorywith a single keystroke. But this could be the start of a generalfile-management utility program; maybe the next step would be to askthe user which of the files in this directory should be moved tooffline tape storage, say.</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="appa_10.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="appa_12.htm"><img alt="Next" border="0" src="../gifs/txtnexta.gif" /></a></td></tr><tr><td align="left" valign="top" width="228">A.10. Answers to Chapter 11 Exercises</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">A.12. Answers to Chapter 13 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 © 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 + =
减小字号Ctrl + -
显示快捷键?