📄 appa_12.htm
字号:
<html><head><title>Answers to Chapter 13 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_11.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_13.htm"><img alt="Next" border="0" src="../gifs/txtnexta.gif" /></a></td></tr></table></div><h2 class="sect1">A.12. Answers to Chapter 13 Exercises</h2><ol><li><p>Here's one way to do it:</p><blockquote><pre class="code">unlink @ARGV;</pre></blockquote><p>...or, if you want to warn the user of any problems:</p><blockquote><pre class="code">foreach (@ARGV) { unlink $_ or warn "Can't unlink '$_': $!, continuing...\n";}</pre></blockquote><p>Here, each item from the command-invocation line is placedindividually into <tt class="literal">$_</tt>, which is then used as theargument to <tt class="literal">unlink</tt>. If something goes wrong, thewarning gives the clue about why.</p></li><li><p>Here's one way to do it:</p><blockquote><pre class="code">use File::Basename;use File::Spec;my($source, $dest) = @ARGV;if (-d $dest) { my $basename = basename $source; $dest = File::Spec->catfile($dest, $basename);}rename $source, $dest or die "Can't rename '$source' to '$dest': $!\n";</pre></blockquote><p>The workhorse in this program is the last statement, but theremainder of the program is necessary when we are renaming into adirectory. First, after declaring the modules we're using, wename the command-line arguments sensibly. If <tt class="literal">$dest</tt>is a directory, we need to extract the basename from the<tt class="literal">$source</tt> name and append it to the directory(<tt class="literal">$dest</tt>). Finally, once <tt class="literal">$dest</tt> ispatched up if needed, the <tt class="literal">rename</tt> does the deed.</p></li><li><p>Here's one way to do it:</p><blockquote><pre class="code">use File::Basename;use File::Spec;my($source, $dest) = @ARGV;if (-d $dest) { my $basename = basename $source; $dest = File::Spec->catfile($dest, $basename);}link $source, $dest or die "Can't link '$source' to '$dest': $!\n";</pre></blockquote><p>As the hint in the exercise description said, this program is muchlike the previous one. The difference is that we'll<tt class="literal">link</tt> rather than <tt class="literal">rename</tt>. Ifyour system doesn't support hard links, you might have writtenthis as the last line:</p><blockquote><pre class="code">print "Would link '$source' to '$dest'.\n";</pre></blockquote></li><li><p>Here's one way to do it:</p><blockquote><pre class="code">use File::Basename;use File::Spec;my $symlink = $ARGV[0] eq '-s';shift @ARGV if $symlink;my($source, $dest) = @ARGV;if (-d $dest) { my $basename = basename $source; $dest = File::Spec->catfile($dest, $basename);}if ($symlink) { symlink $source, $dest or die "Can't make soft link from '$source' to '$dest': $!\n";} else { link $source, $dest or die "Can't make hard link from '$source' to '$dest': $!\n";}</pre></blockquote><p>The first few lines of code (after the two <tt class="literal">use</tt>declarations) look at the first command-line argument, and ifit's "<tt class="literal">-s</tt>", we're making asymbolic link, so we note that as a true value for<tt class="literal">$symlink</tt>. If we saw that"<tt class="literal">-s</tt>", we then need to get rid of it(in the next line). The next few lines are cut-and-pasted from theprevious exercise answers. Finally, based on the truth of<tt class="literal">$symlink</tt>, we'll choose either to create asymbolic link or a hard link. We also updated the dying words to makeit clear which kind of link we were attempting.</p></li><li><p>Here's one way to do it:</p><blockquote><pre class="code">foreach (<.* *>) { my $dest = readlink $_; print "$_ -> $dest\n" if defined $dest;}</pre></blockquote><p>Each item resulting from the glob ends up in <tt class="literal">$_</tt>one by one. If the item is a symbolic link, then<tt class="literal">readlink</tt> returns a defined value, and thelocation is displayed. If not, then the condition fails, and we skipover it.</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_11.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_13.htm"><img alt="Next" border="0" src="../gifs/txtnexta.gif" /></a></td></tr><tr><td align="left" valign="top" width="228">A.11. Answers to Chapter 12 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.13. Answers to Chapter 14 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 + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -