File Coverage

blib/lib/IO/Capture/Stderr.pm
Criterion Covered Total %
statement 39 42 92.8
branch 10 16 62.5
condition n/a
subroutine 10 11 90.9
pod n/a
total 59 69 85.5


line stmt bran cond sub pod time code
1             package IO::Capture::Stderr;
2 6     6   744835 use strict;
  6         17  
  6         259  
3 6     6   39 use warnings;
  6         13  
  6         206  
4 6     6   31 use Carp;
  6         12  
  6         731  
5 6     6   39 use base qw/IO::Capture/;
  6         12  
  6         8389  
6 6     6   3149 use IO::Capture::Tie_STDx;
  6         13  
  6         2222  
7              
8             sub _start {
9 9     9   17 my $self = shift;
10 9         67 $self->line_pointer(1);
11              
12 9 50       26 if ( _capture_warn_check() ) {
13 0 0       0 $self->{'IO::Capture::handler_save'} = defined $SIG{__WARN__} ? $SIG{__WARN__} : 'DEFAULT';
14 0     0   0 $SIG{__WARN__} = sub {print STDERR @_;};
  0         0  
15             }
16             else {
17 9         56 $self->{'IO::Capture::handler_save'} = undef;
18             }
19 9         73 tie *STDERR, "IO::Capture::Tie_STDx";
20             }
21              
22             sub _retrieve_captured_text {
23 8     8   11 my $self = shift;
24 8         13 my $messages = \@{$self->{'IO::Capture::messages'}};
  8         21  
25              
26 8         38 @$messages = ;
27 8         34 return 1;
28             }
29              
30             sub _check_pre_conditions {
31 11     11   19 my $self = shift;
32              
33 11 100       65 return unless $self->SUPER::_check_pre_conditions;
34              
35 10 100       35 if (tied *STDERR) {
36 1         199 carp "WARNING: STDERR already tied, unable to capture";
37 1         4 return;
38             }
39 9         36 return 1;
40             }
41              
42             sub _stop {
43 8     8   13 my $self = shift;
44 8         53 untie *STDERR;
45 8 50       30 $SIG{__WARN__} = $self->{'IO::Capture::handler_save'} if defined $self->{'IO::Capture::handler_save'};
46 8         30 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             sub _capture_warn_check {
97 9     9   15 my $self = shift;
98              
99 9 100       32 if (!defined $SIG{__WARN__} ) {
100 7 50       126 return $^V lt v5.8 ? 1 : 0;
101             }
102 2 50       10 return $self->{'FORCE_CAPTURE_WARN'} ? 1 : 0;
103             }
104             1;
105              
106             __END__