xref: /openbsd-src/gnu/usr.bin/perl/cpan/Test-Harness/t/lib/IO/c55Capture.pm (revision b39c515898423c8d899e35282f4b395f7cad3298)
1*b39c5158Smillertpackage IO::c55Capture;
2*b39c5158Smillert
3*b39c5158Smillertuse IO::Handle;
4*b39c5158Smillert
5*b39c5158Smillert=head1 Name
6*b39c5158Smillert
7*b39c5158Smillertt/lib/IO::c55Capture - a wafer-thin test support package
8*b39c5158Smillert
9*b39c5158Smillert=head1 Why!?
10*b39c5158Smillert
11*b39c5158SmillertCompatibility with 5.5.3 and no external dependencies.
12*b39c5158Smillert
13*b39c5158Smillert=head1 Usage
14*b39c5158Smillert
15*b39c5158SmillertWorks with a global filehandle:
16*b39c5158Smillert
17*b39c5158Smillert    # set a spool to write to
18*b39c5158Smillert    tie local *STDOUT, 'IO::c55Capture';
19*b39c5158Smillert    ...
20*b39c5158Smillert    # clear and retrieve buffer list
21*b39c5158Smillert    my @spooled = tied(*STDOUT)->dump();
22*b39c5158Smillert
23*b39c5158SmillertOr, a lexical (and autocreated) filehandle:
24*b39c5158Smillert
25*b39c5158Smillert    my $capture = IO::c55Capture->new_handle;
26*b39c5158Smillert    ...
27*b39c5158Smillert    my @output = tied($$capture)->dump;
28*b39c5158Smillert
29*b39c5158SmillertNote the '$$' dereference.
30*b39c5158Smillert
31*b39c5158Smillert=cut
32*b39c5158Smillert
33*b39c5158Smillert# XXX actually returns an IO::Handle :-/
34*b39c5158Smillertsub new_handle {
35*b39c5158Smillert    my $class  = shift;
36*b39c5158Smillert    my $handle = IO::Handle->new;
37*b39c5158Smillert    tie $$handle, $class;
38*b39c5158Smillert    return ($handle);
39*b39c5158Smillert}
40*b39c5158Smillert
41*b39c5158Smillertsub TIEHANDLE {
42*b39c5158Smillert    return bless [], __PACKAGE__;
43*b39c5158Smillert}
44*b39c5158Smillert
45*b39c5158Smillertsub PRINT {
46*b39c5158Smillert    my $self = shift;
47*b39c5158Smillert
48*b39c5158Smillert    push @$self, @_;
49*b39c5158Smillert}
50*b39c5158Smillert
51*b39c5158Smillertsub PRINTF {
52*b39c5158Smillert    my $self = shift;
53*b39c5158Smillert    push @$self, sprintf(@_);
54*b39c5158Smillert}
55*b39c5158Smillert
56*b39c5158Smillertsub dump {
57*b39c5158Smillert    my $self = shift;
58*b39c5158Smillert    my @got  = @$self;
59*b39c5158Smillert    @$self = ();
60*b39c5158Smillert    return @got;
61*b39c5158Smillert}
62*b39c5158Smillert
63*b39c5158Smillertpackage util;
64*b39c5158Smillert
65*b39c5158Smillertuse IO::File;
66*b39c5158Smillert
67*b39c5158Smillert# mostly stolen from Module::Build MBTest.pm
68*b39c5158Smillert
69*b39c5158Smillert{    # backwards compatible temp filename recipe adapted from perlfaq
70*b39c5158Smillert    my $tmp_count = 0;
71*b39c5158Smillert    my $tmp_base_name = sprintf( "%d-%d", $$, time() );
72*b39c5158Smillert
73*b39c5158Smillert    sub temp_file_name {
74*b39c5158Smillert        sprintf( "%s-%04d", $tmp_base_name, ++$tmp_count );
75*b39c5158Smillert    }
76*b39c5158Smillert}
77*b39c5158Smillert########################################################################
78*b39c5158Smillert
79*b39c5158Smillertsub save_handle {
80*b39c5158Smillert    my ( $handle, $subr ) = @_;
81*b39c5158Smillert    my $outfile = temp_file_name();
82*b39c5158Smillert
83*b39c5158Smillert    local *SAVEOUT;
84*b39c5158Smillert    open SAVEOUT, ">&" . fileno($handle)
85*b39c5158Smillert      or die "Can't save output handle: $!";
86*b39c5158Smillert    open $handle, "> $outfile" or die "Can't create $outfile: $!";
87*b39c5158Smillert
88*b39c5158Smillert    eval { $subr->() };
89*b39c5158Smillert    my $err = $@;
90*b39c5158Smillert    open $handle, ">&SAVEOUT" or die "Can't restore output: $!";
91*b39c5158Smillert
92*b39c5158Smillert    my $ret = slurp($outfile);
93*b39c5158Smillert    1 while unlink $outfile;
94*b39c5158Smillert    $err and die $err;
95*b39c5158Smillert    return $ret;
96*b39c5158Smillert}
97*b39c5158Smillert
98*b39c5158Smillertsub stdout_of { save_handle( \*STDOUT, @_ ) }
99*b39c5158Smillertsub stderr_of { save_handle( \*STDERR, @_ ) }
100*b39c5158Smillert
101*b39c5158Smillertsub stdout_stderr_of {
102*b39c5158Smillert    my $subr = shift;
103*b39c5158Smillert    my ( $stdout, $stderr );
104*b39c5158Smillert    $stdout = stdout_of(
105*b39c5158Smillert        sub {
106*b39c5158Smillert            $stderr = stderr_of($subr);
107*b39c5158Smillert        }
108*b39c5158Smillert    );
109*b39c5158Smillert    return ( $stdout, $stderr );
110*b39c5158Smillert}
111*b39c5158Smillert
112*b39c5158Smillertsub slurp {
113*b39c5158Smillert    my $fh = IO::File->new( $_[0] ) or die "Can't open $_[0]: $!";
114*b39c5158Smillert    local $/;
115*b39c5158Smillert    return scalar <$fh>;
116*b39c5158Smillert}
117*b39c5158Smillert
118*b39c5158Smillert1;
119*b39c5158Smillert
120*b39c5158Smillert# vim:ts=4:sw=4:et:sta
121