HKS has asked for the wisdom of the Perl Monks concerning the following question:
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.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: seek() functionality on pipes
by BrowserUk (Patriarch) on Jul 21, 2008 at 18:06 UTC | |
by ikegami (Patriarch) on Jul 21, 2008 at 19:08 UTC | |
by BrowserUk (Patriarch) on Jul 21, 2008 at 19:22 UTC | |
by ikegami (Patriarch) on Jul 21, 2008 at 19:34 UTC | |
by BrowserUk (Patriarch) on Jul 21, 2008 at 19:46 UTC | |
| |
by HKS (Acolyte) on Jul 21, 2008 at 18:58 UTC | |
by BrowserUk (Patriarch) on Jul 21, 2008 at 19:32 UTC | |
|
Re: seek() functionality on pipes
by RMGir (Prior) on Jul 21, 2008 at 18:07 UTC | |
by HKS (Acolyte) on Jul 21, 2008 at 19:02 UTC | |
by RMGir (Prior) on Jul 22, 2008 at 11:23 UTC | |
|
Re: seek() functionality on pipes
by Fletch (Bishop) on Jul 21, 2008 at 17:54 UTC | |
|
Re: seek() functionality on pipes
by zentara (Cardinal) on Jul 21, 2008 at 18:19 UTC | |
by HKS (Acolyte) on Jul 21, 2008 at 19:03 UTC | |
|
Re: seek() functionality on pipes
by salva (Canon) on Jul 21, 2008 at 18:07 UTC | |
|
Re: seek() functionality on pipes
by sgifford (Prior) on Jul 22, 2008 at 03:07 UTC |