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

📄 directories.mht

📁 Perl 编程技巧大全。适合初学者阅读。
💻 MHT
📖 第 1 页 / 共 5 页
字号:
From: <由 Microsoft Internet Explorer 5 保存>
Subject: Directories
Date: Wed, 29 Apr 2009 09:26:47 +0800
MIME-Version: 1.0
Content-Type: text/html;
	charset="gb2312"
Content-Transfer-Encoding: quoted-printable
Content-Location: http://pleac.sourceforge.net/pleac_perl/directories.html
X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2900.5579

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" =
"http://www.w3c.org/TR/1999/REC-html401-19991224/loose.dtd">
<HTML><HEAD><TITLE>Directories</TITLE>
<META http-equiv=3DContent-Type content=3D"text/html; charset=3Dgb2312">
<META content=3D"MSHTML 6.00.2900.5659" name=3DGENERATOR><LINK =
title=3DPLEAC-Perl=20
href=3D"index.html" rel=3DHOME><LINK title=3D"File Contents" =
href=3D"filecontents.html"=20
rel=3DPREVIOUS><LINK title=3DSubroutines href=3D"subroutines.html" =
rel=3DNEXT></HEAD>
<BODY class=3DSECT1 text=3D#cecece vLink=3D#d5ae83 aLink=3D#0000ff =
link=3D#f5deb3=20
bgColor=3D#4f6f6f>
<DIV class=3DNAVHEADER>
<TABLE cellSpacing=3D0 cellPadding=3D0 width=3D"100%"=20
summary=3D"Header navigation table" border=3D0>
  <TBODY>
  <TR>
    <TH align=3Dmiddle colSpan=3D3>PLEAC-Perl</TH></TR>
  <TR>
    <TD vAlign=3Dbottom align=3Dleft width=3D"10%"><A accessKey=3DP=20
      =
href=3D"http://pleac.sourceforge.net/pleac_perl/filecontents.html">Prev</=
A></TD>
    <TD vAlign=3Dbottom align=3Dmiddle width=3D"80%"></TD>
    <TD vAlign=3Dbottom align=3Dright width=3D"10%"><A accessKey=3DN=20
      =
href=3D"http://pleac.sourceforge.net/pleac_perl/subroutines.html">Next</A=
></TD></TR></TBODY></TABLE>
<HR align=3Dleft width=3D"100%">
</DIV>
<DIV class=3DSECT1>
<H1 class=3DSECT1><A name=3DDIRECTORIES>9. Directories</A></H1>
<DIV class=3DSECT2>
<H2 class=3DSECT2><A name=3DAEN495>Introduction</A></H2>
<TABLE width=3D"100%" bgColor=3D#2f4f4f border=3D0>
  <TBODY>
  <TR>
    <TD><PRE class=3DSCREEN><FONT color=3D#f5deb3 size=3D+1><FONT =
color=3D#bebebe>#-----------------------------
</FONT><FONT color=3D#cdad00>@entry</FONT> =3D <FONT =
color=3D#98fb98>stat</FONT><FONT color=3D#00ffff>(</FONT><FONT =
color=3D#00cd00>"/usr/bin/vi"</FONT><FONT color=3D#00ffff>)</FONT> <FONT =
color=3D#98fb98>or</FONT> <FONT color=3D#ffa500>die</FONT> <FONT =
color=3D#00cd00>"Couldn't stat /usr/bin/vi : $!"</FONT><FONT =
color=3D#00ffff>;</FONT>
<FONT color=3D#bebebe>#-----------------------------
</FONT><FONT color=3D#cdad00>@entry</FONT> =3D <FONT =
color=3D#98fb98>stat</FONT><FONT color=3D#00ffff>(</FONT><FONT =
color=3D#00cd00>"/usr/bin"</FONT><FONT color=3D#00ffff>)</FONT>    <FONT =
color=3D#98fb98>or</FONT> <FONT color=3D#ffa500>die</FONT> <FONT =
color=3D#00cd00>"Couldn't stat /usr/bin : $!"</FONT><FONT =
color=3D#00ffff>;</FONT>
<FONT color=3D#bebebe>#-----------------------------
</FONT><FONT color=3D#cdad00>@entry</FONT> =3D <FONT =
color=3D#98fb98>stat</FONT><FONT color=3D#00ffff>(</FONT>INFILE<FONT =
color=3D#00ffff>)</FONT>        <FONT color=3D#98fb98>or</FONT> <FONT =
color=3D#ffa500>die</FONT> <FONT color=3D#00cd00>"Couldn't stat INFILE : =
$!"</FONT><FONT color=3D#00ffff>;</FONT>
<FONT color=3D#bebebe>#-----------------------------
</FONT><FONT color=3D#ffa500>use</FONT> File::<FONT =
color=3D#98fb98>stat</FONT><FONT color=3D#00ffff>;</FONT>

