File Coverage

blib/lib/Log/Sigil.pm
Criterion Covered Total %
statement 38 38 100.0
branch 9 10 90.0
condition n/a
subroutine 7 7 100.0
pod 2 2 100.0
total 56 57 98.2


line stmt bran cond sub pod time code
1             package Log::Sigil;
2 7     7   47906 use strict;
  7         10  
  7         268  
3 7     7   31 use warnings;
  7         10  
  7         158  
4 7     7   30 use Exporter "import";
  7         21  
  7         175  
5 7     7   36 use List::Util qw( max );
  7         10  
  7         796  
6              
7 7     7   35 use constant DEBUG => 0;
  7         9  
  7         4245  
8              
9             our @EXPORT = qw( swarn swarn2 );
10             our $VERSION = "1.02";
11              
12             our @SIGILS = (
13             qw(
14             =
15             +
16             !
17             @
18             ),
19             q{#},
20             qw(
21             $
22             %
23             ^
24             &
25             *
26             -
27             |
28             \
29             ~
30             ?
31             ),
32             );
33             our $TIMES = 3;
34             our $SEPARATOR = q{ };
35             our $BIAS = 0;
36             our %INDEX = ( "main::" => 0 ); # Ensure `values` + 1 is the next.
37             my $ANON_REGEX = qr{ (?: .*::__ANON__ | [(]eval[)] ) \z}msx;
38              
39             sub swarn {
40 16     16 1 2970 my $nth = 0;
41 16         22 my $bias = 1;
42              
43 16         88 $nth++
44             while caller $nth;
45              
46 16         112 my( $package, $filename, $line, $subroutine ) = caller $nth - $bias - $BIAS;
47              
48 16         29 $bias++;
49              
50 16 100       60 $subroutine = "main::"
51             if $subroutine eq join q{::}, __PACKAGE__, "swarn";
52              
53 16 100       158 $subroutine = "${subroutine}::$line"
54             if $subroutine =~ m{$ANON_REGEX};
55              
56 16 50       129 $bias++
57             if $subroutine =~ m{$ANON_REGEX};
58              
59 16 100       123 if ( my @list = caller $nth - $bias - $BIAS ) {
60 14         25 ( undef, undef, $line ) = @list;
61             }
62              
63 16         22 warn "\$package:\t$package" if DEBUG;
64 16         17 warn "\$filename:\t$filename" if DEBUG;
65 16         15 warn "\$line:\t$line" if DEBUG;
66 16         17 warn "\$subroutine:\t$subroutine" if DEBUG;
67              
68 16 100       70 unless ( exists $INDEX{ $subroutine } ) {
69 8         80 $INDEX{ $subroutine } = max( values %INDEX ) + 1;
70             }
71              
72 16         42 my $sigil = $SIGILS[ $INDEX{ $subroutine } % @SIGILS ];
73 16         24 warn "\$sigil:\t$sigil" if DEBUG;
74 16         53 unshift @_, $sigil x $TIMES, $SEPARATOR;
75 16         52 push @_, " by ${filename}[$line]: $subroutine\n"; # Ignore if original has \n at the end.
76              
77 16         94 warn @_;
78             }
79              
80             sub swarn2 {
81 2     2 1 656 local $BIAS = $BIAS + 1;
82 2         3 &swarn;
83             }
84              
85             1;
86             __END__