1 package IO::c55Capture;
7 t/lib/IO::c55Capture - a wafer-thin test support package
11 Compatibility with 5.5.3 and no external dependencies.
15 Works with a global filehandle:
17 # set a spool to write to
18 tie local *STDOUT, 'IO::c55Capture';
20 # clear and retrieve buffer list
21 my @spooled = tied(*STDOUT)->dump();
23 Or, a lexical (and autocreated) filehandle:
25 my $capture = IO::c55Capture->new_handle;
27 my @output = tied($$capture)->dump;
29 Note the '$$' dereference.
33 # XXX actually returns an IO::Handle :-/
36 my $handle = IO::Handle->new;
42 return bless [], __PACKAGE__;
53 push @$self, sprintf(@_);
67 # mostly stolen from Module::Build MBTest.pm
69 { # backwards compatible temp filename recipe adapted from perlfaq
71 my $tmp_base_name = sprintf( "%d-%d", $$, time() );
74 sprintf( "%s-%04d", $tmp_base_name, ++$tmp_count );
77 ########################################################################
80 my ( $handle, $subr ) = @_;
81 my $outfile = temp_file_name();
84 open SAVEOUT, ">&" . fileno($handle)
85 or die "Can't save output handle: $!";
86 open $handle, "> $outfile" or die "Can't create $outfile: $!";
90 open $handle, ">&SAVEOUT" or die "Can't restore output: $!";
92 my $ret = slurp($outfile);
93 1 while unlink $outfile;
98 sub stdout_of { save_handle( \*STDOUT, @_ ) }
99 sub stderr_of { save_handle( \*STDERR, @_ ) }
101 sub stdout_stderr_of {
103 my ( $stdout, $stderr );
106 $stderr = stderr_of($subr);
109 return ( $stdout, $stderr );
113 my $fh = IO::File->new( $_[0] ) or die "Can't open $_[0]: $!";
120 # vim:ts=4:sw=4:et:sta