On Wednesday, February 12, 2003, at 11:43  PM, Nathan Herring wrote:

Is there something out there that already does this? Or does it on
arbitrary numbers of filehandles/io::handles?
You need to tie your STDERR and STDOUT file handles. Write a custom tie module and simply tie it to STDERR and STDOUT. Check out the TieOut.pm module in various modules on the CPAN for an example. I use a variation in my App::Info module for testing. It looks like this:

package TieOut;

# This module is swiped and adapted from ExtUtils::MakeMaker.

sub TIEHANDLE { bless [], ref $_[0] || $_[0] }

sub PRINT {
my $self = shift;
push @$self, join '', @_;
}

sub PRINTF {
my $self = shift;
push @$self, sprintf @_;
}

sub READLINE {
my $self = shift;
return shift @$self;
}

sub read {
my $self = shift;
my $ret = join '', @$self;
@$self = ();
return $ret;
}

1;

Then in my tests, I just have:

my $stdout = tie *STDOUT, 'TieOut' or die "Cannot tie STDOUT: $!\n";
my $stdin = tie *STDIN, 'TieOut' or die "Cannot tie STDIN: $!\n";

The upshot is that you can just rewrite the PRINT method to both print stuff to STDOUT and to log what was printed however you like. Use Time::HiRes for sub-second times.

HTH,

David

--
David Wheeler AIM: dwTheory
[EMAIL PROTECTED] ICQ: 15726394
Yahoo!: dew7e
Jabber: [EMAIL PROTECTED]
Kineticode. Setting knowledge in motion.[sm]



Reply via email to