creamygoodness has asked for the wisdom of the Perl Monks concerning the following question:
I need to open many filehandles against the same file. They should all be independent, meaning that they should all be able to seek, read, etc without affecting each other. If I read perlopentut correctly, this should do the trick:
That works as expected on Perl 5.8.6 on my OS X laptop. However, It doesn't work with either 5.8.6, 5.8.0, or bleadperl on a RedHat9 box. seeking/reading against the original messes up the dupe. The dupe SAYS that it's in the right spot, but it doesn't read the data that's there. Here's a test script...open(my $orig, "<", "foofile"; open(my $dupe, "<&", $orig);
#!/usr/bin/perl use strict; use warnings; open (my $junkfile, '>', "junkfile"); my $junk = ''; for my $first_byte (0 .. 255) { for my $second_byte (0 .. 255) { $junk .= pack('CC', $first_byte, $second_byte); $junk .= "\0" x 2; } } print $junkfile $junk; close $junkfile; open(my $orig, '<', "junkfile") or die $!; seek( $orig, 256, 0); print_locations("Locations after seeking ORIG to 256: "); my $correct; read($orig, $correct, 8); print_locations("Locations after reading 8 bytes from ORIG: "); my @bytes = unpack('C*', $correct); print "ORIG read these bytes: @bytes\n"; open(my $dupe, "<&", $orig) or die $!; print_locations("Locations immediately after duping: "); seek( $dupe, 256, 0); print_locations("Locations after seeking DUPE to 256: "); seek($orig, 120, 0); print_locations("Locations after seeking ORIG to 120: "); my $useless; read($orig, $useless, 20); print_locations("Locations after reading 20 bytes from ORIG: "); my $test; read($dupe, $test, 8); print_locations("Locations after reading 8 bytes from DUPE: "); @bytes = unpack('C*', $test); print "DUPE read these bytes: @bytes\n"; sub print_locations { my $label = shift; print "$label\n "; print "ORIG " . tell($orig) . " "; print "DUPE "; print defined $dupe ? tell($dupe) : "not valid"; print "\n"; }
... and its output:
On RedHat9:
$ /usr/local/blead/bin/perl5.9.3 many.plx Locations after seeking ORIG to 256: ORIG 256 DUPE not valid Locations after reading 8 bytes from ORIG: ORIG 264 DUPE not valid ORIG read these bytes: 0 64 0 0 0 65 0 0 Locations immediately after duping: ORIG 264 DUPE 264 Locations after seeking DUPE to 256: ORIG 264 DUPE 256 Locations after seeking ORIG to 120: ORIG 120 DUPE 256 Locations after reading 20 bytes from ORIG: ORIG 140 DUPE 256 Locations after reading 8 bytes from DUPE: ORIG 140 DUPE 264 DUPE read these bytes: 4 30 0 0 4 31 0 0 $
On OS X:
$ perl many.plx Locations after seeking ORIG to 256: ORIG 256 DUPE not valid Locations after reading 8 bytes from ORIG: ORIG 264 DUPE not valid ORIG read these bytes: 0 64 0 0 0 65 0 0 Locations immediately after duping: ORIG 264 DUPE 4096 Locations after seeking DUPE to 256: ORIG 264 DUPE 256 Locations after seeking ORIG to 120: ORIG 120 DUPE 256 Locations after reading 20 bytes from ORIG: ORIG 140 DUPE 256 Locations after reading 8 bytes from DUPE: ORIG 140 DUPE 264 DUPE read these bytes: 0 64 0 0 0 65 0 0 $
What's the right way to dupe a filehandle?
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Duping filehandles
by ambrus (Abbot) on Jan 20, 2006 at 09:25 UTC | |
by creamygoodness (Curate) on Jan 20, 2006 at 16:53 UTC | |
by ff (Hermit) on Jun 16, 2006 at 14:12 UTC |