Assuming you don't need to read the characters you end up discarding, I'd use the following: (Complete with error checking)
use List::Util qw( min );
seek($fh, $bytes_in, SEEK_SET)
or die($!);
my $to_read = $bytes_out-$bytes_in;
while ($to_read) {
my $rv = read($fh, my $buffer, min($chunk_size, $to_read));
die($!) if !defined($rv);
die("Premature eof") if !$rv;
$to_read -= $rv;
print($buffer);
}
If you do want to move the file pointer beyond the last byte you need as you are currently doing, then you could continue always blocks of $chunk_size as you are currently doing.
|