Capture/Stderr.pm

filter: Statementstmt 93.0% 40 / 43 Branchbran 62.5% 10 / 16 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 90.9% 10 / 11 Podpod n/a totaltotal 85.7% 60 / 70 CC 9 SCAR 9.7
CC
9
Cov
85%
_capture_warn_check
4.1
_start
4.1
_check_pre_conditions
3.0
CRAP 2.6
« Capture.pm ↑ Summary Capture/Stdout.pm »
1 package IO::Capture::Stderr;
2 6 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 6 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 6 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 6 ▶use base qw/IO::Capture/;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 6 ▶use IO::Capture::Tie_STDx;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 9 ▶sub _start {
Subroutine _start: called CC=3 SCAR=14.2
9 9 my $self = shift;
10 9 $self->line_pointer(1);
11  
12 9 ▶ if ( _capture_warn_check() ) {
Branch: 1 branch
BranchTrueFalseTotal
if (_capture_warn_check()) { } 0 9 9
13 0 ▶ $self->{'IO::Capture::handler_save'} = defined $SIG{__WARN__} ? $SIG{__WARN__} : 'DEFAULT';
Branch: 1 branch
BranchTrueFalseTotal
defined $SIG{'__WARN__'} ? : 0 0 0
14 0 ▶ $SIG{__WARN__} = sub {print STDERR @_;};
Subroutine __ANON__: not called CC=1 SCAR=6.9
15 }
16 else {
17 9 $self->{'IO::Capture::handler_save'} = undef;
18 }
19 9 tie *STDERR, "IO::Capture::Tie_STDx";
20 }
21  
22 8 ▶sub _retrieve_captured_text {
Subroutine _retrieve_captured_text: called CC=1 SCAR=0.0
23 8 my $self = shift;
24 8 my $messages = \@{$self->{'IO::Capture::messages'}};
25  
26 8 @$messages = <STDERR>;
27 8 return 1;
28 }
29  
30 11 ▶sub _check_pre_conditions {
Subroutine _check_pre_conditions: called CC=3 SCAR=11.0
31 11 my $self = shift;
32  
33 11 ▶ return unless $self->SUPER::_check_pre_conditions;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->SUPER::_check_pre_conditions 1 10 11
34  
35 10 ▶ if (tied *STDERR) {
Branch: 1 branch
BranchTrueFalseTotal
if (tied *STDERR) 1 9 10
36 1 carp "WARNING: STDERR already tied, unable to capture";
37 1 return;
38 }
39 9 return 1;
40 }
41  
42 8 ▶sub _stop {
Subroutine _stop: called CC=2 SCAR=7.0
43 8 my $self = shift;
44 8 untie *STDERR;
45 8 ▶ $SIG{__WARN__} = $self->{'IO::Capture::handler_save'} if defined $self->{'IO::Capture::handler_save'};
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->{"IO::Capture::handler_save"} 0 8 8
46 8 return 1;
47 }
48  
49 # _capture_warn_check
50 #
51 # Check to see if SIG{__WARN__} handler should be set to direct output
52 # from warn() to IO::Capture::Stderr.
53 # There are three things to take into consideration.
54 #
55 # 1) Is the version of perl less than 5.8?
56 # - Before 5.8, there was a bug that caused output from warn()
57 # not to be sent to STDERR if it (STDERR) was tied.
58 # So, we need to put a handler in to send warn() text to
59 # STDERR so IO::Capture::Stderr will capture it.
60 # 2) Is there a handler set already?
61 # - The default handler for SIG{__WARN__} is to send to STDERR.
62 # But, if it is set by the program, it may do otherwise, and
63 # we don't want to break that.
64 # 3) FORCE_CAPTURE_WARN => 1
65 # - To allow users to override a previous handler that was set on
66 # SIG{__WARN__}, there is a variable that can be set. If set,
67 # when there is a handler set on IO::Capture::Stderr startup,
68 # it will be saved and a new hander set that captures output to
69 # IO::Capture::Stderr. On stop, it will restore the programs
70 # handler.
71 #
72 #
73 #
74 # Perl | FORCE_CAPTURE_WARN | Program has | Set our own
75 # < 5.8 | is set | handler set | handler
76 # --------+----------------------+----------------+------------
77 # | | |
78 # --------+----------------------+----------------+------------
79 # X | | | X (1)
80 # --------+----------------------+----------------+------------
81 # | X | |
82 # --------+----------------------+----------------+------------
83 # X | X | | X (1)
84 # --------+----------------------+----------------+------------
85 # | | X |
86 # --------+----------------------+----------------+------------
87 # X | | X |
88 # --------+----------------------+----------------+------------
89 # | X | X | X (2)
90 # --------+----------------------+----------------+------------
91 # X | X | X | X (2)
92 # --------+----------------------+----------------+------------
93 # (1) WAR to get around bug
94 # (2) Replace programs handler with our own
95  
96 9 ▶sub _capture_warn_check {
Subroutine _capture_warn_check: called CC=4 SCAR=14.2
97 9 my $self = shift;
98  
99 9 ▶ if (!defined $SIG{__WARN__} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $SIG{"__WARN__"}) 7 2 9
100 7 ▶ return $^V lt v5.8 ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$^V lt v5.8 ? : 0 7 7
101 }
102 2 ▶ return $self->{'FORCE_CAPTURE_WARN'} ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$self->{'FORCE_CAPTURE_WARN'} ? : 0 2 2
103 }
104 6 1;
105  
106 __END__
« Capture.pm ↑ Summary Capture/Stdout.pm »