$inode =3D <FONT color=3D#98fb98>stat</FONT><FONT =
color=3D#00ffff>(</FONT><FONT color=3D#00cd00>"/usr/bin/vi"</FONT><FONT =
color=3D#00ffff>);</FONT>
$ctime =3D $inode-&gt;ctime<FONT color=3D#00ffff>;</FONT>
$size  =3D $inode-&gt;size<FONT color=3D#00ffff>;</FONT>
<FONT color=3D#bebebe>#-----------------------------
</FONT><FONT color=3D#98fb98>open</FONT><FONT color=3D#00ffff>(</FONT> =
F, <FONT color=3D#00cd00>"&lt; $filename"</FONT> <FONT =
color=3D#00ffff>)</FONT>
    <FONT color=3D#98fb98>or</FONT> <FONT color=3D#ffa500>die</FONT> =
<FONT color=3D#00cd00>"Opening $filename: $!\n"</FONT><FONT =
color=3D#00ffff>;</FONT>
<FONT color=3D#ffa500>unless</FONT> <FONT color=3D#00ffff>(</FONT><FONT =
color=3D#b2dfee>-</FONT><FONT color=3D#ff7f50>s</FONT> F &amp;&amp; =
<FONT color=3D#b2dfee>-T</FONT> _<FONT color=3D#00ffff>)</FONT> <FONT =
color=3D#00ffff>{</FONT>
    <FONT color=3D#ffa500>die</FONT> <FONT color=3D#00cd00>"$filename =
doesn't have text in it.\n"</FONT><FONT color=3D#00ffff>;</FONT>
<FONT color=3D#00ffff>}</FONT>
<FONT color=3D#bebebe>#-----------------------------
</FONT><FONT color=3D#98fb98>opendir</FONT><FONT =
color=3D#00ffff>(</FONT>DIRHANDLE, <FONT =
color=3D#00cd00>"/usr/bin"</FONT><FONT color=3D#00ffff>)</FONT> <FONT =
color=3D#98fb98>or</FONT> <FONT color=3D#ffa500>die</FONT> <FONT =
color=3D#00cd00>"couldn't open /usr/bin : $!"</FONT><FONT =
color=3D#00ffff>;</FONT>
<FONT color=3D#ffa500>while</FONT> <FONT color=3D#00ffff>(</FONT> <FONT =
color=3D#ff7f50>defined</FONT> <FONT color=3D#00ffff>(</FONT>$filename =
=3D <FONT color=3D#98fb98>readdir</FONT><FONT =
color=3D#00ffff>(</FONT>DIRHANDLE<FONT color=3D#00ffff>))</FONT> <FONT =
color=3D#00ffff>)</FONT> <FONT color=3D#00ffff>{</FONT>
    <FONT color=3D#ff7f50>print</FONT> <FONT color=3D#00cd00>"Inside =
