File Coverage

blib/lib/Tie/STDERR.pm
Criterion Covered Total %
statement 27 55 49.0
branch 10 26 38.4
condition 2 3 66.6
subroutine 6 10 60.0
pod 0 3 0.0
total 45 97 46.3


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