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

arg. Sorry! (original post updated, see code)

I had a return() where a print() must be. See, the function appends these chunks to STDOUT. As I read your provided logic, it writes everything to a (possibly large) $buffer and then outputs it as a whole. Or did I oversee something in your solution?

2nd update: had a mix up in there with $buffer and $bytes, cleared up by renaming to $chunk_size.
  • Comment on Re: Advice needed on chunked read + byte-range logic

Replies are listed 'Best First'.
Re^2: Advice needed on chunked read + byte-range logic
by ikegami (Patriarch) on Aug 25, 2010 at 22:58 UTC

    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.

      Elegant.

      Good that you reminded me of error checking!

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