Finalize change #4232.
[p5sagit/p5-mst-13.2.git] / lib / File / Copy.pm
index 70c5eb8..fd812bc 100644 (file)
@@ -9,7 +9,6 @@ package File::Copy;
 
 use strict;
 use Carp;
-use UNIVERSAL qw(isa);
 use vars qw(@ISA @EXPORT @EXPORT_OK $VERSION $Too_Big
            &copy &syscopy &cp &mv);
 
@@ -18,7 +17,7 @@ use vars qw(@ISA @EXPORT @EXPORT_OK $VERSION $Too_Big
 # package has not yet been updated to work with Perl 5.004, and so it
 # would be a Bad Thing for the CPAN module to grab it and replace this
 # module.  Therefore, we set this module's version higher than 2.0.
-$VERSION = '2.01';
+$VERSION = '2.02';
 
 require Exporter;
 @ISA = qw(Exporter);
@@ -48,11 +47,13 @@ sub copy {
 
     my $from_a_handle = (ref($from)
                         ? (ref($from) eq 'GLOB'
-                           || isa($from, 'GLOB') || isa($from, 'IO::Handle'))
+                           || UNIVERSAL::isa($from, 'GLOB')
+                            || UNIVERSAL::isa($from, 'IO::Handle'))
                         : (ref(\$from) eq 'GLOB'));
     my $to_a_handle =   (ref($to)
                         ? (ref($to) eq 'GLOB'
-                           || isa($to, 'GLOB') || isa($to, 'IO::Handle'))
+                           || UNIVERSAL::isa($to, 'GLOB')
+                            || UNIVERSAL::isa($to, 'IO::Handle'))
                         : (ref(\$to) eq 'GLOB'));
 
     if (!$from_a_handle && !$to_a_handle && -d $to && ! -d $from) {
@@ -60,8 +61,11 @@ sub copy {
     }
 
     if (defined &syscopy && \&syscopy != \&copy
-       && $from_a_handle
-       && ($to_a_handle || $^O eq 'os2'))
+       && !$to_a_handle
+       && !($from_a_handle && $^O eq 'os2' )   # OS/2 cannot handle handles
+       && !($from_a_handle && $^O eq 'mpeix')  # and neither can MPE/iX.
+       && !($from_a_handle && $^O eq 'MSWin32')
+       )       
     {
        return syscopy($from, $to);
     }
@@ -146,6 +150,10 @@ sub move {
 
     ($tosz1,$tomt1) = (stat($to))[7,9];
     $fromsz = -s $from;
+    if ($^O eq 'os2' and defined $tosz1 and defined $fromsz) {
+      # will not rename with overwrite
+      unlink $to;
+    }
     return 1 if rename $from, $to;
 
     ($sts,$ossts) = ($! + 0, $^E + 0);
@@ -169,7 +177,25 @@ sub move {
 *mv = \&move;
 
 # &syscopy is an XSUB under OS/2
-*syscopy = ($^O eq 'VMS' ? \&rmscopy : \&copy) unless defined &syscopy;
+unless (defined &syscopy) {
+    if ($^O eq 'VMS') {
+       *syscopy = \&rmscopy;
+    } elsif ($^O eq 'mpeix') {
+       *syscopy = sub {
+           return 0 unless @_ == 2;
+           # Use the MPE cp program in order to
+           # preserve MPE file attributes.
+           return system('/bin/cp', '-f', $_[0], $_[1]) == 0;
+       };
+    } elsif ($^O eq 'MSWin32') {
+       *syscopy = sub {
+           return 0 unless @_ == 2;
+           return Win32::CopyFile(@_, 1);
+       };
+    } else {
+       *syscopy = \©
+    }
+}
 
 1;
 
@@ -209,14 +235,14 @@ argument may be a string, a FileHandle reference or a FileHandle
 glob. Obviously, if the first argument is a filehandle of some
 sort, it will be read from, and if it is a file I<name> it will
 be opened for reading. Likewise, the second argument will be
-written to (and created if need be).  If the second argument is
-a file name and specifies an existing directory, and the first
-argument does not specify
+written to (and created if need be).
 
 B<Note that passing in
 files as handles instead of names may lead to loss of information
 on some operating systems; it is recommended that you use file
-names whenever possible.>
+names whenever possible.>  Files are opened in binary mode where
+applicable.  To get a consistent behaviour when copying from a
+filehandle to a file, use C<binmode> on the filehandle.
 
 An optional third parameter can be used to specify the buffer
 size used for copying. This is the number of bytes from the
@@ -252,9 +278,9 @@ second parameter, preserving OS-specific attributes and file
 structure.  For Unix systems, this is equivalent to the simple
 C<copy> routine.  For VMS systems, this calls the C<rmscopy>
 routine (see below).  For OS/2 systems, this calls the C<syscopy>
-XSUB directly.
+XSUB directly. For Win32 systems, this calls C<Win32::CopyFile>.
 
-=head2 Special behavior if C<syscopy> is defined (VMS and OS/2)
+=head2 Special behaviour if C<syscopy> is defined (OS/2, VMS and Win32)
 
 If both arguments to C<copy> are not file handles,
 then C<copy> will perform a "system copy" of
@@ -316,7 +342,7 @@ $! will be set if an error was encountered.
 =head1 AUTHOR
 
 File::Copy was written by Aaron Sherman I<E<lt>ajs@ajs.comE<gt>> in 1995,
-and updated by Charles Bailey I<E<lt>bailey@genetics.upenn.eduE<gt>> in 1996.
+and updated by Charles Bailey I<E<lt>bailey@newman.upenn.eduE<gt>> in 1996.
 
 =cut