/usr/bin is something called $filename\n"</FONT><FONT =
color=3D#00ffff>;</FONT>
<FONT color=3D#00ffff>}</FONT>
<FONT color=3D#98fb98>closedir</FONT><FONT =
color=3D#00ffff>(</FONT>DIRHANDLE<FONT color=3D#00ffff>);</FONT>
<FONT color=3D#bebebe>#-----------------------------
</FONT></PRE></FONT></TD></TR></TBODY></TABLE></DIV>
<DIV class=3DSECT2>
<H2 class=3DSECT2><A name=3DAEN498>Getting and Setting =
Timestamps</A></H2>
<TABLE width=3D"100%" bgColor=3D#2f4f4f border=3D0>
  <TBODY>
  <TR>
    <TD><PRE class=3DSCREEN><FONT color=3D#f5deb3 size=3D+1><FONT =
color=3D#bebebe>#-----------------------------
</FONT><FONT color=3D#00ffff>(</FONT>$READTIME, $WRITETIME<FONT =
color=3D#00ffff>)</FONT> =3D <FONT color=3D#00ffff>(</FONT><FONT =
color=3D#98fb98>stat</FONT><FONT color=3D#00ffff>(</FONT>$filename<FONT =
color=3D#00ffff>))[</FONT><FONT color=3D#cdcd00>8</FONT>,<FONT =
color=3D#cdcd00>9</FONT><FONT color=3D#00ffff>];</FONT>

<FONT color=3D#98fb98>utime</FONT><FONT =
color=3D#00ffff>(</FONT>$NEWREADTIME, $NEWWRITETIME, $filename<FONT =
color=3D#00ffff>);</FONT>
<FONT color=3D#bebebe>#-----------------------------
</FONT>$SECONDS_PER_DAY =3D <FONT color=3D#cdcd00>60</FONT> * <FONT =
color=3D#cdcd00>60</FONT> * <FONT color=3D#cdcd00>24</FONT><FONT =
color=3D#00ffff>;</FONT>
<FONT color=3D#00ffff>(</FONT>$atime, $mtime<FONT =
color=3D#00ffff>)</FONT> =3D <FONT color=3D#00ffff>(</FONT><FONT =
color=3D#98fb98>stat</FONT><FONT color=3D#00ffff>(</FONT>$file<FONT =
color=3D#00ffff>))[</FONT><FONT color=3D#cdcd00>8</FONT>,<FONT =
color=3D#cdcd00>9</FONT><FONT color=3D#00ffff>];</FONT>
$atime -=3D <FONT color=3D#cdcd00>7</FONT> * $SECONDS_PER_DAY<FONT =
color=3D#00ffff>;</FONT>
$mtime -=3D <FONT color=3D#cdcd00>7</FONT> * $SECONDS_PER_DAY<FONT =
color=3D#00ffff>;</FONT>

<FONT color=3D#98fb98>utime</FONT><FONT color=3D#00ffff>(</FONT>$atime, =
$mtime, $file<FONT color=3D#00ffff>)</FONT>
    <FONT color=3D#98fb98>or</FONT> <FONT color=3D#ffa500>die</FONT> =
