isync has asked for the wisdom of the Perl Monks concerning the following question:
As you can see, I avoided tell() to determine position. I blindly assumed it would be slower/produce a disk operation, hence the reliance on a calculated $pos.# chunked read() with optional ranges: the way read() is used here +, we can't use the LENGTH, OFFSET feature of read() # so we need to use the initial seek() in combination with a limit + by position my $chunk_size = $cnf->{chunk_size} || 1024; my $bytes_in = $cnf->{bytes_in} || 0; my $bytes_out= $cnf->{bytes_out}; my $pos = $bytes_in; seek($fh,$bytes_in,0); while ( read( $fh, my $buffer, $chunk_size ) ) { $pos += length($chunk_size); if( defined($bytes_out) && $pos >= $bytes_out){ # return bytes::substr($buffer, 0, ($chunk_size - ($pos - +$bytes_out)) ); # make last chunk shorter print bytes::substr($buffer, 0, ($chunk_size - ($pos - $by +tes_out)) ); last; }else{ # return $buffer; print $buffer; } }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Advice needed on chunked read + byte-range logic
by ikegami (Patriarch) on Aug 25, 2010 at 22:00 UTC | |
|
Re: Advice needed on chunked read + byte-range logic
by isync (Hermit) on Aug 25, 2010 at 23:16 UTC | |
by ikegami (Patriarch) on Aug 26, 2010 at 01:38 UTC | |
by isync (Hermit) on Aug 27, 2010 at 19:05 UTC | |
by ikegami (Patriarch) on Aug 27, 2010 at 22:20 UTC | |
|
Re: Advice needed on chunked read + byte-range logic
by isync (Hermit) on Aug 25, 2010 at 22:08 UTC | |
by ikegami (Patriarch) on Aug 25, 2010 at 22:58 UTC | |
by isync (Hermit) on Aug 25, 2010 at 23:27 UTC |