develooper Front page | perl.perl5.porters | Postings from July 2001

[PATCH] File::Copy fails on tied handles

Thread Next
From:
Wilson, Doug
Date:
July 12, 2001 15:19
Subject:
[PATCH] File::Copy fails on tied handles
Message ID:
35A280DF784CD411A06B0008C7B130ADB5505B@sdex04.sd.intuit.com

version is File::Copy 2.02, patch also works against 2.03

Maybe I'm not patient enough, but it seems like my bugreports
are in limbo, and the FAQ says go ahead and submit a patch, so
here it is...

Here's a test script:
#!/usr/local/bin/perl -l -w

# use lib qw( . ); #Where I have my modified File::Copy
use strict;
# Not a core lib module, but simplest test case
# I could come up with
use IO::Scalar;
use File::Copy;

my $input;
my $output;
tie *IN, 'IO::Scalar', \$input;
print IN "This is a test";
seek IN, 0, 0;
tie *OUT, 'IO::Scalar', \$output;
copy(\*IN, \*OUT) or die "Acck: $!";
close IN;
close OUT;
print $output;

And here's the patch:
--- Copy.pm.orig        Thu Jul 12 08:29:43 2001
+++ Copy.pm     Thu Jul 12 14:55:32 2001
@@ -78,24 +78,27 @@
     my $closefrom = 0;
     my $closeto = 0;
     my ($size, $status, $r, $buf);
-    local(*FROM, *TO);
     local($\) = '';
 
+    my $from_h;
     if ($from_a_handle) {
-       *FROM = *$from{FILEHANDLE};
+       $from_h = $from;
     } else {
        $from = _protect($from) if $from =~ /^\s/s;
-       open(FROM, "< $from\0") or goto fail_open1;
-       binmode FROM or die "($!,$^E)";
+       $from_h = \do { local *FH };
+       open($from_h, "< $from\0") or goto fail_open1;
+       binmode $from_h or die "($!,$^E)";
        $closefrom = 1;
     }
 
+    my $to_h;
     if ($to_a_handle) {
-       *TO = *$to{FILEHANDLE};
+       $to_h = $to;
     } else {
        $to = _protect($to) if $to =~ /^\s/s;
-       open(TO,"> $to\0") or goto fail_open2;
-       binmode TO or die "($!,$^E)";
+       $to_h = \do { local *FH };
+       open($to_h,"> $to\0") or goto fail_open2;
+       binmode $to_h or die "($!,$^E)";
        $closeto = 1;
     }
 
@@ -103,7 +106,8 @@
        $size = shift(@_) + 0;
        croak("Bad buffer size for copy: $size\n") unless ($size > 0);
     } else {
-       $size = -s FROM;
+       no warnings 'uninitialized';
+       $size = -s $from_h;
        $size = 1024 if ($size < 512);
        $size = $Too_Big if ($size > $Too_Big);
     }
@@ -111,17 +115,17 @@
     $! = 0;
     for (;;) {
        my ($r, $w, $t);
-       defined($r = sysread(FROM, $buf, $size))
+       defined($r = sysread($from_h, $buf, $size))
            or goto fail_inner;
        last unless $r;
        for ($w = 0; $w < $r; $w += $t) {
-           $t = syswrite(TO, $buf, $r - $w, $w)
+           $t = syswrite($to_h, $buf, $r - $w, $w)
                or goto fail_inner;
        }
     }
 
-    close(TO) || goto fail_open2 if $closeto;
-    close(FROM) || goto fail_open1 if $closefrom;
+    close($to_h) || goto fail_open2 if $closeto;
+    close($from_h) || goto fail_open1 if $closefrom;
 
     # Use this idiom to avoid uninitialized value warning.
     return 1;
@@ -131,14 +135,14 @@
     if ($closeto) {
        $status = $!;
        $! = 0;
-       close TO;
+       close $to_h;
        $! = $status unless $!;
     }
   fail_open2:
     if ($closefrom) {
        $status = $!;
        $! = 0;
-       close FROM;
+       close $from_h;
        $! = $status unless $!;
     }
   fail_open1:

Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About