# 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 - $bytes_out)) ); last; }else{ # return $buffer; print $buffer; } }