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"; } #### $ /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 $ #### $ 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 $