<FONT color=3D#00cd00>"couldn't backdate $file by a week w/ utime: =
$!"</FONT><FONT color=3D#00ffff>;</FONT>
<FONT color=3D#bebebe>#-----------------------------
</FONT>$mtime =3D <FONT color=3D#00ffff>(</FONT><FONT =
color=3D#98fb98>stat</FONT> $file<FONT color=3D#00ffff>)[</FONT><FONT =
color=3D#cdcd00>9</FONT><FONT color=3D#00ffff>];</FONT>
<FONT color=3D#98fb98>utime</FONT><FONT color=3D#00ffff>(</FONT><FONT =
color=3D#98fb98>time</FONT>, $mtime, $file<FONT =
color=3D#00ffff>);</FONT>
<FONT color=3D#bebebe>#-----------------------------
</FONT><FONT color=3D#ffa500>use</FONT> File::<FONT =
color=3D#98fb98>stat</FONT><FONT color=3D#00ffff>;</FONT>
<FONT color=3D#98fb98>utime</FONT><FONT color=3D#00ffff>(</FONT><FONT =
color=3D#98fb98>time</FONT>, <FONT color=3D#98fb98>stat</FONT><FONT =
color=3D#00ffff>(</FONT>$file<FONT color=3D#00ffff>)</FONT>-&gt;mtime, =
$file<FONT color=3D#00ffff>);</FONT>
<FONT color=3D#bebebe>#-----------------------------
# <FONT size=3D-1><A =
href=3D"http://pleac.sourceforge.net/include/perl/ch09/uvi">download the =
following standalone program</A></FONT>
#!/usr/bin/perl -w
# uvi - vi a file without changing its access times
</FONT>
$file =3D <FONT color=3D#ff7f50>shift</FONT> <FONT =
color=3D#98fb98>or</FONT> <FONT color=3D#ffa500>die</FONT> <FONT =
color=3D#00cd00>"usage: uvi filename\n"</FONT><FONT =
color=3D#00ffff>;</FONT>
<FONT color=3D#00ffff>(</FONT>$atime, $mtime<FONT =
color=3D#00ffff>)</FONT> =3D <FONT color=3D#00ffff>(</FONT><FONT =
color=3D#98fb98>stat</FONT><FONT color=3D#00ffff>(</FONT>$file<FONT =
color=3D#00ffff>))[</FONT><FONT color=3D#cdcd00>8</FONT>,<FONT =
color=3D#cdcd00>9</FONT><FONT color=3D#00ffff>];</FONT>
<FONT color=3D#98fb98>system</FONT><FONT color=3D#00ffff>(</FONT><FONT =
color=3D#cdcd00>$ENV</FONT><FONT color=3D#00ffff>{</FONT><FONT =
color=3D#00cd00>EDITOR</FONT><FONT color=3D#00ffff>}</FONT> || <FONT =
color=3D#00cd00>"vi"</FONT>, $file<FONT color=3D#00ffff>);</FONT>
<FONT color=3D#98fb98>utime</FONT><FONT color=3D#00ffff>(</FONT>$atime, =
$mtime, $file<FONT color=3D#00ffff>)</FONT>
    <FONT color=3D#98fb98>or</FONT> <FONT color=3D#ffa500>die</FONT> =
<FONT color=3D#00cd00>"couldn't restore $file to orig times: =
$!"</FONT><FONT color=3D#00ffff>;</FONT>

<FONT color=3D#bebebe>#-----------------------------
</FONT></PRE></FONT></TD></TR></TBODY></TABLE></DIV>
<DIV class=3DSECT2>
<H2 class=3DSECT2><A name=3DAEN501>Deleting a File</A></H2>
<TABLE width=3D"100%" bgColor=3D#2f4f4f border=3D0>
  <TBODY>
  <TR>
    <TD><PRE class=3DSCREEN><FONT color=3D#f5deb3 size=3D+1><FONT =
color=3D#bebebe>#-----------------------------
</FONT><FONT color=3D#98fb98>unlink</FONT><FONT =
color=3D#00ffff>(</FONT>$FILENAME<FONT color=3D#00ffff>)</FONT>          =
       <FONT color=3D#98fb98>or</FONT> <FONT color=3D#ffa500>die</FONT> =
