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("file1","file2") or die "Copy failed: $!";
12 copy("Copy.pm",\*STDOUT);
13 move("/dev1/fileA","/dev2/fileB");
14
15 use File::Copy "cp";
16
17 $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 (and created if need be). Trying to
32 copy a file on top of itself is a fatal error.
33
34 Note that passing in files as handles instead of names may lead to
35 loss of information on some operating systems; it is recommended
36 that you use file names whenever possible. Files are opened in
37 binary mode where applicable. To get a consistent behaviour when
38 copying from a filehandle to a file, use "binmode" on the
39 filehandle.
40
41 An optional third parameter can be used to specify the buffer size
42 used for copying. This is the number of bytes from the first file,
43 that will be held in memory at any given time, before being written
44 to the second file. The default buffer size depends upon the file,
45 but will generally be the whole file (up to 2MB), or 1k for
46 filehandles that do not reference files (eg. sockets).
47
48 You may use the syntax "use File::Copy "cp"" to get at the "cp"
49 alias for this function. The syntax is exactly the same.
50
51 move
52 The "move" function also takes two parameters: the current name and
53 the intended name of the file to be moved. If the destination
54 already exists and is a directory, and the source is not a
55 directory, then the source file will be renamed into the directory
56 specified by the destination.
57
58 If possible, move() will simply rename the file. Otherwise, it
59 copies the file to the new location and deletes the original. If
60 an error occurs during this copy-and-delete process, you may be
61 left with a (possibly partial) copy of the file under the
62 destination name.
63
64 You may use the "mv" alias for this function in the same way that
65 you may use the "cp" alias for "copy".
66
67 syscopy
68 File::Copy also provides the "syscopy" routine, which copies the
69 file specified in the first parameter to the file specified in the
70 second parameter, preserving OS-specific attributes and file
71 structure. For Unix systems, this is equivalent to the simple
72 "copy" routine, which doesn't preserve OS-specific attributes. For
73 VMS systems, this calls the "rmscopy" routine (see below). For
74 OS/2 systems, this calls the "syscopy" XSUB directly. For Win32
75 systems, this calls "Win32::CopyFile".
76
77 On Mac OS (Classic), "syscopy" calls "Mac::MoreFiles::FSpFileCopy",
78 if available.
79
80 Special behaviour if "syscopy" is defined (OS/2, VMS and Win32):
81
82 If both arguments to "copy" are not file handles, then "copy" will
83 perform a "system copy" of the input file to a new output file, in
84 order to preserve file attributes, indexed file structure, etc.
85 The buffer size parameter is ignored. If either argument to "copy"
86 is a handle to an opened file, then data is copied using Perl
87 operators, and no effort is made to preserve file attributes or
88 record structure.
89
90 The system copy routine may also be called directly under VMS and
91 OS/2 as "File::Copy::syscopy" (or under VMS as
92 "File::Copy::rmscopy", which is the routine that does the actual
93 work for syscopy).
94
95 rmscopy($from,$to[,$date_flag])
96 The first and second arguments may be strings, typeglobs, typeglob
97 references, or objects inheriting from IO::Handle; they are used in
98 all cases to obtain the filespec of the input and output files,
99 respectively. The name and type of the input file are used as
100 defaults for the output file, if necessary.
101
102 A new version of the output file is always created, which inherits
103 the structure and RMS attributes of the input file, except for
104 owner and protections (and possibly timestamps; see below). All
105 data from the input file is copied to the output file; if either of
106 the first two parameters to "rmscopy" is a file handle, its
107 position is unchanged. (Note that this means a file handle
108 pointing to the output file will be associated with an old version
109 of that file after "rmscopy" returns, not the newly created
110 version.)
111
112 The third parameter is an integer flag, which tells "rmscopy" how
113 to handle timestamps. If it is < 0, none of the input file's
114 timestamps are propagated to the output file. If it is > 0, then
115 it is interpreted as a bitmask: if bit 0 (the LSB) is set, then
116 timestamps other than the revision date are propagated; if bit 1 is
117 set, the revision date is propagated. If the third parameter to
118 "rmscopy" is 0, then it behaves much like the DCL COPY command: if
119 the name or type of the output file was explicitly specified, then
120 no timestamps are propagated, but if they were taken implicitly
121 from the input filespec, then all timestamps other than the
122 revision date are propagated. If this parameter is not supplied,
123 it defaults to 0.
124
125 Like "copy", "rmscopy" returns 1 on success. If an error occurs,
126 it sets $!, deletes the output file, and returns 0.
127
129 All functions return 1 on success, 0 on failure. $! will be set if an
130 error was encountered.
131
133 ยท On Mac OS (Classic), the path separator is ':', not '/', and the
134 current directory is denoted as ':', not '.'. You should be careful
135 about specifying relative pathnames. While a full path always
136 begins with a volume name, a relative pathname should always begin
137 with a ':'. If specifying a volume name only, a trailing ':' is
138 required.
139
140 E.g.
141
142 copy("file1", "tmp"); # creates the file 'tmp' in the current directory
143 copy("file1", ":tmp:"); # creates :tmp:file1
144 copy("file1", ":tmp"); # same as above
145 copy("file1", "tmp"); # same as above, if 'tmp' is a directory (but don't do
146 # that, since it may cause confusion, see example #1)
147 copy("file1", "tmp:file1"); # error, since 'tmp:' is not a volume
148 copy("file1", ":tmp:file1"); # ok, partial path
149 copy("file1", "DataHD:"); # creates DataHD:file1
150
151 move("MacintoshHD:fileA", "DataHD:fileB"); # moves (doesn't copy) files from one
152 # volume to another
153
155 File::Copy was written by Aaron Sherman <ajs@ajs.com> in 1995, and
156 updated by Charles Bailey <bailey@newman.upenn.edu> in 1996.
157
158
159
160perl v5.10.1 2009-04-25 File::Copy(3pm)