1File::Copy(3pm) Perl Programmers Reference Guide File::Copy(3pm)
2
3
4
6 File::Copy - Copy files or filehandles
7
9 use File::Copy;
10
11 copy("sourcefile", "destinationfile") or die "Copy failed: $!";
12 copy("Copy.pm", \*STDOUT);
13 move("/dev1/sourcefile", "/dev2/destinationfile");
14
15 use File::Copy "cp";
16
17 my $n = FileHandle->new("/a/file", "r");
18 cp($n, "x");
19
21 The File::Copy module provides two basic functions, "copy" and "move",
22 which are useful for getting the contents of a file from one place to
23 another.
24
25 copy
26 The "copy" function takes two parameters: a file to copy from and a
27 file to copy to. Either argument may be a string, a FileHandle
28 reference or a FileHandle glob. Obviously, if the first argument is
29 a filehandle of some sort, it will be read from, and if it is a
30 file name it will be opened for reading. Likewise, the second
31 argument will be written to. If the second argument does not exist
32 but the parent directory does exist, then it will be created.
33 Trying to copy a file into a non-existent directory is an error.
34 Trying to copy a file on top of itself is also an error. "copy"
35 will not overwrite read-only files.
36
37 If the destination (second argument) already exists and is a
38 directory, and the source (first argument) is not a filehandle,
39 then the source file will be copied into the directory specified by
40 the destination, using the same base name as the source file. It's
41 a failure to have a filehandle as the source when the destination
42 is a directory.
43
44 Note that passing in files as handles instead of names may lead to
45 loss of information on some operating systems; it is recommended
46 that you use file names whenever possible. Files are opened in
47 binary mode where applicable. To get a consistent behaviour when
48 copying from a filehandle to a file, use "binmode" on the
49 filehandle.
50
51 An optional third parameter can be used to specify the buffer size
52 used for copying. This is the number of bytes from the first file,
53 that will be held in memory at any given time, before being written
54 to the second file. The default buffer size depends upon the file,
55 but will generally be the whole file (up to 2MB), or 1k for
56 filehandles that do not reference files (eg. sockets).
57
58 You may use the syntax "use File::Copy "cp"" to get at the "cp"
59 alias for this function. The syntax is exactly the same. The
60 behavior is nearly the same as well: as of version 2.15, "cp" will
61 preserve the source file's permission bits like the shell utility
62 cp(1) would do with default options, while "copy" uses the default
63 permissions for the target file (which may depend on the process'
64 "umask", file ownership, inherited ACLs, etc.). That is, if the
65 destination file already exists, "cp" will leave its permissions
66 unchanged; otherwise the permissions are taken from the source file
67 and modified by the "umask". If an error occurs in setting
68 permissions, "cp" will return 0, regardless of whether the file was
69 successfully copied.
70
71 move
72 The "move" function also takes two parameters: the current name and
73 the intended name of the file to be moved. If the destination
74 already exists and is a directory, and the source is not a
75 directory, then the source file will be renamed into the directory
76 specified by the destination.
77
78 If possible, move() will simply rename the file. Otherwise, it
79 copies the file to the new location and deletes the original. If
80 an error occurs during this copy-and-delete process, you may be
81 left with a (possibly partial) copy of the file under the
82 destination name.
83
84 You may use the "mv" alias for this function in the same way that
85 you may use the "cp" alias for "copy".
86
87 syscopy
88 File::Copy also provides the "syscopy" routine, which copies the
89 file specified in the first parameter to the file specified in the
90 second parameter, preserving OS-specific attributes and file
91 structure. For Unix systems, this is equivalent to the simple
92 "copy" routine, which doesn't preserve OS-specific attributes. For
93 VMS systems, this calls the "rmscopy" routine (see below). For
94 OS/2 systems, this calls the "syscopy" XSUB directly. For Win32
95 systems, this calls "Win32::CopyFile".
96
97 Special behaviour if "syscopy" is defined (OS/2, VMS and Win32):
98
99 If both arguments to "copy" are not file handles, then "copy" will
100 perform a "system copy" of the input file to a new output file, in
101 order to preserve file attributes, indexed file structure, etc.
102 The buffer size parameter is ignored. If either argument to "copy"
103 is a handle to an opened file, then data is copied using Perl
104 operators, and no effort is made to preserve file attributes or
105 record structure.
106
107 The system copy routine may also be called directly under VMS and
108 OS/2 as "File::Copy::syscopy" (or under VMS as
109 "File::Copy::rmscopy", which is the routine that does the actual
110 work for syscopy).
111
112 rmscopy($from,$to[,$date_flag])
113 The first and second arguments may be strings, typeglobs, typeglob
114 references, or objects inheriting from IO::Handle; they are used in
115 all cases to obtain the filespec of the input and output files,
116 respectively. The name and type of the input file are used as
117 defaults for the output file, if necessary.
118
119 A new version of the output file is always created, which inherits
120 the structure and RMS attributes of the input file, except for
121 owner and protections (and possibly timestamps; see below). All
122 data from the input file is copied to the output file; if either of
123 the first two parameters to "rmscopy" is a file handle, its
124 position is unchanged. (Note that this means a file handle
125 pointing to the output file will be associated with an old version
126 of that file after "rmscopy" returns, not the newly created
127 version.)
128
129 The third parameter is an integer flag, which tells "rmscopy" how
130 to handle timestamps. If it is < 0, none of the input file's
131 timestamps are propagated to the output file. If it is > 0, then
132 it is interpreted as a bitmask: if bit 0 (the LSB) is set, then
133 timestamps other than the revision date are propagated; if bit 1 is
134 set, the revision date is propagated. If the third parameter to
135 "rmscopy" is 0, then it behaves much like the DCL COPY command: if
136 the name or type of the output file was explicitly specified, then
137 no timestamps are propagated, but if they were taken implicitly
138 from the input filespec, then all timestamps other than the
139 revision date are propagated. If this parameter is not supplied,
140 it defaults to 0.
141
142 "rmscopy" is VMS specific and cannot be exported; it must be
143 referenced by its full name, e.g.:
144
145 File::Copy::rmscopy($from, $to) or die $!;
146
147 Like "copy", "rmscopy" returns 1 on success. If an error occurs,
148 it sets $!, deletes the output file, and returns 0.
149
151 All functions return 1 on success, 0 on failure. $! will be set if an
152 error was encountered.
153
155 Before calling copy() or move() on a filehandle, the caller should
156 close or flush() the file to avoid writes being lost. Note that this is
157 the case even for move(), because it may actually copy the file,
158 depending on the OS-specific implementation, and the underlying
159 filesystem(s).
160
162 File::Copy was written by Aaron Sherman <ajs@ajs.com> in 1995, and
163 updated by Charles Bailey <bailey@newman.upenn.edu> in 1996.
164
165
166
167perl v5.38.2 2023-11-30 File::Copy(3pm)