<FONT color=3D#00cd00>"Can't delete $FILENAME: $!\n"</FONT><FONT =
color=3D#00ffff>;</FONT>
<FONT color=3D#98fb98>unlink</FONT><FONT color=3D#00ffff>(</FONT><FONT =
color=3D#cdad00>@FILENAMES</FONT><FONT color=3D#00ffff>)</FONT> =3D=3D =
<FONT color=3D#cdad00>@FILENAMES</FONT>  <FONT color=3D#98fb98>or</FONT> =
<FONT color=3D#ffa500>die</FONT> <FONT color=3D#00cd00>"Couldn't unlink =
all of </FONT><FONT color=3D#cdad00>@FILENAMES:</FONT><FONT =
color=3D#00cd00> $!\n"</FONT><FONT color=3D#00ffff>;</FONT>
<FONT color=3D#bebebe>#-----------------------------
</FONT><FONT color=3D#98fb98>unlink</FONT><FONT =
color=3D#00ffff>(</FONT>$file<FONT color=3D#00ffff>)</FONT> <FONT =
color=3D#98fb98>or</FONT> <FONT color=3D#ffa500>die</FONT> <FONT =
color=3D#00cd00>"Can't unlink $file: $!"</FONT><FONT =
color=3D#00ffff>;</FONT>
<FONT color=3D#bebebe>#-----------------------------
</FONT><FONT color=3D#ffa500>unless</FONT> <FONT =
color=3D#00ffff>((</FONT>$count =3D <FONT =
color=3D#98fb98>unlink</FONT><FONT color=3D#00ffff>(</FONT><FONT =
color=3D#cdad00>@filelist</FONT><FONT color=3D#00ffff>))</FONT> =3D=3D =
<FONT color=3D#cdad00>@filelist</FONT><FONT color=3D#00ffff>)</FONT> =
<FONT color=3D#00ffff>{</FONT>
    <FONT color=3D#98fb98>warn</FONT> <FONT color=3D#00cd00>"could only =
delete $count of "</FONT>
            . <FONT color=3D#00ffff>(</FONT><FONT =
color=3D#cdad00>@filelist</FONT><FONT color=3D#00ffff>)</FONT> . <FONT =
color=3D#00cd00>" files"</FONT><FONT color=3D#00ffff>;</FONT>
<FONT color=3D#00ffff>}</FONT>
<FONT color=3D#bebebe>#-----------------------------
</FONT></PRE></FONT></TD></TR></TBODY></TABLE></DIV>
<DIV class=3DSECT2>
<H2 class=3DSECT2><A name=3DAEN504>Copying or Moving a File</A></H2>
<TABLE width=3D"100%" bgColor=3D#2f4f4f border=3D0>
  <TBODY>
  <TR>
    <TD><PRE class=3DSCREEN><FONT color=3D#f5deb3 size=3D+1><FONT =
color=3D#bebebe>#-----------------------------
</FONT><FONT color=3D#ffa500>use</FONT> <FONT =
color=3D#b2dfee>File::Copy</FONT><FONT color=3D#00ffff>;</FONT>
copy<FONT color=3D#00ffff>(</FONT>$oldfile, $newfile<FONT =
color=3D#00ffff>);</FONT>
<FONT color=3D#bebebe>#-----------------------------
</FONT><FONT color=3D#98fb98>open</FONT><FONT =
color=3D#00ffff>(</FONT>IN,  <FONT color=3D#00cd00>"&lt; =
$oldfile"</FONT><FONT color=3D#00ffff>)</FONT>                     <FONT =
color=3D#98fb98>or</FONT> <FONT color=3D#ffa500>die</FONT> <FONT =
color=3D#00cd00>"can't open $oldfile: $!"</FONT><FONT =
color=3D#00ffff>;</FONT>
<FONT color=3D#98fb98>open</FONT><FONT color=3D#00ffff>(</FONT>OUT, =
<FONT color=3D#00cd00>"&gt; $newfile"</FONT><FONT =
color=3D#00ffff>)</FONT>                     <FONT =
color=3D#98fb98>or</FONT> <FONT color=3D#ffa500>die</FONT> <FONT =
color=3D#00cd00>"can't open $newfile: $!"</FONT><FONT =
color=3D#00ffff>;</FONT>

$blksize =3D <FONT color=3D#00ffff>(</FONT><FONT =
color=3D#98fb98>stat</FONT> IN<FONT color=3D#00ffff>)[</FONT><FONT =

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -