in reply to Tee STDOUT

Here's the context: I want to capture the results of Test::Harness runtests into a string, while it also goes to the screen. Here's what I have:
my $stdout = ''; eval { *STDOUT = new IO::Tee( \*STDERR, IO::Scalar->new(\$stdout)); $ok = runtests(@tests); };
It errors out with
write() on unopened filehandle STDOUT at /usr/lib/perl5/vendor_perl/5. +8.2/Test/Harness.pm line 665.
How can I capture runtest output to a string as well as the screen?

Thanks!

water

Replies are listed 'Best First'.
Re^2: Tee STDOUT
by ikegami (Patriarch) on Sep 24, 2004 at 02:31 UTC
    I don't have IO::Scalar, but the following works. Downside: requires perl 5.8.0 or higher.
    use 5.8.0; # for open \$buf. use strict; use warnings; use IO::Tee (); my $stdout = ''; { my $fh_scalar; open($fh_scalar, '>', \$stdout) or die("Can't open \$stdout: $!\n"); local *STDOUT = new IO::Tee( \*STDERR, $fh_scalar ); print("Test\n"); } print("\n"); print("\$stdout:\n"); print($stdout);

    I added local to easily restore the original STDOUT, but it's not needed.

      Thanks, but I tried your code and still get
      write() on unopened filehandle STDOUT at /usr/lib/perl5/vendor_perl/5. +8.2/Test/Harness.pm line 665.
      Maybe this has something to do with the magic internals and filehandle remapping already going on in Test::Harness?