⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ch09_11.htm

📁 By Tom Christiansen and Nathan Torkington ISBN 1-56592-243-3 First Edition, published August 1998
💻 HTM
字号:
<HTML><HEAD><TITLE>Recipe 9.10. Splitting a Filename into Its Component Parts (Perl Cookbook)</TITLE><METANAME="DC.title"CONTENT="Perl Cookbook"><METANAME="DC.creator"CONTENT="Tom Christiansen &amp; Nathan Torkington"><METANAME="DC.publisher"CONTENT="O'Reilly &amp; Associates, Inc."><METANAME="DC.date"CONTENT="1999-07-02T01:39:21Z"><METANAME="DC.type"CONTENT="Text.Monograph"><METANAME="DC.format"CONTENT="text/html"SCHEME="MIME"><METANAME="DC.source"CONTENT="1-56592-243-3"SCHEME="ISBN"><METANAME="DC.language"CONTENT="en-US"><METANAME="generator"CONTENT="Jade 1.1/O'Reilly DocBook 3.0 to HTML 4.0"><LINKREV="made"HREF="mailto:online-books@oreilly.com"TITLE="Online Books Comments"><LINKREL="up"HREF="ch09_01.htm"TITLE="9. Directories"><LINKREL="prev"HREF="ch09_10.htm"TITLE="9.9. Renaming Files"><LINKREL="next"HREF="ch09_12.htm"TITLE="9.11. Program: symirror"></HEAD><BODYBGCOLOR="#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="Perl Cookbook"><area shape="rect" coords="629,-11,726,25" href="jobjects/fsearch.htm" alt="Search this book" /></map><div class="navbar"><p><TABLEWIDTH="684"BORDER="0"CELLSPACING="0"CELLPADDING="0"><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="228"><ACLASS="sect1"HREF="ch09_10.htm"TITLE="9.9. Renaming Files"><IMGSRC="../gifs/txtpreva.gif"ALT="Previous: 9.9. Renaming Files"BORDER="0"></A></TD><TDALIGN="CENTER"VALIGN="TOP"WIDTH="228"><B><FONTFACE="ARIEL,HELVETICA,HELV,SANSERIF"SIZE="-1"><ACLASS="chapter"REL="up"HREF="ch09_01.htm"TITLE="9. Directories"></A></FONT></B></TD><TDALIGN="RIGHT"VALIGN="TOP"WIDTH="228"><ACLASS="sect1"HREF="ch09_12.htm"TITLE="9.11. Program: symirror"><IMGSRC="../gifs/txtnexta.gif"ALT="Next: 9.11. Program: symirror"BORDER="0"></A></TD></TR></TABLE></DIV><DIVCLASS="sect1"><H2CLASS="sect1"><ACLASS="title"NAME="ch09-chap09_splitting_0">9.10. Splitting a Filename into Its Component Parts</A></H2><DIVCLASS="sect2"><H3CLASS="sect2"><ACLASS="title"NAME="ch09-pgfId-1032">Problem<ACLASS="indexterm"NAME="ch09-idx-1000004439-0"></A><ACLASS="indexterm"NAME="ch09-idx-1000004439-1"></A><ACLASS="indexterm"NAME="ch09-idx-1000004439-2"></A><ACLASS="indexterm"NAME="ch09-idx-1000004439-3"></A></A></H3><PCLASS="para">You want to extract a filename, its enclosing directory, or the extension(s)<EMCLASS="emphasis"> </EM>from a string that contains a full pathname.</P></DIV><DIVCLASS="sect2"><H3CLASS="sect2"><ACLASS="title"NAME="ch09-pgfId-1038">Solution</A></H3><PCLASS="para">Use routines from the standard File::Basename module.</P><PRECLASS="programlisting">use File::Basename;$base = basename($path);$dir  = dirname($path);($base, $dir, $ext) = fileparse($path);</PRE></DIV><DIVCLASS="sect2"><H3CLASS="sect2"><ACLASS="title"NAME="ch09-pgfId-1054">Discussion</A></H3><PCLASS="para">The standard File::Basename module contains routines to split up a filename. <CODECLASS="literal">dirname</CODE><ACLASS="indexterm"NAME="ch09-idx-1000004440-0"></A><ACLASS="indexterm"NAME="ch09-idx-1000004440-1"></A> and <CODECLASS="literal">basename</CODE> supply the directory and filename portions respectively:</P><PRECLASS="programlisting">$path = '/usr/lib/libc.a';$file = basename($path);    $dir  = dirname($path);     print &quot;dir is $dir, file is $file\n&quot;;# dir is /usr/lib, file is libc.a</PRE><PCLASS="para">The <CODECLASS="literal">fileparse</CODE><ACLASS="indexterm"NAME="ch09-idx-1000004441-0"></A> function can be used to extract the extension. To do so, pass <CODECLASS="literal">fileparse</CODE> the path to decipher and a regular expression that matches the extension. You must give <CODECLASS="literal">fileparse</CODE> this pattern because an extension isn't necessarily dot-separated. Consider <CODECLASS="literal">&quot;.tar.gz&quot;--</CODE>is the extension <CODECLASS="literal">&quot;.tar&quot;</CODE>, <CODECLASS="literal">&quot;.gz&quot;</CODE>, or <CODECLASS="literal">&quot;.tar.gz&quot;</CODE>? By specifying the pattern, you control which of these you get.</P><PRECLASS="programlisting">$path = '/usr/lib/libc.a';($name,$dir,$ext) = fileparse($path,'\..*');print &quot;dir is $dir, name is $name, extension is $ext\n&quot;;# dir is /usr/lib/, name is libc, extension is .a</PRE><PCLASS="para">By default, these routines parse pathnames using your operating system's normal conventions for directory separators by looking at the <CODECLASS="literal">$^O</CODE> variable, which holds a string identifying the system you're running on. That value was determined when Perl was built and installed. You can change the default by calling the <CODECLASS="literal">fileparse_set_fstype</CODE> routine. This alters the behavior of subsequent calls to the File::Basename functions:</P><PRECLASS="programlisting">fileparse_set_fstype(&quot;MacOS&quot;);$path = &quot;Hard%20Drive:System%20Folder:README.txt&quot;;($name,$dir,$ext) = fileparse($path,'\..*');print &quot;dir is $dir, name is $name, extension is $ext\n&quot;;# dir is Hard%20Drive:System%20Folder, name is README, extension is .txt</PRE><PCLASS="para">To pull out just the extension, you might use this:</P><PRECLASS="programlisting">sub extension {    my $path = shift;    my $ext = (fileparse($path,'\..*'))[2];    $ext =~ s/^\.//;    return $ext;}</PRE><PCLASS="para">When called on a file like <EMCLASS="emphasis">source.c.bak</EM>, this returns an extension of <CODECLASS="literal">&quot;c.bak&quot;</CODE>, not just <CODECLASS="literal">&quot;bak&quot;</CODE>. If you wanted just <CODECLASS="literal">&quot;.bak&quot;</CODE> returned, use <CODECLASS="literal">'\..*?'</CODE> as the second argument to <CODECLASS="literal">fileparse</CODE>.</P><PCLASS="para">When passed a pathname with a trailing directory separator, such as <CODECLASS="literal">lib/</CODE>, <CODECLASS="literal">fileparse</CODE> considers the directory name to be <CODECLASS="literal">&quot;lib/&quot;</CODE>, whereas <CODECLASS="literal">dirname</CODE> considers it to be <CODECLASS="literal">&quot;.&quot;</CODE>. <ACLASS="indexterm"NAME="ch09-idx-1000004443-0"></A><ACLASS="indexterm"NAME="ch09-idx-1000004443-1"></A><ACLASS="indexterm"NAME="ch09-idx-1000004443-2"></A><ACLASS="indexterm"NAME="ch09-idx-1000004443-3"></A></P></DIV><DIVCLASS="sect2"><H3CLASS="sect2"><ACLASS="title"NAME="ch09-pgfId-1116">See Also</A></H3><PCLASS="para">The documentation for the standard File::Basename module (also in <ACLASS="olink"HREF="../prog/ch07_01.htm">Chapter 7</A> of <ACLASS="citetitle"HREF="../prog/index.htm"TITLE="Programming Perl"><CITECLASS="citetitle">Programming Perl</CITE></A>); the entry for <CODECLASS="literal">$^O</CODE> in <ICLASS="filename">perlvar </I>(1), and in the <ACLASS="olink"HREF="../prog/ch02_09.htm">"Special Variables"</A> section of <ACLASS="olink"HREF="../prog/ch02_01.htm">Chapter 2</A> of <ACLASS="citetitle"HREF="../prog/index.htm"TITLE="Programming Perl"><CITECLASS="citetitle">Programming Perl</CITE></A></P></DIV></DIV><DIVCLASS="htmlnav"><P></P><HRALIGN="LEFT"WIDTH="684"TITLE="footer"><TABLEWIDTH="684"BORDER="0"CELLSPACING="0"CELLPADDING="0"><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="228"><ACLASS="sect1"HREF="ch09_10.htm"TITLE="9.9. Renaming Files"><IMGSRC="../gifs/txtpreva.gif"ALT="Previous: 9.9. Renaming Files"BORDER="0"></A></TD><TDALIGN="CENTER"VALIGN="TOP"WIDTH="228"><ACLASS="book"HREF="index.htm"TITLE="Perl Cookbook"><IMGSRC="../gifs/txthome.gif"ALT="Perl Cookbook"BORDER="0"></A></TD><TDALIGN="RIGHT"VALIGN="TOP"WIDTH="228"><ACLASS="sect1"HREF="ch09_12.htm"TITLE="9.11. Program: symirror"><IMGSRC="../gifs/txtnexta.gif"ALT="Next: 9.11. Program: symirror"BORDER="0"></A></TD></TR><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="228">9.9. Renaming Files</TD><TDALIGN="CENTER"VALIGN="TOP"WIDTH="228"><ACLASS="index"HREF="index/index.htm"TITLE="Book Index"><IMGSRC="../gifs/index.gif"ALT="Book Index"BORDER="0"></A></TD><TDALIGN="RIGHT"VALIGN="TOP"WIDTH="228">9.11. Program: symirror</TD></TR></TABLE><HRALIGN="LEFT"WIDTH="684"TITLE="footer"><FONTSIZE="-1"></DIV<!-- LIBRARY NAV BAR --> <img src="../gifs/smnavbar.gif" usemap="#library-map" border="0" alt="Library Navigation Links"><p> <a href="copyrght.htm">Copyright &copy; 2002</a> O'Reilly &amp; 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 + -