File Coverage

blib/lib/Tie/STDOUT.pm
Criterion Covered Total %
statement 23 28 82.1
branch 0 2 0.0
condition 5 6 83.3
subroutine 9 13 69.2
pod n/a
total 37 49 75.5


line stmt bran cond sub pod time code
1             package Tie::STDOUT;
2              
3 2     2   98796 use 5.008;
  2         9  
  2         92  
4              
5 2     2   12 no warnings; # in case they've been turned on
  2         4  
  2         115  
6              
7             $VERSION = '1.0401';
8              
9 2     2   24 use strict;
  2         8  
  2         103  
10              
11             open(REALSTDOUT, ">&STDOUT");
12              
13             # do this late to avoid bogus warning about only using REALSTDOUT once
14 2     2   10 use warnings;
  2         2  
  2         1056  
15              
16             =head1 NAME
17              
18             Tie::STDOUT - intercept writes to STDOUT and apply user-defined functions
19             to them.
20              
21             =head1 SYNOPSIS
22              
23             use Tie::STDOUT
24             print => sub {
25             print map { uc } @_;
26             },
27             printf => ...
28             syswrite => ... ;
29              
30             =head1 DESCRIPTION
31              
32             This module intercepts all writes to the STDOUT filehandle and applies
33             whatever function you desire to what would have gone to STDOUT. In the
34             example above, any use of the print() function on this filehandle will
35             have its output transmogrified into upper case.
36              
37             You will have noticed that we blithely print to the default filehandle
38             (which is almost always STDOUT) in the function we supplied. Relax, this
39             doesn't cause an infinite loop, because your functions are always called
40             with a *normal* STDOUT.
41              
42             You may provide up to three user-defined functions which are respectively
43             called whenever you use print(), printf() or syswrite() on the filehandle:
44              
45             =over 4
46              
47             =item print
48              
49             defaults to printing to the real STDOUT;
50              
51             =item printf
52              
53             defaults to passing all parameters through sprintf() and then passing
54             them to whatever the 'print' function is;
55              
56             =item syswrite
57              
58             Defaults to going straight through to the real STDOUT.
59              
60             =back
61              
62             You will note that the default behaviour is exactly the same as it would
63             be without this module.
64              
65             Because we have a sensible default for 'printf' and because syswrite is so
66             rarely used, you will normally only have to provide your own code for
67             'print'.
68              
69             =head1 BUGS
70              
71             Doesn't work on perl 5.6, because it seems that localising tied
72             filehandles doesn't work.
73              
74             =head1 SEE ALSO
75              
76             =over 4
77              
78             =item Tie::STDERR
79              
80             =item IO::Capture::Stdout
81              
82             =back
83              
84             =head1 FEEDBACK
85              
86             I like to know who's using my code. All comments, including constructive
87             criticism, are welcome. Please email me.
88              
89             =head1 AUTHOR
90              
91             David Cantrell EFE
92              
93             =head1 COPYRIGHT
94              
95             Copyright 2006 David Cantrell
96              
97             This module is free-as-in-speech software, and may be used, distributed,
98             and modified under the same terms as Perl itself.
99              
100             =cut
101              
102             sub import {
103 2     2   22 my $class = shift;
104 2         5 my %params = @_;
105 2         13 tie *STDOUT, $class, %params;
106             }
107              
108             sub TIEHANDLE {
109 2     2   6 my($class, %params) = @_;
110             my $self = {
111 0     0   0 print => $params{print} || sub { print @_; },
112             syswrite => $params{syswrite} || sub {
113 0     0   0 my($buf, $len, $offset) = @_;
114 0 0       0 syswrite(STDOUT, $buf, $len, defined($offset) ? $offset : 0);
115             }
116 2   100     34 };
      50        
117             $self->{printf} = $params{printf} || sub {
118 0     0   0 $self->{print}->(sprintf($_[0], @_[1 .. $#_]))
119 2   100     56 };
120 2         1807 bless($self, $class);
121             }
122              
123             sub _with_real_STDOUT {
124 2     2   42 open(local *STDOUT, ">&REALSTDOUT");
125 2         12 $_[0]->(@_[1 .. $#_]);
126             }
127              
128 1     1   29 sub PRINT { _with_real_STDOUT(shift()->{print}, @_); }
129 1     1   31 sub PRINTF { _with_real_STDOUT(shift()->{printf}, @_); }
130 0     0     sub WRITE { _with_real_STDOUT(shift()->{syswrite}, @_); }
131              
132             1;