line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package IO::Capture::Stdout; |
2
|
6
|
|
|
6
|
|
133448
|
use Carp; |
|
6
|
|
|
|
|
16
|
|
|
6
|
|
|
|
|
584
|
|
3
|
6
|
|
|
6
|
|
33
|
use base qw/IO::Capture/; |
|
6
|
|
|
|
|
9
|
|
|
6
|
|
|
|
|
3648
|
|
4
|
6
|
|
|
6
|
|
3634
|
use IO::Capture::Tie_STDx; |
|
6
|
|
|
|
|
12
|
|
|
6
|
|
|
|
|
1559
|
|
5
|
|
|
|
|
|
|
|
6
|
|
|
|
|
|
|
sub _start { |
7
|
9
|
|
|
9
|
|
18
|
my $self = shift; |
8
|
9
|
|
|
|
|
64
|
$self->line_pointer(1); |
9
|
9
|
|
|
|
|
56
|
tie *STDOUT, "IO::Capture::Tie_STDx"; |
10
|
|
|
|
|
|
|
} |
11
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
sub _retrieve_captured_text { |
13
|
9
|
|
|
9
|
|
14
|
my $self = shift; |
14
|
9
|
|
|
|
|
15
|
my $messages = \@{$self->{'IO::Capture::messages'}}; |
|
9
|
|
|
|
|
21
|
|
15
|
|
|
|
|
|
|
|
16
|
9
|
|
|
|
|
41
|
@$messages = ; |
17
|
|
|
|
|
|
|
#$self->line_pointer(1); |
18
|
9
|
|
|
|
|
39
|
return 1; |
19
|
|
|
|
|
|
|
} |
20
|
|
|
|
|
|
|
|
21
|
|
|
|
|
|
|
sub _check_pre_conditions { |
22
|
12
|
|
|
12
|
|
19
|
my $self = shift; |
23
|
|
|
|
|
|
|
|
24
|
12
|
100
|
|
|
|
68
|
return unless $self->SUPER::_check_pre_conditions; |
25
|
|
|
|
|
|
|
|
26
|
10
|
100
|
|
|
|
34
|
if (tied *STDOUT) { |
27
|
1
|
|
|
|
|
175
|
carp "WARNING: STDOUT already tied, unable to capture"; |
28
|
1
|
|
|
|
|
35
|
return; |
29
|
|
|
|
|
|
|
} |
30
|
9
|
|
|
|
|
34
|
return 1; |
31
|
|
|
|
|
|
|
} |
32
|
|
|
|
|
|
|
|
33
|
|
|
|
|
|
|
sub _stop { |
34
|
9
|
|
|
9
|
|
103
|
untie *STDOUT; |
35
|
|
|
|
|
|
|
} |
36
|
|
|
|
|
|
|
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, is derived from the abstract class C. |
101
|
|
|
|
|
|
|
See L. The purpose of the module (as the name suggests) is to capture |
102
|
|
|
|
|
|
|
any output sent to C. 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
|
|
|
|
|
|
|
Cstop> 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 Object. |
145
|
|
|
|
|
|
|
|
146
|
|
|
|
|
|
|
=item * |
147
|
|
|
|
|
|
|
|
148
|
|
|
|
|
|
|
Can B be called on an object that is already capturing. |
149
|
|
|
|
|
|
|
|
150
|
|
|
|
|
|
|
=item * |
151
|
|
|
|
|
|
|
|
152
|
|
|
|
|
|
|
Can B be called while STDOUT tied to an object. |
153
|
|
|
|
|
|
|
|
154
|
|
|
|
|
|
|
=item * |
155
|
|
|
|
|
|
|
|
156
|
|
|
|
|
|
|
C 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 |
178
|
|
|
|
|
|
|
|
179
|
|
|
|
|
|
|
=over 4 |
180
|
|
|
|
|
|
|
|
181
|
|
|
|
|
|
|
=item * |
182
|
|
|
|
|
|
|
|
183
|
|
|
|
|
|
|
Lines are read from the buffer at the position of the C, |
184
|
|
|
|
|
|
|
and the pointer is incremented by one. |
185
|
|
|
|
|
|
|
|
186
|
|
|
|
|
|
|
$next_line = $capture->read; |
187
|
|
|
|
|
|
|
|
188
|
|
|
|
|
|
|
=back |
189
|
|
|
|
|
|
|
|
190
|
|
|
|
|
|
|
=item * |
191
|
|
|
|
|
|
|
|
192
|
|
|
|
|
|
|
In I |
193
|
|
|
|
|
|
|
|
194
|
|
|
|
|
|
|
=over 4 |
195
|
|
|
|
|
|
|
|
196
|
|
|
|
|
|
|
=item * |
197
|
|
|
|
|
|
|
|
198
|
|
|
|
|
|
|
The array is returned. The C 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 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. |
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. |
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 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 |
271
|
|
|
|
|
|
|
|
272
|
|
|
|
|
|
|
L |
273
|
|
|
|
|
|
|
|
274
|
|
|
|
|
|
|
L |
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 |