📄 ch09_04.htm
字号:
<HTML><HEAD><TITLE>Recipe 9.3. Copying or Moving a File (Perl Cookbook)</TITLE><METANAME="DC.title"CONTENT="Perl Cookbook"><METANAME="DC.creator"CONTENT="Tom Christiansen & Nathan Torkington"><METANAME="DC.publisher"CONTENT="O'Reilly & Associates, Inc."><METANAME="DC.date"CONTENT="1999-07-02T01:39:06Z"><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_03.htm"TITLE="9.2. Deleting a File"><LINKREL="next"HREF="ch09_05.htm"TITLE="9.4. Recognizing Two Names for the Same File"></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_03.htm"TITLE="9.2. Deleting a File"><IMGSRC="../gifs/txtpreva.gif"ALT="Previous: 9.2. Deleting a File"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_05.htm"TITLE="9.4. Recognizing Two Names for the Same File"><IMGSRC="../gifs/txtnexta.gif"ALT="Next: 9.4. Recognizing Two Names for the Same File"BORDER="0"></A></TD></TR></TABLE></DIV><DIVCLASS="sect1"><H2CLASS="sect1"><ACLASS="title"NAME="ch09-44220">9.3. Copying or Moving a File</A></H2><DIVCLASS="sect2"><H3CLASS="sect2"><ACLASS="title"NAME="ch09-pgfId-366">Problem<ACLASS="indexterm"NAME="ch09-idx-1000004368-0"></A><ACLASS="indexterm"NAME="ch09-idx-1000004368-1"></A><ACLASS="indexterm"NAME="ch09-idx-1000004368-2"></A><ACLASS="indexterm"NAME="ch09-idx-1000004368-3"></A><ACLASS="indexterm"NAME="ch09-idx-1000004368-4"></A><ACLASS="indexterm"NAME="ch09-idx-1000004368-5"></A></A></H3><PCLASS="para">You need to copy a file, but Perl has no built-in copy command.</P></DIV><DIVCLASS="sect2"><H3CLASS="sect2"><ACLASS="title"NAME="ch09-pgfId-372">Solution</A></H3><PCLASS="para">Use the <CODECLASS="literal">copy</CODE> function from the standard File::Copy module:</P><PRECLASS="programlisting">use File::Copy;copy($oldfile, $newfile);</PRE><PCLASS="para">You can do it by hand:</P><PRECLASS="programlisting">open(IN, "< $oldfile") or die "can't open $oldfile: $!";open(OUT, "> $newfile") or die "can't open $newfile: $!";$blksize = (stat IN)[11] || 16384; # preferred block size?while ($len = sysread IN, $buf, $blksize) { if (!defined $len) { next if $! =~ /^Interrupted/; # ^Z and fg die "System read error: $!\n"; } $offset = 0; while ($len) { # Handle partial writes. defined($written = syswrite OUT, $buf, $len, $offset) or die "System write error: $!\n"; $len -= $written; $offset += $written; };}close(IN);close(OUT);</PRE><PCLASS="para">Or you can call your system's copy program:</P><PRECLASS="programlisting">system("cp $oldfile $newfile"); # unixsystem("copy $oldfile $newfile"); # dos, vms</PRE></DIV><DIVCLASS="sect2"><H3CLASS="sect2"><ACLASS="title"NAME="ch09-pgfId-430">Discussion</A></H3><PCLASS="para">The File::Copy module provides <CODECLASS="literal">copy</CODE> and <CODECLASS="literal">move</CODE> functions. These are more convenient than resorting to low-level I/O calls and more portable than calling <CODECLASS="literal">system</CODE>. <CODECLASS="literal">move</CODE> works across file-system boundaries; the standard Perl built-in <CODECLASS="literal">rename</CODE> (usually) does not.</P><PRECLASS="programlisting">use File::Copy;copy("datafile.dat", "datafile.bak") or die "copy failed: $!";move("datafile.new", "datafile.dat") or die "move failed: $!";</PRE><PCLASS="para">Because these functions return only a simple success status, you can't easily tell which file prevented the copy or move from being done. Copying the files manually lets you pinpoint which files didn't copy, but it fills your program with complex <CODECLASS="literal">sysread</CODE>s and <CODECLASS="literal">syswrite</CODE>s.<ACLASS="indexterm"NAME="ch09-idx-1000004370-0"></A><ACLASS="indexterm"NAME="ch09-idx-1000004370-1"></A><ACLASS="indexterm"NAME="ch09-idx-1000004370-2"></A><ACLASS="indexterm"NAME="ch09-idx-1000004370-3"></A><ACLASS="indexterm"NAME="ch09-idx-1000004370-4"></A><ACLASS="indexterm"NAME="ch09-idx-1000004370-5"></A></P></DIV><DIVCLASS="sect2"><H3CLASS="sect2"><ACLASS="title"NAME="ch09-pgfId-452">See Also</A></H3><PCLASS="para">Documentation for the standard File::Copy 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 <CODECLASS="literal">rename</CODE>, <CODECLASS="literal">read</CODE>, and <CODECLASS="literal">syswrite</CODE> functions in <ICLASS="filename">perlfunc</I> (1) and in <ACLASS="olink"HREF="../prog/ch03_01.htm">Chapter 3</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_03.htm"TITLE="9.2. Deleting a File"><IMGSRC="../gifs/txtpreva.gif"ALT="Previous: 9.2. Deleting a File"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_05.htm"TITLE="9.4. Recognizing Two Names for the Same File"><IMGSRC="../gifs/txtnexta.gif"ALT="Next: 9.4. Recognizing Two Names for the Same File"BORDER="0"></A></TD></TR><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="228">9.2. Deleting a File</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.4. Recognizing Two Names for the Same File</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 © 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 + -