File::Copy(3) Perl Programmers Reference Guide File::Copy(3) NAME File::Copy - Copy files or filehandles SYNOPSIS use File::Copy; copy("file1","file2") or die "Copy failed: $!"; copy("Copy.pm",\*STDOUT); move(3x,7,3x curs_move)("/dev1/fileA","/dev2/fileB"); use File::Copy "cp"; $n = FileHandle->new("/a/file(1,n)","r"); cp($n,"x"); DESCRIPTION The File::Copy module provides two basic functions, "copy" and "move(3x,7,3x curs_move)", which are useful for getting the contents of a file(1,n) from one place to another. The "copy" function takes two parameters: a file(1,n) to copy from and a file(1,n) to copy to. Either argument may be a string(3,n), a FileHandle ref- erence or a FileHandle glob. Obviously, if(3,n) the first argument is a filehandle of some sort(1,3), it will be read(2,n,1 builtins) from, and if(3,n) it is a file(1,n) name it will be opened for reading. Likewise, the second argument will be written to (and created if(3,n) need be). Trying to copy a file(1,n) on top of itself is a fatal error. Note that passing in(1,8) files as handles instead of names may lead to loss of information on some operating systems; it is recommended that you use file(1,n) names whenever possible. Files are opened in(1,8) binary mode where applicable. To get a consistent behaviour when copying from a filehandle to a file(1,n), use "binmode" on the filehan- dle. An optional third parameter can be used to specify the buffer size used for copying. This is the number of bytes from the first file(1,n), that wil be held in(1,8) memory at any given time(1,2,n), before being written to the second file. The default buffer size depends upon the file(1,n), but will generally be the whole file(1,n) (up to 2Mb), or 1k for file- handles that do not reference files (eg. sockets). You may use the syntax "use File::Copy "cp"" to get at the "cp" alias for this function. The syntax is exactly the same. The "move(3x,7,3x curs_move)" function also takes two parameters: the current name and the intended name of the file(1,n) to be moved. If the destination already exists and is a directory, and the source is not a direc- tory, then the source file(1,n) will be renamed into the directory spec- ified by the destination. If possible, move(3x,7,3x curs_move)() will simply rename(1,2,n) the file. Otherwise, it copies the file(1,n) to the new location and deletes the original. If an error(8,n) occurs during this copy-and-delete process, you may be left with a (possibly partial) copy of the file(1,n) under the destina- tion name. You may use the "mv" alias for this function in(1,8) the same way that you may use the "cp" alias for "copy". File::Copy also provides the "syscopy" routine, which copies the file(1,n) specified in(1,8) the first parameter to the file(1,n) specified in(1,8) the second parameter, preserving OS-specific attributes and file(1,n) structure. For Unix systems, this is equivalent to the simple "copy" routine, which doesn't preserve OS-specific attributes. For VMS systems, this calls the "rmscopy" routine (see below). For OS/2 systems, this calls the "syscopy" XSUB directly. For Win32 systems, this calls "Win32::Copy- File". On Mac OS (Classic), "syscopy" calls "Mac::MoreFiles::FSpFileCopy", if(3,n) available. Special behaviour if(3,n) "syscopy" is defined (OS/2, VMS and Win32) If both arguments to "copy" are not file(1,n) handles, then "copy" will per- form a "system copy" of the input file(1,n) to a new output file(1,n), in(1,8) order to preserve file(1,n) attributes, indexed file(1,n) structure, etc. The buffer size parameter is ignored. If either argument to "copy" is a handle to an opened file(1,n), then data is copied using Perl operators, and no effort is made to preserve file(1,n) attributes or record structure. The system copy routine may also be called directly under VMS and OS/2 as "File::Copy::syscopy" (or under VMS as "File::Copy::rmscopy", which is the routine that does the actual work for syscopy). rmscopy($from,$to[,$date_flag]) The first and second arguments may be strings, typeglobs, typeglob references, or objects inheriting from IO::Handle; they are used in(1,8) all cases to obtain the filespec of the input and output files, respectively. The name and type of the input file(1,n) are used as defaults for the output file(1,n), if(3,n) necessary. A new version(1,3,5) of the output file(1,n) is always created, which inherits the structure and RMS attributes of the input file(1,n), except for owner and protections (and possibly timestamps; see below). All data from the input file(1,n) is copied to the output file(1,n); if(3,n) either of the first two parameters to "rmscopy" is a file(1,n) handle, its posi- tion is unchanged. (Note that this means a file(1,n) handle pointing to the output file(1,n) will be associated with an old version(1,3,5) of that file(1,n) after "rmscopy" returns, not the newly created version.) The third parameter is an integer flag, which tells "rmscopy" how to handle timestamps. If it is < 0, none of the input file(1,n)'s time- stamps are propagated to the output file. If it is > 0, then it is interpreted as a bitmask: if(3,n) bit 0 (the LSB) is set(7,n,1 builtins), then time- stamps other than the revision date are propagated; if(3,n) bit 1 is set(7,n,1 builtins), the revision date is propagated. If the third parameter to "rmscopy" is 0, then it behaves much like the DCL COPY command: if(3,n) the name or type of the output file(1,n) was explicitly specified, then no timestamps are propagated, but if(3,n) they were taken implicitly from the input filespec, then all timestamps other than the revi- sion date are propagated. If this parameter is not supplied, it defaults to 0. Like "copy", "rmscopy" returns 1 on success. If an error(8,n) occurs, it sets $!, deletes the output file(1,n), and returns 0. RETURN All functions return 1 on success, 0 on failure. $! will be set(7,n,1 builtins) if(3,n) an error(8,n) was encountered. NOTES On Mac OS (Classic), the path separator is ':', not '/', and the current directory is denoted as ':', not '.'. You should be careful about specifying relative pathnames. While a full path always begins with a volume name, a relative pathname should always begin with a ':'. If specifying a volume name only, a trailing ':' is required. E.g. copy("file1", "tmp"); # creates the file(1,n) 'tmp' in(1,8) the current directory copy("file1", ":tmp:"); # creates :tmp:file1 copy("file1", ":tmp"); # same as above copy("file1", "tmp"); # same as above, if(3,n) 'tmp' is a directory (but don't do # that, since it may cause confusion, see example #1) copy("file1", "tmp:file1"); # error(8,n), since 'tmp:' is not a volume copy("file1", ":tmp:file1"); # ok, partial path copy("file1", "DataHD:"); # creates DataHD:file1 move(3x,7,3x curs_move)("MacintoshHD:fileA", "DataHD:fileB"); # moves (don't copies) files from one # volume to another AUTHOR File::Copy was written by Aaron Sherman <ajs@ajs.com> in(1,8) 1995, and updated by Charles Bailey <bailey@newman.upenn.edu> in(1,8) 1996. perl v5.8.5 2001-09-21 File::Copy(3)