Greets,

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:

open(my $orig, "<", "foofile"; open(my $dupe, "<&", $orig);
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...
#!/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?

--
Marvin Humphrey
Rectangular Research ― http://www.rectangular.com

In reply to Duping filehandles by creamygoodness

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.