# 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.11';
+$VERSION = '2.12';
require Exporter;
@ISA = qw(Exporter);
my $from = shift;
my $to = shift;
+ my $size;
+ if (@_) {
+ $size = shift(@_) + 0;
+ croak("Bad buffer size for copy: $size\n") unless ($size > 0);
+ }
+
my $from_a_handle = (ref($from)
? (ref($from) eq 'GLOB'
|| UNIVERSAL::isa($from, 'GLOB')
my $closefrom = 0;
my $closeto = 0;
- my ($size, $status, $r, $buf);
+ my ($status, $r, $buf);
local($\) = '';
my $from_h;
$closefrom = 1;
}
+ # Seems most logical to do this here, in case future changes would want to
+ # make this croak for some reason.
+ unless (defined $size) {
+ $size = tied(*$from_h) ? 0 : -s $from_h || 0;
+ $size = 1024 if ($size < 512);
+ $size = $Too_Big if ($size > $Too_Big);
+ }
+
my $to_h;
if ($to_a_handle) {
$to_h = $to;
$closeto = 1;
}
- if (@_) {
- $size = shift(@_) + 0;
- croak("Bad buffer size for copy: $size\n") unless ($size > 0);
- } else {
- $size = tied(*$from_h) ? 0 : -s $from_h || 0;
- $size = 1024 if ($size < 512);
- $size = $Too_Big if ($size > $Too_Big);
- }
-
$! = 0;
for (;;) {
my ($r, $w, $t);
-#!./perl
+#!./perl -w
BEGIN {
if( $ENV{PERL_CORE} ) {
my $TB = Test::More->builder;
-plan tests => 60;
+plan tests => 70;
# We're going to override rename() later on but Perl has to see an override
# at compile time to honor it.
}
# First we create a file
- open(F, ">file-$$") or die;
+ open(F, ">file-$$") or die $!;
binmode F; # for DOSISH platforms, because test 3 copies to stdout
printf F "ok\n";
close F;
copy "file-$$", "copy-$$";
- open(F, "copy-$$") or die;
+ open(F, "copy-$$") or die $!;
$foo = <F>;
close(F);
require IO::File;
$fh = IO::File->new(">copy-$$") or die "Cannot open copy-$$:$!";
- binmode $fh or die;
+ binmode $fh or die $!;
copy("file-$$",$fh);
$fh->close or die "close: $!";
open(R, "copy-$$") or die; $foo = <R>; close(R);
require FileHandle;
my $fh = FileHandle->new(">copy-$$") or die "Cannot open copy-$$:$!";
- binmode $fh or die;
+ binmode $fh or die $!;
copy("file-$$",$fh);
$fh->close;
- open(R, "copy-$$") or die; $foo = <R>; close(R);
+ open(R, "copy-$$") or die $!; $foo = <R>; close(R);
is $foo, "ok\n", 'copy(fn, fh): same contents';
unlink "file-$$" or die "unlink: $!";
ok move("copy-$$", "file-$$"), 'move';
ok -e "file-$$", ' destination exists';
ok !-e "copy-$$", ' source does not';
- open(R, "file-$$") or die; $foo = <R>; close(R);
+ open(R, "file-$$") or die $!; $foo = <R>; close(R);
is $foo, "ok\n", 'contents preserved';
TODO: {
}
# trick: create lib/ if not exists - not needed in Perl core
- unless (-d 'lib') { mkdir 'lib' or die; }
+ unless (-d 'lib') { mkdir 'lib' or die $!; }
copy "file-$$", "lib";
open(R, "lib/file-$$") or die $!; $foo = <R>; close(R);
is $foo, "ok\n", 'copy(fn, dir): same contents';
# Do it twice to ensure copying over the same file works.
copy "file-$$", "lib";
- open(R, "lib/file-$$") or die; $foo = <R>; close(R);
+ open(R, "lib/file-$$") or die $!; $foo = <R>; close(R);
is $foo, "ok\n", 'copy over the same file works';
unlink "lib/file-$$" or die "unlink: $!";
ok !-z "file-$$",
'rt.perl.org 5196: copying to itself would truncate the file';
- unlink "symlink-$$";
- unlink "file-$$";
+ unlink "symlink-$$" or die $!;
+ unlink "file-$$" or die $!;
}
SKIP: {
ok ! -z "file-$$",
'rt.perl.org 5196: copying to itself would truncate the file';
- unlink "hardlink-$$";
- unlink "file-$$";
+ unlink "hardlink-$$" or die $!;
+ unlink "file-$$" or die $!;
}
+
+ open(F, ">file-$$") or die $!;
+ binmode F;
+ print F "this is file\n";
+ close F;
+
+ my $copy_msg = "this is copy\n";
+ open(F, ">copy-$$") or die $!;
+ binmode F;
+ print F $copy_msg;
+ close F;
+
+ my @warnings;
+ local $SIG{__WARN__} = sub { push @warnings, join '', @_ };
+
+ # pie-$$ so that we force a non-constant, else the numeric conversion (of 0)
+ # is cached and we don't get a warning the second time round
+ is eval { copy("file-$$", "copy-$$", "pie-$$"); 1 }, undef,
+ "a bad buffer size fails to copy";
+ like $@, qr/Bad buffer size for copy/, "with a helpful error message";
+ unless (is scalar @warnings, 1, "There is 1 warning") {
+ diag $_ foreach @warnings;
+ }
+
+ is -s "copy-$$", length $copy_msg, "but does not truncate the destination";
+ open(F, "copy-$$") or die $!;
+ $foo = <F>;
+ close(F);
+ is $foo, $copy_msg, "nor change the destination's contents";
+
+ unlink "file-$$" or die $!;
+ unlink "copy-$$" or die $!;
}