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