SOLVED: Via the pseek function posted by BrowserUK:
use constant CHUNK => 4*1024; sub pseek { my( $p, $o ) = @_; read( $p, my $discard, $CHUNK ), $o -= $CHUNK while $o > $CHUNK; read( $p, $discard, $o-1 ); return $o; }
Thanks to all for your help.
-------------------------------------Short:
Is there a way to achieve seek()-type functionality on pipe output?
Long:
A project I'm working on reads files from a given offset. For bland text files, this is simple:
open(FILE, $path) || die "$!\n"; seek(FILE, $offset, 0); while(<FILE>) { # do stuff } close FILE;
However, it also needs to be able to read compressed files. Rather than hardcoding each compression format handler, I'd like to just add a configuration directive that points my program at the appropriate cat tool for the format (zcat, bzcat, whatever happens to be relevant) and open it like this:
open(FILE, '-|', "$cat $file") || die "$!\n";
But as you all know, I can't seek() on a pipe.
How can I work around this? I could dump the output to a file and then read it back in, but this is horribly inefficient and will cause significant performance problems as the files can reach 300-500 MB.
Thanks for any help.In reply to seek() functionality on pipes by HKS
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |