Capture/Stdout.pm

filter: Statementstmt 100% 25 / 25 Branchbran 100% 4 / 4 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 7 / 7 Podpod n/a totaltotal 100% 36 / 36 CC 3 SCAR 5.1
CC
3
Cov
100%
_check_pre_conditions
3.0
BEGIN
1.0
BEGIN
1.0
CRAP 1.7
« Capture/Stderr.pm ↑ Summary Capture/Tie_STDx.pm »
1 package IO::Capture::Stdout;
2 6 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 6 ▶use base qw/IO::Capture/;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 6 ▶use IO::Capture::Tie_STDx;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 9 ▶sub _start {
Subroutine _start: called CC=1 SCAR=0.0
7 9 my $self = shift;
8 9 $self->line_pointer(1);
9 9 tie *STDOUT, "IO::Capture::Tie_STDx";
10 }
11  
12 9 ▶sub _retrieve_captured_text {
Subroutine _retrieve_captured_text: called CC=1 SCAR=0.0
13 9 my $self = shift;
14 9 my $messages = \@{$self->{'IO::Capture::messages'}};
15  
16 9 @$messages = <STDOUT>;
17 #$self->line_pointer(1);
18 9 return 1;
19 }
20  
21 12 ▶sub _check_pre_conditions {
Subroutine _check_pre_conditions: called CC=3 SCAR=11.0
22 12 my $self = shift;
23  
24 12 ▶ return unless $self->SUPER::_check_pre_conditions;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->SUPER::_check_pre_conditions 2 10 12
25  
26 10 ▶ if (tied *STDOUT) {
Branch: 1 branch
BranchTrueFalseTotal
if (tied *STDOUT) 1 9 10
27 1 carp "WARNING: STDOUT already tied, unable to capture";
28 1 return;
29 }
30 9 return 1;
31 }
32  
33 9 ▶sub _stop {
Subroutine _stop: called CC=1 SCAR=0.0
34 9 untie *STDOUT;
35 }
36 6 1;
37  
38 =head1 NAME
39  
40 IO::Capture::Stdout - Capture any output sent to STDOUT
41  
42 =head1 SYNOPSIS
43  
44 # Generic example (Just to give the overall view)
45 use IO::Capture::Stdout;
46  
47 $capture = IO::Capture::Stdout->new();
48  
49 $capture->start(); # STDOUT Output captured
50 print STDOUT "Test Line One\n";
51 print STDOUT "Test Line Two\n";
52 print STDOUT "Test Line Three\n";
53 $capture->stop(); # STDOUT output sent to wherever it was before 'start'
54  
55 # In 'scalar context' returns next line
56 $line = $capture->read;
57 print "$line"; # prints "Test Line One"
58  
59 $line = $capture->read;
60 print "$line"; # prints "Test Line Two"
61  
62 # move line pointer to line 1
63 $capture->line_pointer(1);
64  
65 $line = $capture->read;
66 print "$line"; # prints "Test Line One"
67  
68 # Find out current line number
69 $current_line_position = $capture->line_pointer;
70  
71 # In 'List Context' return an array(list)
72 @all_lines = $capture->read;
73  
74 # More useful example 1 - "Using in module tests"
75 # Note: If you don't want to make users install
76 # the IO::Capture module just for your tests,
77 # you can just install in the t/lib directory
78 # of your module and use the lib pragma in
79 # your tests.
80  
81 use lib "t/lib";
82 use IO::Capture::Stdout;
83  
84 use Test::More;
85  
86 my $capture = IO::Capture::Stdout->new;
87 $capture->start
88  
89 # execute with a bad parameter to make sure get
90 # an error.
91  
92 ok( ! $test("Bad Parameter") );
93  
94 $capture->stop();
95  
96  
97  
98 =head1 DESCRIPTION
99  
100 The module C<IO::Capture::Stdout>, is derived from the abstract class C<IO::Capture>.
101 See L<IO::Capture>. The purpose of the module (as the name suggests) is to capture
102 any output sent to C<STDOUT>. After the capture is stopped, the STDOUT filehandle
103 will be reset to the previous location. E.g., If previously redirected to a file, when
104 C<IO::Capture-E<gt>stop> is called, output will start going into that file again.
105  
106 Note: This module won't work with the perl function, system(), or any other operation
107 involving a fork(). If you want to capture the output from a system command,
108 it is faster to use open() or back-ticks.
109  
110 my $output = `/usr/sbin/ls -l 2>&1`;
111  
112  
113 =head1 METHODS
114  
115 =head2 new
116  
117 =over 4
118  
119 =item *
120  
121 Creates a new capture object.
122  
123 =item *
124  
125 An object can be reused as needed, so will only need to do one of these.
126  
127 =over 4
128  
129 =item *
130  
131 Be aware, any data previously captured will be discarded if a new
132 capture session is started.
133  
134 =back
135  
136 =back
137  
138 =head2 start
139  
140 =over 4
141  
142 =item *
143  
144 Start capturing data into the C<IO::Capture> Object.
145  
146 =item *
147  
148 Can B<not> be called on an object that is already capturing.
149  
150 =item *
151  
152 Can B<not> be called while STDOUT tied to an object.
153  
154 =item *
155  
156 C<undef> will be returned on an error.
157  
158 =back
159  
160 =head2 stop
161  
162 =over 4
163  
164 =item *
165  
166 Stop capturing data and point STDOUT back to it's previous output location
167 I.e., untie STDOUT
168  
169 =back
170  
171 =head2 read
172  
173 =over 4
174  
175 =item *
176  
177 In I<Scalar Context>
178  
179 =over 4
180  
181 =item *
182  
183 Lines are read from the buffer at the position of the C<line_pointer>,
184 and the pointer is incremented by one.
185  
186 $next_line = $capture->read;
187  
188 =back
189  
190 =item *
191  
192 In I<List Context>
193  
194 =over 4
195  
196 =item *
197  
198 The array is returned. The C<line_pointer> is not affected.
199  
200 @buffer = $capture->read;
201  
202 =back
203  
204 =item *
205  
206 Data lines are returned exactly as they were captured. You may want
207 to use C<chomp> on them if you don't want the end of line character(s)
208  
209 while (my $line = $capture->read) {
210 chomp $line;
211 $cat_line = join '', $cat_line, $line;
212 }
213  
214 =back
215  
216 =head2 line_pointer
217  
218 =over 4
219  
220 =item *
221  
222 Reads or sets the C<line_pointer>.
223  
224 my $current_line = $capture->line_pointer;
225 $capture->line_pointer(1);
226  
227 =back
228  
229 =head1 SUB-CLASSING
230  
231 =head2 Adding Features
232  
233 If you would like to sub-class this module to add a feature (method) or two,
234 here is a couple of easy steps. Also see L<IO::Capture::Overview>.
235  
236 =over 4
237  
238 =item 1
239  
240 Give your package a name
241  
242 package MyPackage;
243  
244 =item 2
245  
246 Use this C<IO::Capture::Stdout> as your base class like this:
247  
248 package MyPackage;
249  
250 use base qw/IO::Capture::Stdout/;
251  
252 =item 3
253  
254 Add your new method like this
255  
256 package MyPackage;
257  
258 use base qw/IO::Capture::Stdout/;
259  
260 sub grep {
261 my $self = shift;
262  
263 for $line (
264 }
265  
266 =back
267  
268 =head1 See Also
269  
270 L<IO::Capture::Overview>
271  
272 L<IO::Capture>
273  
274 L<IO::Capture::Stderr>
275  
276  
277 =head1 AUTHORS
278  
279 Mark Reynolds
280 reynolds@sgi.com
281  
282 Jon Morgan
283 jmorgan@sgi.com
284  
285 =head1 COPYRIGHT
286  
287 Copyright (c) 2003, Mark Reynolds. All Rights Reserved.
288 This module is free software. It may be used, redistributed
289 and/or modified under the same terms as Perl itself.
290  
291 =cut
« Capture/Stderr.pm ↑ Summary Capture/Tie_STDx.pm »