http://www.perlmonks.org?node_id=398587

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

I have combed the archives of this site looking for help with redirecting STDOUT from the Test::ok function temporarily. Unfortunately, all the answers so far work fine in 5.8 but not on 5.6 or require adding a new file to application distribution.

My first attempt was as follows:

# Works with 5.8 only! sub ok_test_wrapper($@) { my ($desc,@params) = @_; my ($stdout); open (OLDOUT, '>&STDOUT') || die "dup() of STDOUT failed: $!"; close STDOUT or die "close STDOUT: $!"; open STDOUT, '>', \$stdout or die "redirect STDOUT: $!"; Test::ok(@params); close STDOUT or die "close STDOUT: $!"; open (STDOUT, '>&OLDOUT') || die "reopen STDOUT: $!"; close OLDOUT or die "close OLDOUT: $!"; chomp($stdout); print "$stdout - $desc\n"; return; } sub ok($;$$) { ok_test_wrapper("$_[[1]]",$_[[0]]); }

Unfortunately, the manpage for open in 5.6 does not allow the use of references in the same way as 5.8. It creates a file by the name 'HASH(0x3859383)' or the like.

p>Using Tie::Handle requires creating another file which on our system requires going through the whole design process again. The preferred solution is a modification to the file this code resides in.

The only option I came up with, which is a hack, is to write/read a temp file...yuck...

# Hack sub ok_test_wrapper($@) { my ($desc,@params) = @_; my ($stdout) = "./foo3.$$"; open (OLDOUT, '>&STDOUT') || die "dup() of STDOUT failed: $!"; close STDOUT or die "close STDOUT: $!"; open STDOUT, '>', $stdout or die "redirect STDOUT: $!"; select STDOUT; $| = 1; Test::ok(@params); close STDOUT or die "close STDOUT: $!"; open (STDOUT, '>&OLDOUT') || die "reopen STDOUT: $!"; close OLDOUT or die "close OLDOUT: $!"; open FH, "<$stdout" or die "reading file failed: $!"; my $tmp = <FH>; close(FH); unlink($stdout); chomp($tmp); print "$tmp - $desc\n"; return; }

Any other ideas?

mgc