See Tie::Handle and the documentation that it references (mostly perltie).

Using that you can make a handle that hides the real file handle inside of itself. Then you can have your handle's implementation of READLINE squirrel away a copy of the read line to an internal buffer. You'd implement a SEEK that would set an attribute so that the next call to READLINE would read from the hidden buffer.

But mixing byte-offset logic (seek) with read-line logic gets to be a pain. So implementing such a tied handle such that it can take care of all of the general cases may be more work than reworking your code a tiny bit so that you are calling methods and then implementing an object that only supports the functionality you need (read next line, save this line and subsequent lines, go back to the saved line, etc.). That way you could deal only in "lines".

Alternately, you could implement a tied handle that works well enough if you only use it in the ways that your program currently does. So, for example, tell() would give you back a line number not a byte offset and trying to READ instead of READLINE would croak.

- tye        


In reply to Re: buffering zipped pipes (Tie::Handle) by tye
in thread buffering zipped pipes by Anonymous Monk

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.