blib/lib/Tie/STDERR.pm

filter: Statementstmt 55.2% 37 / 67 Branchbran 39.2% 11 / 28 Conditioncond 66.6% 2 / 3 MC/DCmcdc 50.0% 1 / 2 Subroutinesub 50.0% 5 / 10 Podpod 0.0% 0 / 3 totaltotal 49.5% 56 / 113 CC 16 SCAR 27.9
CC
16
Cov
51%
process_result
29.7
import
20.0
PRINT
2.0
CRAP 16.4
↑ Summary
1  
2 package Tie::STDERR;
3  
4 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 1 my $stderr = ''; ### here we store the STDERR output
7  
8 1 my $need_apache_cleanup = 0;
9 1 ▶if (defined $ENV{'MOD_PERL'})
Branch: 1 branch
BranchTrueFalseTotal
if (defined $ENV{"MOD_PERL"}) 0 1 1
10 0 { $need_apache_cleanup = 1; }
11  
12 sub TIEHANDLE
13 1 ▶ {
Subroutine TIEHANDLE: called CC=1 SCAR=0.0
14 1 my $class = shift;
15 1 bless {}, $class;
16 }
17 sub PRINT
18 0 ▶ {
Subroutine PRINT: not called CC=1 SCAR=6.9
19 0 my $self = shift;
20 0 $stderr .= join '', @_;
21 }
22  
23 1 my $default_user = 'root'; ### change this to 'root'
24 1 my $default_subject = 'STDERR output detected'; ### change this to your Subject
25 1 my $default_mail = '| /usr/lib/sendmail -t'; ### default command
26 1 my $run_function = undef;
27 1 my $append_scalar = undef;
28  
29 1 my ($user, $subject, $command);
30  
31 0 ▶sub error_id {
Subroutine error_id: not called CC=1 SCAR=6.9
Pod undocumented
32 0 my @localtime = localtime;
33 0 sprintf "%04d%02d%02d-%02d%02d%02d-%05d", $localtime[5] + 1900,
34 $localtime[4] + 1, @localtime[3, 2, 1, 0], $$;
35 }
36  
37 1 ▶sub process_result {
Subroutine process_result: called CC=6 SCAR=33.9
Pod undocumented
38 ### local *STDERR; untie *STDERR;
39 1 local $ENV{'PATH'} = '/bin:/usr/bin:/usr/local/bin';
40 1 ▶ if ($stderr ne '')
Branch: 1 branch
BranchTrueFalseTotal
if ($stderr ne "") 0 1 1
41 {
42 0 ▶ if (defined $run_function)
Branch: 3 branches
BranchTrueFalseTotal
if (defined $run_function) { } 0 0 0
elsif (defined $append_scalar) { } 0 0 0
elsif (defined $command) { } 0 0 0
43 0 { &$run_function($stderr); }
44 elsif (defined $append_scalar)
45 0 { $$append_scalar .= $stderr; }
46 elsif (defined $command)
47 {
48 0 open OUT, $command;
49 0 print OUT $stderr;
50 }
51 else
52 {
53 0 open OUT, $default_mail;
54 ### print STDERR "Sending bug by email\n";
55 0 my $now = error_id;
56 0 print OUT "To: $user\nSubject: $subject\n\nOutput to STDERR detected in $0:\n", $stderr, "\n\nTime: $now\n\n";
57 0 print OUT "\%ENV:\n";
58 0 for (sort keys %ENV) { print OUT "$_ = $ENV{$_}\n"; }
59 }
60 0 close OUT;
61 0 $stderr = '';
62 }
63 }
64  
65 1 ▶END {
Subroutine END: called CC=1 SCAR=0.0
66 1 process_result();
67 }
68 0 ▶sub register_apache_cleanup {
Subroutine register_apache_cleanup: not called CC=1 SCAR=6.9
Pod undocumented
69 0 eval 'use Apache; my $r = Apache->request; $r->register_cleanup(\&process_result); ';
70 }
71 2 ▶sub import {
Subroutine import: called CC=11 SCAR=29.9
72 ### print STDERR "Tie::STDERR::import(@_) called\n";
73 2 my $class = shift;
74  
75 2 ▶ if (@_ and not defined $_[0]) ### explicit undef
Branch: 1 branch
BranchTrueFalseTotal
if (@_ and not defined $_[0]) 1 1 2
Condition66%2/3
@_ and not defined $_[0]
!ll&&!rl&&r
011
MC/DC50%1/2
@_ and not defined $_[0]
@_ not defined $_[0]
Truth table66%2/3
A and not(B)
A= @_B= not defined $_[0]
ABresult
0X0
100
111
76 {
77 1 $^W = 0;
78 1 ▶ untie *STDERR if ref tied *STDERR eq 'Tie::STDERR';
Branch: 1 branch
BranchTrueFalseTotal
if ref tied *STDERR eq "Tie::STDERR" 1 0 1
79 1 $run_function = undef; $append_scalar = undef;
80 1 return;
81 }
82  
83 ### return if -t STDERR or -t STDOUT;
84  
85 1 ▶ unless (ref tied *STDERR eq 'Tie::STDERR')
Branch: 1 branch
BranchTrueFalseTotal
unless (ref tied *STDERR eq "Tie::STDERR") 1 0 1
86 {
87 1 tie *STDERR, __PACKAGE__;
88  
89 0 ▶ $SIG{__WARN__} = sub { print STDERR @_; };
Subroutine __ANON__: not called CC=1 SCAR=1.2
90  
91 0 ▶ $SIG{__DIE__} = sub { print STDERR @_; };
Subroutine __ANON__: not called CC=1 SCAR=1.2
92 }
93  
94 1 ($user, $subject, $command) = ($default_user, $default_subject, undef);
95  
96 1 ▶ return unless @_;
Branch: 1 branch
BranchTrueFalseTotal
unless @_ 0 1 1
97  
98 1 ▶ if (ref $_[0] eq 'CODE')
Branch: 2 branches
BranchTrueFalseTotal
if (ref $_[0] eq 'CODE') { } 0 1 1
elsif (ref $_[0] eq 'SCALAR') { } 0 1 1
99 0 { $run_function = $_[0]; }
100 elsif (ref $_[0] eq 'SCALAR')
101 0 { $append_scalar = $_[0]; }
102 else {
103 1 my $arg = shift;
104 1 ▶ if ($arg =~ /^\s*([|>].*)/s)
Branch: 1 branch
BranchTrueFalseTotal
if ($arg =~ /^\s*([|>].*)/s) { } 1 0 1
105 1 { $command = $1; }
106 else
107 {
108 0 $arg =~ s/\n$//;
109 0 $user = $arg;
110 0 $arg = shift;
111 0 ▶ if (defined $arg)
Branch: 1 branch
BranchTrueFalseTotal
if (defined $arg) 0 0 0
112 0 { $arg =~ s/\n$//; $subject = $arg; }
113 }
114 }
115 1 ▶ if ($need_apache_cleanup) {
Branch: 1 branch
BranchTrueFalseTotal
if ($need_apache_cleanup) 0 1 1
116 0 register_apache_cleanup();
117 }
118 }
119  
120 1 $Tie::STDERR::VERSION = '0.26';
121  
122 1 1;
123  
124 __END__
↑ Summary