/usr/share/perl5/IO/Capture/Stderr.pm is in libio-capture-perl 0.05-4.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 | package IO::Capture::Stderr;
use strict;
use warnings;
use Carp;
use base qw/IO::Capture/;
use IO::Capture::Tie_STDx;
sub _start {
my $self = shift;
$self->line_pointer(1);
if ( _capture_warn_check() ) {
$self->{'IO::Capture::handler_save'} = defined $SIG{__WARN__} ? $SIG{__WARN__} : 'DEFAULT';
$SIG{__WARN__} = sub {print STDERR @_;};
}
else {
$self->{'IO::Capture::handler_save'} = undef;
}
tie *STDERR, "IO::Capture::Tie_STDx";
}
sub _retrieve_captured_text {
my $self = shift;
my $messages = \@{$self->{'IO::Capture::messages'}};
@$messages = <STDERR>;
return 1;
}
sub _check_pre_conditions {
my $self = shift;
return unless $self->SUPER::_check_pre_conditions;
if (tied *STDERR) {
carp "WARNING: STDERR already tied, unable to capture";
return;
}
return 1;
}
sub _stop {
my $self = shift;
untie *STDERR;
$SIG{__WARN__} = $self->{'IO::Capture::handler_save'} if defined $self->{'IO::Capture::handler_save'};
return 1;
}
# _capture_warn_check
#
# Check to see if SIG{__WARN__} handler should be set to direct output
# from warn() to IO::Capture::Stderr.
# There are three things to take into consideration.
#
# 1) Is the version of perl less than 5.8?
# - Before 5.8, there was a bug that caused output from warn()
# not to be sent to STDERR if it (STDERR) was tied.
# So, we need to put a handler in to send warn() text to
# STDERR so IO::Capture::Stderr will capture it.
# 2) Is there a handler set already?
# - The default handler for SIG{__WARN__} is to send to STDERR.
# But, if it is set by the program, it may do otherwise, and
# we don't want to break that.
# 3) FORCE_CAPTURE_WARN => 1
# - To allow users to override a previous handler that was set on
# SIG{__WARN__}, there is a variable that can be set. If set,
# when there is a handler set on IO::Capture::Stderr startup,
# it will be saved and a new hander set that captures output to
# IO::Capture::Stderr. On stop, it will restore the programs
# handler.
#
#
#
# Perl | FORCE_CAPTURE_WARN | Program has | Set our own
# < 5.8 | is set | handler set | handler
# --------+----------------------+----------------+------------
# | | |
# --------+----------------------+----------------+------------
# X | | | X (1)
# --------+----------------------+----------------+------------
# | X | |
# --------+----------------------+----------------+------------
# X | X | | X (1)
# --------+----------------------+----------------+------------
# | | X |
# --------+----------------------+----------------+------------
# X | | X |
# --------+----------------------+----------------+------------
# | X | X | X (2)
# --------+----------------------+----------------+------------
# X | X | X | X (2)
# --------+----------------------+----------------+------------
# (1) WAR to get around bug
# (2) Replace programs handler with our own
sub _capture_warn_check {
my $self = shift;
if (!defined $SIG{__WARN__} ) {
return $^V lt v5.8 ? 1 : 0;
}
return $self->{'FORCE_CAPTURE_WARN'} ? 1 : 0;
}
1;
__END__
=head1 NAME
C<IO::Capture::Stderr> - Capture all output sent to C<STDERR>
=head1 SYNOPSIS
use IO::Capture::Stderr;
$capture = IO::Capture::Stderr->new();
$capture->start(); # STDERR Output captured
print STDERR "Test Line One\n";
print STDERR "Test Line Two\n";
print STDERR "Test Line Three\n";
$capture->stop(); # STDERR output sent to wherever it was before 'start'
# In 'scalar context' returns next line
$line = $capture->read;
print "$line"; # prints "Test Line One"
$line = $capture->read;
print "$line"; # prints "Test Line Two"
# move line pointer to line 1
$capture->line_pointer(1);
$line = $capture->read;
print "$line"; # prints "Test Line One"
# Find out current line number
$current_line_position = $capture->line_pointer;
# In 'List Context' return an array(list)
@all_lines = $capture->read;
# Example 1 - "Using in module tests"
# Note: If you don't want to make users install
# the IO::Capture module just for your tests,
# you can just install in the t/lib directory
# of your module and use the lib pragma in
# your tests.
use lib "t/lib";
use IO::Capture:Stderr;
use Test::More;
# Create new capture object. Showing FORCE_CAPTURE_WARN being cleared
# for example, but 0 is the default, so you don't need to specify
# unless you want to set.
my $capture = IO::Capture:Stderr->new( {FORCE_CAPTURE_WARN => 0} );
$capture->start
# execute with a bad parameter to make sure get
# an error.
ok( ! $test("Bad Parameter") );
$capture->stop();
=head1 DESCRIPTION
The module C<IO::Capture::Stderr>, is derived from the abstract class C<IO::Capture>.
See L<IO::Capture>. The purpose of the module (as the name suggests) is to capture
any output sent to C<STDOUT>. After the capture is stopped, the STDOUT filehandle
will be reset to the previous location. E.g., If previously redirected to a file, when
C<IO::Capture-E<gt>stop> is called, output will start going into that file again.
Note: This module won't work with the perl function, system(), or any other operation
involving a fork(). If you want to capture the output from a system command,
it is faster to use open() or back-ticks.
my $output = `/usr/sbin/ls -l 2>&1`;
=head1 METHODS
=head2 new
=over 4
=item *
Creates a new capture object.
=item *
An object can be reused as needed, so will only need to do one of these.
=over 4
=item *
Be aware, any data previously captured will be discarded if a new
capture session is started.
=back
=back
=head2 start
=over 4
=item *
Start capturing data into the C<IO::Capture> Object.
=item *
Can B<not> be called on an object that is already capturing.
=item *
Can B<not> be called while STDERR tied to an object.
=item *
C<undef> will be returned on an error.
=back
=head2 stop
=over 4
=item *
Stop capturing data and point STDERR back to it's previous output location
I.e., untie STDERR
=back
=head2 read
=over 4
=item *
In I<Scalar Context>
=over 4
=item *
Lines are read from the buffer at the position of the C<line_pointer>,
and the pointer is incremented by one.
$next_line = $capture->read;
=back
=item *
In I<List Context>
=over 4
=item *
The array is returned. The C<line_pointer> is not affected.
@buffer = $capture->read;
=back
=item *
Data lines are returned exactly as they were captured. You may want
to use C<chomp> on them if you don't want the end of line character(s)
while (my $line = $capture->read) {
chomp $line;
$cat_line = join '', $cat_line, $line;
}
=back
=head2 line_pointer
=over 4
=item *
Reads or sets the C<line_pointer>.
my $current_line = $capture->line_pointer;
$capture->line_pointer(1);
=back
=head1 ARGUMENTS
Pass any arguments to new() in a single array reference.
IO::Capture::Stderr->new( {FORCE_CAPTURE_WARN => 1} );
=head2 FORCE_CAPTURE_WARN
=over 4
Normally, IO::Capture::Stderr will capture text from I<warn()> function calls. This is because output
from I<warn()> is normally directed to STDERR. If you wish to force IO::Capture::Stderr to grab the
text from I<warn()>, set FORCE_CAPTURE_WARN to a 1. Then C<IO::Capture::Stderr> will save the handle
that C<$SIG{__WARN__}> was set to, redirect it to itself on C<start()>, and then set C<$SIG{__WARN__}>
back after C<stop()> is called.
=back
=head1 SUB-CLASSING
=head2 Adding Features
If you would like to sub-class this module to add a feature (method) or two,
here is a couple of easy steps. Also see L<IO::Capture::Overview>.
=over 4
=item 1
Give your package a name
package MyPackage;
=item 2
Use this C<IO::Capture::Stderr> as your base class like this:
package MyPackage;
use base qw/IO::Capture::Stderr/;
=item 3
Add your new method like this
package MyPackage;
use base qw/IO::Capture::Stderr/;
sub grep {
my $self = shift;
for $line (
}
=back
=head1 See Also
L<IO::Capture::Overview>
L<IO::Capture>
L<IO::Capture::Stdout>
=head1 AUTHORS
Mark Reynolds
reynolds@sgi.com
Jon Morgan
jmorgan@sgi.com
=head1 COPYRIGHT
Copyright (c) 2003, Mark Reynolds. All Rights Reserved.
This module is free software. It may be used, redistributed
and/or modified under the same terms as Perl itself.
=cut
|