kostya has asked for the wisdom of the Perl Monks concerning the following question:

I've got a set of perl objects that need to capture the output of other objects. I accomplish this basically by saving STDOUT and then opening STDOUT on the write-end of a FIFO:
pipe READ_FIFO, WRITE_FIFO; open STDOUT_SAVED, ">&STDOUT"; open STDOUT, ">&WRITE_FIFO"; component->display(); #displays the component to STDOUT->WRITE_FIFO open STDOUT, ">&STDOUT_SAVED"; close WRITE_FIFO; $content=""; while (<READ_FIFO>) { $content .= $_; } close READ_FIFO; return $content;

The problem is that there appears to be a 4k (4096B) buffer limit. This solution works great with small components (text less than 4k), but for longer ones, the code hangs on the "component->display()". It basically fills up the buffer and waits for it to be cleared. But since I'm doing this all in one process, it never gets cleared :-)

Is there a way to increase the buffer size?

BTW, I'm not married to this approach, but it seemed the cheapest. I thought storing up a buffer of text in a FIFO was cheaper than forking a process. I also don't want to use a file--try to avoid the overhead and headaches involved with temporary files (especially accross multiple platforms).

Suggestions on other ways to catch the STDOUT to a variable are also welcome! TIA

Replies are listed 'Best First'.
Re: FIFO buffer size limits
by hdp (Beadle) on Apr 26, 2001 at 22:39 UTC
    You can do away with the FIFO entirely using IO::Scalar. Hope this helps. :) tie *OUT, 'IO::Scalar', \$foo

    Then select this filehandle in whatever code needs to have its output captured.

    By the way, this question's somewhat misnamed -- when it comes down to it, you want to get some output into a variable; the FIFO problems are just an artifact of the particular approach that you're taking. Despite this, you only devote the first and last sentences to your real goal.

    In general, you'll get more useful responses if you say "I want to do X! (Y is what came to mind, but it doesn't work quite right)" rather than "I'm doing Y! (p.s. it's because I want X)". X is desired, Y is nonessential, so focus on X, not Y. I hope that makes sense. :)

    hdp.

      Thanks. That did the trick.
Re: FIFO buffer size limits
by traveler (Parson) on Apr 26, 2001 at 22:45 UTC
    This is a "feature" of pipes. The 4k sounds like a *nix (possibly Linux) limit. Pipes were not designed to be large temporary files, they were designed for interprocess communication.

    If what you want is temporary storage, a temporary file is probably best. If your data is small (for some definition of small) why not just save it in memory?

    It really depends on what you are actually trying to do.

    --traveler