| 1 |
package IO::Catch; |
| 2 |
use strict; |
| 3 |
use Carp qw(croak); |
| 4 |
|
| 5 |
=head1 NAME |
| 6 |
|
| 7 |
IO::Catch - capture STDOUT and STDERR into global variables |
| 8 |
|
| 9 |
=head1 AUTHOR |
| 10 |
|
| 11 |
Max Maischein ( corion at cpan.org ) |
| 12 |
All code ripped from pod2test by M. Schwern |
| 13 |
|
| 14 |
=head1 SYNOPSIS |
| 15 |
|
| 16 |
# pre-5.8.0's warns aren't caught by a tied STDERR. |
| 17 |
our ($_STDOUT_, $_STDERR_); |
| 18 |
tie *STDOUT, 'IO::Catch', '_STDOUT_' or die $!; |
| 19 |
tie *STDERR, 'IO::Catch', '_STDERR_' or die $!; |
| 20 |
|
| 21 |
# now you can access $main::_STDOUT_ and $_STDERR_ |
| 22 |
# to see the output. |
| 23 |
|
| 24 |
=cut |
| 25 |
|
| 26 |
our $VERSION = '0.02'; |
| 27 |
|
| 28 |
sub TIEHANDLE { |
| 29 |
my($class, $var) = @_; |
| 30 |
croak "Need a variable name to tie to" unless $var; |
| 31 |
return bless { var => $var }, $class; |
| 32 |
} |
| 33 |
|
| 34 |
sub PRINT { |
| 35 |
no strict 'refs'; |
| 36 |
my($self) = shift; |
| 37 |
${'main::'.$self->{var}} = "" |
| 38 |
unless defined ${'main::'.$self->{var}}; |
| 39 |
${'main::'.$self->{var}} .= join '', @_; |
| 40 |
} |
| 41 |
|
| 42 |
sub PRINTF { |
| 43 |
no strict 'refs'; |
| 44 |
my($self) = shift; |
| 45 |
my $tmpl = shift; |
| 46 |
${'main::'.$self->{var}} = "" |
| 47 |
unless defined ${'main::'.$self->{var}}; |
| 48 |
${'main::'.$self->{var}} .= sprintf $tmpl, @_; |
| 49 |
} |
| 50 |
|
| 51 |
sub OPEN {} # XXX Hackery in case the user redirects |
| 52 |
sub CLOSE {} # XXX STDERR/STDOUT. This is not the behavior we want. |
| 53 |
|
| 54 |
sub READ {} |
| 55 |
sub READLINE {} |
| 56 |
sub GETC {} |
| 57 |
sub BINMODE {} |
| 58 |
|
| 59 |
1; |