Programming Perl

Programming PerlSearch this book
Previous: 7.2.28 File::CheckTree - Run Many Tests on a Collection of FilesChapter 7
The Standard Perl Library
Next: 7.2.30 File::Find - Traverse a File Tree
 

7.2.29 File::Copy - Copy Files or Filehandles

use File::Copy;

copy("src-file", "dst-file");
copy("Copy.pm", \*STDOUT);

use POSIX;
use File::Copy 'cp';

$fh = FileHandle->new("/dev/null", "r");
cp($fh, "dst-file");'

The Copy module provides one function, copy(), that takes two parameters: a file to copy from and a file to copy to. Either argument may be a string, a FileHandle reference, or a FileHandle glob. If the first argument is a filehandle of some sort, it will be read from; if it is a filename, it will be opened for reading. Likewise, the second argument will be written to (and created if need be).

An optional third parameter is a hint that requests the buffer size to be used for copying. This is the number of bytes from the first file that will be held in memory at any given time, before being written to the second file. The default buffer size depends upon the file and the operating system, but will generally be the whole file (up to 2Mb), or 1kb for filehandles that do not reference files (for example, sockets).

When running under VMS, this routine performs an RMS copy of the file, in order to preserve file attributes, indexed file structure, and so on. The buffer size parameter is ignored.

You may use the syntax:

use File::Copy "cp"

to get at the cp() alias for the copy() function. The syntax is exactly the same.

copy() returns 1 on success, 0 on failure; $! will be set if an error was encountered.


Previous: 7.2.28 File::CheckTree - Run Many Tests on a Collection of FilesProgramming PerlNext: 7.2.30 File::Find - Traverse a File Tree
7.2.28 File::CheckTree - Run Many Tests on a Collection of FilesBook Index7.2.30 File::Find - Traverse a File Tree