📄 ch32_20.htm
字号:
<html><head><title>File::Copy (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="File::Copy"><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="ch32_19.htm"><img src="../gifs/txtpreva.gif" alt="Previous" border="0"></a></td><td align="center" valign="top" width="171"><a href="ch32_01.htm">Chapter 32: Standard Modules</a></td><td align="right" valign="top" width="172"><a href="ch32_21.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">32.20. File::Copy</h2><p><blockquote><pre class="programlisting">use File::Copy;copy("/tmp/fileA", "/tmp/fileA.orig") or die "copy failed: $!";copy("/etc/motd", *STDOUT) or die "copy failed: $!";move("/tmp/fileA", "/tmp/fileB") or die "move failed: $!";use File::Copy qw/cp mv/; # Get normal Unix names.cp "/tmp/fileA", "/tmp/fileA.orig" or die "copy failed: $!";mv "/tmp/fileA", "/tmp/fileB" or die "move failed: $!";</pre></blockquote>The <tt class="literal">File::Copy</tt> module exports two functions,<tt class="literal">copy</tt> and <tt class="literal">move</tt>, that respectivelycopy or rename their first argument to their second argument, similarto calling the Unix <em class="emphasis">cp</em>(1) and<em class="emphasis">mv</em>(1) commands (names you may use if youimport them explicitly). The <tt class="literal">copy</tt> function alsoaccepts filehandles as arguments. These functions return true whenthey work and false when they fail, setting <tt class="literal">$!</tt>(<tt class="literal">$OS_ERROR</tt>) as appropriate. (Unfortunately, youcan't tell whether something like "Permission denied" applies to thefirst file or to the second one.) These functions are something of acompromise between convenience and precision. They do not support thenumerous options and optimizations found in<em class="emphasis">cp</em>(1) and<em class="emphasis">mv</em>(1), such as recursive copying,automatic backups, retention of original timestamps and ownershipinformation, and interactive confirmation. If you need any of thosefeatures, it's probably best to call your platform's versions of thosecommands.<a href="#FOOTNOTE-2">[2]</a> Just realize that not all systemssupport the same commands or use the same options for them.<blockquote><pre class="programlisting">system("cp -R -pi /tmp/dir1 /tmp/dir2") == 0 or die "external cp command status was $?";</pre></blockquote></p><blockquote class="footnote"><a name="FOOTNOTE-2"></a><p>[2]Or get the PPT versions if your platform istool-challenged.</p></blockquote><!-- 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="ch32_19.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="ch32_21.htm"><img src="../gifs/txtnexta.gif" alt="Next" border="0"></a></td></tr><tr><td align="left" valign="top" width="172">32.19. File::Compare</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">32.21. File::Find</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 + -