1N/A# source code has been placed in the public domain by the author. 1N/A# Please be kind and preserve the documentation. 1N/A# Additions copyright 1996 by Charles Bailey. Permission is granted 1N/A# to distribute the revised code under the same terms as Perl itself. 1N/A# Note that this module implements only *part* of the API defined by 1N/A# package has not yet been updated to work with Perl 5.004, and so it 1N/A# would be a Bad Thing for the CPAN module to grab it and replace this 1N/A# module. Therefore, we set this module's version higher than 2.0. 1N/Aif ($^O
eq 'MacOS') {
1N/A warn 'Mac::MoreFiles could not be loaded; using non-native syscopy' 1N/A if ($^O
eq 'MacOS') {
1N/A # a partial dir name that's valid only in the cwd (e.g. 'tmp') 1N/A croak(
"Usage: copy(FROM, TO [, BUFFERSIZE]) ")
1N/A unless(@_ ==
2 || @_ ==
3);
1N/A : (
ref(\$
to)
eq 'GLOB'));
1N/A if ($
from eq $
to) {
# works for references, too 1N/A croak(
"'$from' and '$to' are identical (not copied)");
1N/A !($^O
eq 'MSWin32' || $^O
eq 'os2' || $^O
eq 'vms')) {
1N/A croak(
"'$from' and '$to' are identical (not copied)");
1N/A croak(
"Bad buffer size for copy: $size\n")
unless ($
size >
0);
1N/A for ($w =
0; $w < $r; $w += $t) {
1N/A # Use this idiom to avoid uninitialized value warning. 1N/A # All of these contortions try to preserve error messages... 1N/A # will not rename with overwrite 1N/A # Did rename return an error even though it succeeded, because $to 1N/A # is on a remote NFS file system, and NFS lost the server's ack? 1N/Aif ($^O
eq 'MacOS') {
1N/A# &syscopy is an XSUB under OS/2 1N/A }
elsif ($^O
eq 'mpeix') {
1N/A return 0 unless @_ ==
2;
1N/A # Use the MPE cp program in order to 1N/A # preserve MPE file attributes. 1N/A return system(
'/bin/cp',
'-f', $_[
0], $_[
1]) ==
0;
1N/A }
elsif ($^O
eq 'MSWin32') {
1N/A return 0 unless @_ ==
2;
1N/A if ($
to =~ /(.*:)([^:]+):?$/) {
1N/AFile::Copy - Copy files or filehandles 1N/A copy("file1","file2") or die "Copy failed: $!"; 1N/AThe File::Copy module provides two basic functions, C<copy> and 1N/AC<move>, which are useful for getting the contents of a file from 1N/Aone place to another. 1N/AThe C<copy> function takes two 1N/Aparameters: a file to copy from and a file to copy to. Either 1N/Aargument may be a string, a FileHandle reference or a FileHandle 1N/Aglob. Obviously, if the first argument is a filehandle of some 1N/Asort, it will be read from, and if it is a file I<name> it will 1N/Abe opened for reading. Likewise, the second argument will be 1N/Awritten to (and created if need be). Trying to copy a file on top 1N/Aof itself is a fatal error. 1N/AB<Note that passing in 1N/Afiles as handles instead of names may lead to loss of information 1N/Aon some operating systems; it is recommended that you use file 1N/Anames whenever possible.> Files are opened in binary mode where 1N/Aapplicable. To get a consistent behaviour when copying from a 1N/Afilehandle to a file, use C<binmode> on the filehandle. 1N/AAn optional third parameter can be used to specify the buffer 1N/Asize used for copying. This is the number of bytes from the 1N/Afirst file, that wil be held in memory at any given time, before 1N/Abeing written to the second file. The default buffer size depends 1N/Aupon the file, but will generally be the whole file (up to 2Mb), or 1N/A1k for filehandles that do not reference files (eg. sockets). 1N/AYou may use the syntax C<use File::Copy "cp"> to get at the 1N/A"cp" alias for this function. The syntax is I<exactly> the same. 1N/AThe C<move> function also takes two parameters: the current name 1N/Aand the intended name of the file to be moved. If the destination 1N/Aalready exists and is a directory, and the source is not a 1N/Adirectory, then the source file will be renamed into the directory 1N/Aspecified by the destination. 1N/AIf possible, move() will simply rename the file. Otherwise, it copies 1N/Athe file to the new location and deletes the original. If an error occurs 1N/Aduring this copy-and-delete process, you may be left with a (possibly partial) 1N/Acopy of the file under the destination name. 1N/AYou may use the "mv" alias for this function in the same way that 1N/Ayou may use the "cp" alias for C<copy>. 1N/AFile::Copy also provides the C<syscopy> routine, which copies the 1N/Afile specified in the first parameter to the file specified in the 1N/Asecond parameter, preserving OS-specific attributes and file 1N/Astructure. For Unix systems, this is equivalent to the simple 1N/AC<copy> routine, which doesn't preserve OS-specific attributes. For 1N/AVMS systems, this calls the C<rmscopy> routine (see below). For OS/2 1N/Asystems, this calls the C<syscopy> XSUB directly. For Win32 systems, 1N/Athis calls C<Win32::CopyFile>. 1N/AOn Mac OS (Classic), C<syscopy> calls C<Mac::MoreFiles::FSpFileCopy>, 1N/A=head2 Special behaviour if C<syscopy> is defined (OS/2, VMS and Win32) 1N/AIf both arguments to C<copy> are not file handles, 1N/Athen C<copy> will perform a "system copy" of 1N/Athe input file to a new output file, in order to preserve file 1N/Aattributes, indexed file structure, I<etc.> The buffer size 1N/Aparameter is ignored. If either argument to C<copy> is a 1N/Ahandle to an opened file, then data is copied using Perl 1N/Aoperators, and no effort is made to preserve file attributes 1N/AThe system copy routine may also be called directly under VMS and OS/2 1N/Aas C<File::Copy::syscopy> (or under VMS as C<File::Copy::rmscopy>, which 1N/Ais the routine that does the actual work for syscopy). 1N/A=item rmscopy($from,$to[,$date_flag]) 1N/AThe first and second arguments may be strings, typeglobs, typeglob 1N/Areferences, or objects inheriting from IO::Handle; 1N/Athey are used in all cases to obtain the 1N/AI<filespec> of the input and output files, respectively. The 1N/Aname and type of the input file are used as defaults for the 1N/Aoutput file, if necessary. 1N/AA new version of the output file is always created, which 1N/Ainherits the structure and RMS attributes of the input file, 1N/Aexcept for owner and protections (and possibly timestamps; 1N/Asee below). All data from the input file is copied to the 1N/Aoutput file; if either of the first two parameters to C<rmscopy> 1N/Ais a file handle, its position is unchanged. (Note that this 1N/Ameans a file handle pointing to the output file will be 1N/Aassociated with an old version of that file after C<rmscopy> 1N/Areturns, not the newly created version.) 1N/AThe third parameter is an integer flag, which tells C<rmscopy> 1N/Ahow to handle timestamps. If it is E<lt> 0, none of the input file's 1N/Atimestamps are propagated to the output file. If it is E<gt> 0, then 1N/Ait is interpreted as a bitmask: if bit 0 (the LSB) is set, then 1N/Atimestamps other than the revision date are propagated; if bit 1 1N/Ais set, the revision date is propagated. If the third parameter 1N/Ato C<rmscopy> is 0, then it behaves much like the DCL COPY command: 1N/Aif the name or type of the output file was explicitly specified, 1N/Athen no timestamps are propagated, but if they were taken implicitly 1N/Afrom the input filespec, then all timestamps other than the 1N/Arevision date are propagated. If this parameter is not supplied, 1N/ALike C<copy>, C<rmscopy> returns 1 on success. If an error occurs, 1N/Ait sets C<$!>, deletes the output file, and returns 0. 1N/AAll functions return 1 on success, 0 on failure. 1N/A$! will be set if an error was encountered. 1N/AOn Mac OS (Classic), the path separator is ':', not '/', and the 1N/Acurrent directory is denoted as ':', not '.'. You should be careful 1N/Aabout specifying relative pathnames. While a full path always begins 1N/Awith a volume name, a relative pathname should always begin with a 1N/A':'. If specifying a volume name only, a trailing ':' is required. 1N/A copy("file1", "tmp"); # creates the file 'tmp' in the current directory 1N/A copy("file1", ":tmp:"); # creates :tmp:file1 1N/A copy("file1", ":tmp"); # same as above 1N/A copy("file1", "tmp"); # same as above, if 'tmp' is a directory (but don't do 1N/A # that, since it may cause confusion, see example #1) 1N/A copy("file1", "tmp:file1"); # error, since 'tmp:' is not a volume 1N/A copy("file1", ":tmp:file1"); # ok, partial path 1N/A copy("file1", "DataHD:"); # creates DataHD:file1 1N/A move("MacintoshHD:fileA", "DataHD:fileB"); # moves (don't copies) files from one 1N/AFile::Copy was written by Aaron Sherman I<E<lt>ajs@ajs.comE<gt>> in 1995, 1N/Aand updated by Charles Bailey I<E<lt>bailey@newman.upenn.eduE<gt>> in 1996.