📄 ch25_03.htm
字号:
<html><head><title>Files and Filesystems (Programming Perl)</title><!-- STYLESHEET --><link rel="stylesheet" type="text/css" href="../style/style1.css"><!-- METADATA --><!--Dublin Core Metadata--><meta name="DC.Creator" content=""><meta name="DC.Date" content=""><meta name="DC.Format" content="text/xml" scheme="MIME"><meta name="DC.Generator" content="XSLT stylesheet, xt by James Clark"><meta name="DC.Identifier" content=""><meta name="DC.Language" content="en-US"><meta name="DC.Publisher" content="O'Reilly & Associates, Inc."><meta name="DC.Source" content="" scheme="ISBN"><meta name="DC.Subject.Keyword" content=""><meta name="DC.Title" content="Files and Filesystems"><meta name="DC.Type" content="Text.Monograph"></head><body><!-- START OF BODY --><!-- TOP BANNER --><img src="gifs/smbanner.gif" usemap="#banner-map" border="0" alt="Book Home"><map name="banner-map"><AREA SHAPE="RECT" COORDS="0,0,466,71" HREF="index.htm" ALT="Programming Perl"><AREA SHAPE="RECT" COORDS="467,0,514,18" HREF="jobjects/fsearch.htm" ALT="Search this book"></map><!-- TOP NAV BAR --><div class="navbar"><table width="515" border="0"><tr><td align="left" valign="top" width="172"><a href="ch25_02.htm"><img src="../gifs/txtpreva.gif" alt="Previous" border="0"></a></td><td align="center" valign="top" width="171"><a href="ch25_01.htm">Chapter 25: Portable Perl</a></td><td align="right" valign="top" width="172"><a href="ch25_04.htm"><img src="../gifs/txtnexta.gif" alt="Next" border="0"></a></td></tr></table></div><hr width="515" align="left"><!-- SECTION BODY --><h2 class="sect1">25.3. Files and Filesystems</h2><a name="INDEX-4333"></a><a name="INDEX-4334"></a><a name="INDEX-4335"></a><a name="INDEX-4336"></a><a name="INDEX-4337"></a><p>File path components are separated with <tt class="literal">/</tt> on Unix,with <tt class="literal">\</tt> on Windows, and with <tt class="literal">:</tt> onMacs. Some systems support neither hard links(<tt class="literal">link</tt>) nor symbolic links(<tt class="literal">symlink</tt>, <tt class="literal">readlink</tt>,<tt class="literal">lstat</tt>). Some systems pay attention tocapitalization of filenames, some don't, and some pay attention whencreating files but not when reading them.</p><p>There are modules that can help. The standard<tt class="literal">File::Spec</tt> modules provide some functions of theRight Thing persuasion:<blockquote><pre class="programlisting">use File::Spec::Functions;chdir( updir() ); # go up one directory$file = catfile( curdir(), 'temp', 'file.txt' );</pre></blockquote>That last line reads in <em class="emphasis">./temp/file.txt</em> on Unixand Windows, or <em class="emphasis">:temp:file.txt</em> on Macs, or<em class="emphasis">[.temp]file.txt</em> on VMS, and stores the file'scontents in <tt class="literal">$file</tt>.</p><p>The <tt class="literal">File::Basename</tt> module, anotherplatform-tolerant module bundled with Perl, splits a pathname into itscomponents: the base filename, the full path to the directory, and thefile suffix.</p><p>Here are some tips for writing portable file-manipulating Perl programs:</p><ul><li><p><a name="INDEX-4338"></a>Don't use two files of the same name with different case, like<em class="emphasis">test.pl</em> and <em class="emphasis">Test.pl</em>, since someplatforms ignore capitalization.</p></li><li><p>Constrain filenames to the 8.3 convention (eight-letter names andthree-letter extensions) where possible. You can often get away with longerfilenames as long as you make sure the filenames will remain unique whenshoved through an 8.3-sized hole in the wall. (Hey, it's gotta be easier thanshoving a camel through the eye of a needle.)</p></li><li><p>Minimize nonalphanumeric characters in filenames. Using underscores isoften okay, but it wastes a character that could better be used for uniquenesson 8.3 systems. (Remember, that's why we don't usually put underscoresinto module names.)</p></li><li><p><a name="INDEX-4339"></a>Likewise, when using the <tt class="literal">AutoSplit</tt> module, try to constrain yoursubroutine names to eight characters or less, and don't give twosubroutines the same name with different case. If you need longersubroutine names, make the first eight characters of each unique.</p></li><li><p>Always use <tt class="literal"><</tt> explicitly to open a file for reading;otherwise, on systems that allow punctuation in filenames, afile prefixed with a <tt class="literal">></tt> character could result in a file beingwiped out, and a file prefixed with a <tt class="literal">|</tt> character could result in apipe open. That's because the two-argument form of <tt class="literal">open</tt> is magicaland will interpret characters like <tt class="literal">></tt>, <tt class="literal"><</tt>, and <tt class="literal">|</tt>, whichmay be the wrong thing to do. (Except when it's right.)<blockquote><pre class="programlisting">open(FILE, $existing_file) or die $!; # wrongishopen(FILE, "<$existing_file") or die $!; # righteropen(FILE, "<", $existing_file) or die $!; # righterer</pre></blockquote></p></li><li><p>Don't assume text files will end with a newline. They should, butsometimes people forget, especially when their text editor helps them forget.</p></li></ul><!-- BOTTOM NAV BAR --><hr width="515" align="left"><div class="navbar"><table width="515" border="0"><tr><td align="left" valign="top" width="172"><a href="ch25_02.htm"><img src="../gifs/txtpreva.gif" alt="Previous" border="0"></a></td><td align="center" valign="top" width="171"><a href="index.htm"><img src="../gifs/txthome.gif" alt="Home" border="0"></a></td><td align="right" valign="top" width="172"><a href="ch25_04.htm"><img src="../gifs/txtnexta.gif" alt="Next" border="0"></a></td></tr><tr><td align="left" valign="top" width="172">25.2. Endianness and Number Width</td><td align="center" valign="top" width="171"><a href="index/index.htm"><img src="../gifs/index.gif" alt="Book Index" border="0"></a></td><td align="right" valign="top" width="172">25.4. System Interaction</td></tr></table></div><hr width="515" align="left"><!-- LIBRARY NAV BAR --><img src="../gifs/smnavbar.gif" usemap="#library-map" border="0" alt="Library Navigation Links"><p><font size="-1"><a href="copyrght.htm">Copyright © 2001</a> O'Reilly & Associates. All rights reserved.</font></p><map name="library-map"> <area shape="rect" coords="2,-1,79,99" href="../index.htm"><area shape="rect" coords="84,1,157,108" href="../perlnut/index.htm"><area shape="rect" coords="162,2,248,125" href="../prog/index.htm"><area shape="rect" coords="253,2,326,130" href="../advprog/index.htm"><area shape="rect" coords="332,1,407,112" href="../cookbook/index.htm"><area shape="rect" coords="414,2,523,103" href="../sysadmin/index.htm"></map><!-- END OF BODY --></body></html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -