📄 ch12_01.htm
字号:
<html><head><title>Directory Operations (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_07.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_02.htm"><img alt="Next" border="0" src="../gifs/txtnexta.gif" /></a></td></tr></table></div>
<h1 class="chapter">Chapter 12. Directory Operations</h1>
<div class="htmltoc"><h4 class="tochead">Contents:</h4>
<p> <a href="#lperl3-CHP-12-SECT-1">Moving Around the Directory Tree</a><br />
<a href="ch12_02.htm">Globbing</a><br />
<a href="ch12_03.htm">An Alternate Syntax for Globbing</a><br />
<a href="ch12_04.htm">Directory Handles</a><br />
<a href="ch12_05.htm">Recursive Directory Listing</a><br />
<a href="ch12_06.htm">Exercises</a><br /></p></div>
<p><a name="INDEX-851" /></a>The
files we created in the previous chapter were generally in the same
place as our program. But modern operating systems let us organize
files into directories, allowing us to keep our Beatles MP3s away
from our important Llama book chapter sources so that we don't
accidentally send an MP3 file to the publisher. Perl lets you
manipulate these directories directly, in ways that are even fairly
portable from one operating system to another.
</p>
<div class="sect1"><a name="lperl3-CHP-12-SECT-1" /></a>
<h2 class="sect1">12.1. Moving Around the Directory Tree</h2>
<p>Your program runs with a "working directory," which is
the starting point for relative pathnames. That is, if you refer to
the file <tt class="literal">fred</tt>, that means
"<tt class="literal">fred</tt> in the current working
directory."
</p>
<p>The <tt class="literal">chdir</tt><a name="INDEX-852" /></a> operator changes the
<a name="INDEX-853" /></a>working directory.
It's just like the Unix shell's <a name="INDEX-854" /></a><i class="command">cd</i> command:
</p>
<blockquote><pre class="code">chdir "/etc" or die "cannot chdir to /etc: $!";</pre></blockquote>
<p>Because this is a system request, the value of <tt class="literal">$!</tt>
will be set if an error occurs. You should normally check
<tt class="literal">$!</tt> when a false value is returned from
<tt class="literal">chdir</tt>, since that indicates that something has
not gone as requested.
</p>
<p>The working directory is inherited by all processes that Perl starts
(we'll talk more about that in <a href="ch14_01.htm">Chapter 14, "Process Management"</a>).
However, the change in working directory cannot affect the process
that invoked Perl, such as the shell.<a href="#FOOTNOTE-275">[275]</a> So you can't make a Perl
program to replace your shell's <i class="command">cd</i> command.
</p><blockquote class="footnote"> <a name="FOOTNOTE-275" /></a><p>[275]This
isn't a limitation on Perl's part; it's actually a
feature of Unix, Windows, and other systems. If you really need to
change the shell's working directory, see the documentation of
your shell. </p> </blockquote>
<p>If you omit the parameter, Perl determines your
<a name="INDEX-855" /></a>home
directory as best as possible and attempts to set the working
directory to your home directory, similar to using the
<i class="command">cd</i> command at the shell without a parameter. This
is one of the few places where omitting the parameter doesn't
use <tt class="literal">$_</tt>.
</p>
<p>Some shells permit you to use a tilde-prefixed path with
<i class="command">cd</i> to use another user's home directory as a
starting point (like <tt class="literal">cd</tt>
<tt class="literal">~merlyn</tt>). This is a function of the shell, not the
operating system, and Perl is calling the operating system directly.
Thus, a <a name="INDEX-856" /></a> <a name="INDEX-857" /></a>tilde-prefix will not work with
<tt class="literal">chdir</tt>.
</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="ch11_07.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_02.htm"><img alt="Next" border="0" src="../gifs/txtnexta.gif" /></a></td></tr><tr><td align="left" valign="top" width="228">11.7. 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">12.2. Globbing</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 + -