in reply to How do I redirect STDOUT to a string?

The "old school" way of doing this was by juggling the current default filehandle with select(), e.g.:

my $oldfh = select STDOUT; my $out = IO::File->new('output.txt', 'w',) or die $!; select $out; # Do something involving print(). It will go to output.txt. # Be nice and give back STDOUT select $oldfh;

This functionality is nicely wrapped in the oft-forgotten SelectSaver module (part of the Perl core distribution).

NB - don't confuse this version of select() with the four-argument version, which reports whether a file descriptor is ready for read/write.