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

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.