According to IO::Handle, ungetc is only promised to work for 1 character. What's the best way to implement ungetstring? Here are my requirements:

- needs to be compatible with FileHandle
- needs to work if the file handle is a pipe (I use open('-|') to decompress data from STDIN using an external decompression program)
- needs to work if I happen to accidentally read eof (I've found that FreeBSD won't let me ungetc if I already read past EOF)
- takes an already opened handle

I'll obviously need to store the previously read (and unget'd) bytes. Should I tie a filehandle? Or should I implement a wrapper class for FileHandle? Or IO::Handle? As I understand it, I can't inherit from FileHandle because it doesn't really inherit from IO::*...

Lastly, if some kind soul wouldn't mind sharing an initial implementation with me, I would appreciate it.

Thanks!


In reply to Best way to implement ungetstring? by coppit

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.