File Coverage

blib/lib/Devel/SummarizedWarnings.pm
Criterion Covered Total %
statement 22 43 51.1
branch 2 18 11.1
condition n/a
subroutine 7 8 87.5
pod 0 3 0.0
total 31 72 43.0


line stmt bran cond sub pod time code
1             package Devel::SummarizedWarnings;
2 1     1   35742 use strict;
  1         2  
  1         45  
3 1     1   5 use vars qw(@LOGGED_WARNINGS $INSTALLED_HANDLER $VERSION);
  1         2  
  1         174  
4              
5             sub install_handler {
6 2     2 0 16 my $old_handler = $SIG{'__WARN__'};
7             my $new_handler =
8             ( $old_handler
9 0     0   0 ? sub { &$old_handler; &append_to_warning_log; }
  0         0  
10 2 50       20 : \&append_to_warning_log );
11            
12 2         16 $INSTALLED_HANDLER = $SIG{'__WARN__'} = $new_handler;
13            
14 2         516 return;
15             }
16              
17             # Modify this globally
18             BEGIN {
19 1     1   5 $VERSION = 0.01;
20 1         8 install_handler();
21             }
22              
23             END {
24 1     1   595 dump_warnings();
25             }
26              
27             sub append_to_warning_log {
28 1     1 0 1255 push @LOGGED_WARNINGS, @_;
29             }
30              
31             sub dump_warnings {
32 1     1 0 6 local $^W;
33 1 50       7 if ( $SIG{'__WARN__'} != $INSTALLED_HANDLER ) {
34 0         0 push
35             @LOGGED_WARNINGS,
36             __PACKAGE__ . " was disabled prior to summarization\n";
37             }
38              
39             # Summarize the saved warnings
40 1         3 my %order;
41             my %sum;
42 1         5 while ( my $warning = shift @LOGGED_WARNINGS ) {
43 0         0 my $msg;
44             my $line;
45 0 0       0 if ( not $warning =~ /^(.*) at .+? line (\d+)\.$/s ) {
46 0         0 $warning =~ s/\n$//;
47 0         0 $msg = $warning;
48 0         0 $line = 'NoSuchLine';
49             } else {
50 0         0 $msg = $1;
51 0         0 $line = $2;
52             }
53 0         0 $sum{$msg}{$line}++;
54            
55 0 0       0 if ( not exists $order{$msg} ) {
56 0         0 $order{$msg} = 1 + %order;
57             }
58             }
59              
60             # Reformat the summarization
61 1         2 my @out;
62 1         8 for ( sort { $order{$a} <=> $order{$b} }
  0         0  
63             keys %order ) {
64 0         0 my $wrn = $sum{$_};
65 0 0       0 if ( exists $wrn->{'NoSuchLine'} ) {
66 0 0       0 push
67             @out,
68             $_
69             . ( $wrn->{'NoSuchLine'} > 1
70             ? " (x$wrn->{'NoSuchLine'})"
71             : '' );
72             } else {
73 0         0 push
74             @out,
75 0 0       0 "$_ on line@{[1 < keys %$wrn ? 's' : '']} "
76             . join( 2 == keys %$wrn ? ' and ' : ', ',
77 0 0       0 map "$_@{[ $wrn->{$_} == 1
78             ? ''
79             : qq[ (x$wrn->{$_})]]}",
80 0 0       0 sort { $a <=> $b }
81             keys %$wrn );
82             }
83             }
84            
85 1         4 local $\ = "\n";
86 1         9 print STDERR for @out;
87             }
88              
89             1;
90              
91             __END__