SPUG: Attaching a string to STDOUT

David Dyck david.dyck at fluke.com
Mon Apr 19 23:36:10 CDT 2004

On Tue, 20 Apr 2004 at 00:04 -0400, Jack Foy <jack at gehennom.net> wrote:

> I'd like to override STDOUT so that 'print' and friends append to a
> string, rather than writing to a physical file.  How may I do this?  Any
> help is appreciated.

IO::String is a module that will most likely do what you want, but it includes
a note

    Note that perl-5.8 and better has built-in support for "in
    memory" files, which are set up by passing a reference
    instead of a filename to the open() call. The reason for
    using this module is that it makes the code backwards
    compatible with older versions of Perl.

I tried it in perl 5.9.2 and get:
$ perl -we 'open F,">",\$f; select F; print "hello"; print ", world"; print STDOUT "<$f>\n"'
<hello, world>

for more info
  perldoc -f IO::String;

More information about the spug-list mailing list