in reply to Re: Advice needed on chunked read + byte-range logic
in thread Advice needed on chunked read + byte-range logic

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.

Replies are listed 'Best First'.
Re^3: Advice needed on chunked read + byte-range logic
by isync (Hermit) on Aug 25, 2010 at 23:27 UTC
    Elegant.

    Good that you reminded me of error checking!

    btw: perlmonks.org should be renamed askikegami.org ;) Thanks!