SPUG: Attaching a string to STDOUT

David Dyck david.dyck at fluke.com
Wed Apr 21 18:13:33 CDT 2004


On Wed, 21 Apr 2004 at 15:12 -0700, Michael R. Wolf <MichaelRWolf at att.net>...:

> Jack Foy <jack at gehennom.net> writes:
>
> > 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.
>
> Does IO::All do this?

Looks like it uses IO::String that we mentioned earlier, as

    The IO::All object is a proxy for IO::File, IO::Dir, IO::Socket,
    IO::String, Tie::File and File::ReadBackwards. You can use most of the
    methods found in these classes and in IO::Handle (which they all inherit
    from). IO::All is easily subclassable. You can override any methods and
    also add new methods of your own.

I didn't have the Spiffy module installed, so I had to learn about that
also.

I think the IO::All example that does the same thing as the earlier
perl-5.8 builtin string example is a bit longer, e.g:

perl -we 'use IO::All qw(-tie); my $s=io(q($)); select $s;
    print "hello"; print ", world";
	print STDOUT "<",${$s->string_ref},">\n";'

but at least the prints to the strings are about the same.

# perl5.8 builtin string files
perl -we 'open F,">",\$f; select F;
    print "hello"; print ", world";
	print STDOUT "<$f>\n"'

 David



More information about the spug-list mailing list