blib/lib/Carp/Growl.pm

filter: Statementstmt 96.3% 157 / 163 Branchbran 81.8% 36 / 44 Conditioncond 100% 3 / 3 MC/DCmcdc 100% 2 / 2 Subroutinesub 93.1% 27 / 29 Podpod n/a totaltotal 93.3% 225 / 241 CC 30 SCAR 11.6
CC
30
Cov
93%
_local_import
6.0
_local_unimport
5.0
_global_import
5.0
CRAP 3.2
↑ Summary
1 package Carp::Growl;
2  
3 11 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 11 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 11 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 11 ▶use version; our $VERSION = '0.0.10';
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 11 ▶use Growl::Any;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 11 my $build_warnmess;
12  
13 11 my $DEFAULT_FUNCS = +{};
14  
15 11 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.2
16 30 ▶ $build_warnmess = sub {
Subroutine __ANON__: called CC=4 SCAR=14.6
17 30 my ( $func, @args ) = @_;
18 30 my ( $pkg, $file, $line ) = caller(1);
19 30 my $self;
20 30 ▶ unless (@args) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@args) 0 30 30
21 0 ▶ unshift @args,
Branch: 1 branch
BranchTrueFalseTotal
$func eq 'warn' ? : 0 0 0
22 ( $func eq 'warn' ? "Warning: something's wrong" : "Died" );
23 }
24 30 $self = join( $", @args );
25 30 ▶ unless ( $self =~ s/\n \z//msx ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self =~ s/\n \z//msx) 28 2 30
26 28 $self .= " at $file line $line.";
27 }
28 30 $self;
29 11 };
30 }
31  
32 11 ▶if ( $] < 5.016000 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($] < 5.016) { } 0 11 11
33 0 @{$DEFAULT_FUNCS}{qw/warn die/} = (
34 0 ▶ sub { CORE::warn( $build_warnmess->( 'warn', @_ ), $/ ); },
Subroutine __ANON__: not called CC=1 SCAR=6.9
35 0 ▶ sub { CORE::die( $build_warnmess->( 'die', @_ ), $/ ); },
Subroutine __ANON__: not called CC=1 SCAR=6.9
36 0 );
37 }
38 else {
39 11 $DEFAULT_FUNCS->{warn} = \&CORE::warn;
40 11 $DEFAULT_FUNCS->{die} = \&CORE::die;
41 }
42  
43 11 my $g = Growl::Any->new( appname => __PACKAGE__, events => [qw/warn die/] );
44  
45 11 our @CARP_NOT;
46  
47 11 my $KEEP = {};
48 11 my $imported = 0;
49  
50 11 my $AVAILABLE_IMPORT_ARGS = [qw/global/];
51  
52 7 ▶my $validate_args = sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
53 7 my %bads;
54 7 for my $good (@$AVAILABLE_IMPORT_ARGS) {
55 7 $bads{$_}++ for grep { $_ ne $good } @_;
56 }
57 7 keys %bads;
58 11 };
59 11 for my $f (qw/carp croak/) {
60 11 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
61 22 $DEFAULT_FUNCS->{$f} = *{ 'Carp::' . $f }{CODE};
62 }
63 11 my $BUILD_FUNC_ARGS = +{
64 warn => { event => 'warn', title => 'WARNING', },
65 die => { event => 'die', title => 'FATAL', },
66 carp => { event => 'warn', title => 'WARNING', },
67 croak => { event => 'die', title => 'FATAL', },
68 };
69  
70 56 ▶sub _build_func {
Subroutine _build_func: called CC=3 SCAR=11.0
71 56 my $func = shift;
72 56 ▶ if ( ( $func eq 'warn' || $func eq 'die' ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($func eq 'warn' or $func eq 'die') { } 28 28 56
Condition100%3/3
$func eq "warn" or $func eq "die"
l!l&&r!l&&!r
141428
MC/DC100%2/2
$func eq "warn" or $func eq "die"
$func eq "warn" $func eq "die"
Truth table100%3/3
A or B
A= $func eq "warn"B= $func eq "die"
ABresult
1X1
011
000
73 30 ▶ return sub {
Subroutine __ANON__: called CC=2 SCAR=7.1
74 $g->notify(
75 $BUILD_FUNC_ARGS->{$func}->{event}, # event
76 $BUILD_FUNC_ARGS->{$func}->{title}, # title
77 30 ▶ $build_warnmess->( $func, @_ ), # message
Branch: 1 branch
BranchTrueFalseTotal
if defined $^S 30 0 30
78 undef, # icon
79 )
80 if defined $^S;
81 30 goto &{ $DEFAULT_FUNCS->{$func} };
82 28 };
83 }
84 else {
85 30 ▶ return sub {
Subroutine __ANON__: called CC=2 SCAR=7.0
86 11 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
87 # if ( caller eq 'main' ) {
88 # local *{'main::CARP_NOT'};
89 # push @{ *{'main::CARP_NOT'} }, __PACKAGE__;
90 # }
91 30 my $msg = Carp::shortmess(@_);
92 30 chomp $msg;
93 $g->notify(
94 $BUILD_FUNC_ARGS->{$func}->{event}, # event
95 $BUILD_FUNC_ARGS->{$func}->{title}, # title
96 30 ▶ $msg, # message
Branch: 1 branch
BranchTrueFalseTotal
if defined $^S 30 0 30
97 undef, # icon
98 )
99 if defined $^S;
100 30 goto &{ $DEFAULT_FUNCS->{$func} };
101 28 };
102 }
103 }
104  
105 ##~~~~~ IMPORT ~~~~~##
106  
107 16 ▶sub import {
Subroutine import: called CC=4 SCAR=13.9
108 16 my $self = shift;
109 16 my @args = @_;
110 16 ▶ if (@args) {
Branch: 1 branch
BranchTrueFalseTotal
if (@args) { } 7 9 16
111 7 my @bads = $validate_args->(@args);
112 7 ▶ CORE::die 'Illegal args: "'
Branch: 1 branch
BranchTrueFalseTotal
if @bads 2 5 7
113 . join( '", "', @bads )
114 . '" for import()'
115 if @bads;
116 5 $imported = 2;
117 5 ▶ goto &_global_import if grep { $_ eq 'global' } @args;
Branch: 1 branch
BranchTrueFalseTotal
if grep {$_ eq "global";} @args 5 0 5
118 }
119 else {
120 9 $imported = 1;
121 }
122 9 goto &_local_import;
123 }
124  
125 14 ▶sub _local_import {
Subroutine _local_import: called CC=6 SCAR=17.9
126 14 ▶ my $args = @_ ? \@_ : [ keys %$BUILD_FUNC_ARGS ];
Branch: 1 branch
BranchTrueFalseTotal
@_ ? : 5 9 14
127 14 my $pkg = caller();
128 11 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
129 14 for my $func ( keys %$BUILD_FUNC_ARGS ) {
130 8 $KEEP->{$pkg}->{$func} = \&{ *{ $pkg . '::' . $func } }
131 56 ▶ if defined *{ $pkg . '::' . $func }{CODE};
Branch: 1 branch
BranchTrueFalseTotal
if defined *{$pkg . '::' . $func;}{"CODE"} 8 48 56
132 }
133 14 for my $func (@$args) {
134 11 ▶ no warnings 'redefine';
Subroutine BEGIN: called CC=1 SCAR=0.0
135 46 *{ $pkg . '::' . $func } = _build_func($func);
136 }
137 14 ▶ push @CARP_NOT, $pkg if $pkg ne 'main';
Branch: 1 branch
BranchTrueFalseTotal
if $pkg ne "main" 7 7 14
138 }
139  
140 5 ▶sub _global_import {
Subroutine _global_import: called CC=5 SCAR=16.1
141 11 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
142 11 ▶ no warnings 'redefine';
Subroutine BEGIN: called CC=1 SCAR=0.0
143 5 my $pkg = caller;
144 5 for my $func (qw/warn die/) {
145 2 $KEEP->{'CORE::GLOBAL'}->{$func} = \&{ *{ 'CORE::GLOBAL::' . $func } }
146 10 ▶ if defined *{ 'CORE::GLOBAL::' . $func }{CODE};
Branch: 1 branch
BranchTrueFalseTotal
if defined *{'CORE::GLOBAL::' . $func;}{"CODE"} 2 8 10
147 10 *{ 'CORE::GLOBAL::' . $func } = _build_func($func);
148 2 $KEEP->{$pkg}->{$func} = \&{ *{ $pkg . '::' . $func } }
149 10 ▶ if defined *{ $pkg . '::' . $func }{CODE};
Branch: 1 branch
BranchTrueFalseTotal
if defined *{$pkg . '::' . $func;}{"CODE"} 2 8 10
150 2 undef &{ *{ $pkg . '::' . $func } }
151 10 ▶ if defined *{ $pkg . '::' . $func }{CODE};
Branch: 1 branch
BranchTrueFalseTotal
if defined *{$pkg . '::' . $func;}{"CODE"} 2 8 10
152 }
153 5 push @Carp::CARP_NOT, __PACKAGE__;
154 5 @_ = qw/carp croak/;
155 5 goto &_local_import;
156 }
157  
158 ##~~~~~ UNIMPORT ~~~~~##
159  
160 6 ▶sub unimport {
Subroutine unimport: called CC=2 SCAR=6.9
161 6 my $self = shift;
162 6 my @args = @_;
163 6 ▶ CORE::die 'Illegal args: "' . join( '", "', @args ) . '" for unimport()'
Branch: 1 branch
BranchTrueFalseTotal
if @args 1 5 6
164 if @args;
165 5 $imported = 0;
166 5 goto &_global_unimport;
167 }
168  
169 5 ▶sub _local_unimport {
Subroutine _local_unimport: called CC=5 SCAR=16.1
170 5 ▶ my $args = @_ ? \@_ : [ keys %$BUILD_FUNC_ARGS ];
Branch: 1 branch
BranchTrueFalseTotal
@_ ? : 0 5 5
171 5 my ($pkg) = caller();
172 11 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
173 11 ▶ no warnings 'redefine';
Subroutine BEGIN: called CC=1 SCAR=0.0
174 5 for my $func (@$args) {
175 20 ▶ if ( $KEEP->{$pkg}->{$func} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($KEEP->{$pkg}{$func}) { } 8 12 20
176 8 *{ $pkg . '::' . $func } = $KEEP->{$pkg}->{$func};
177 }
178 else {
179 8 undef &{ *{ $pkg . '::' . $func } }
180 12 ▶ if defined *{ $pkg . '::' . $func }{CODE};
Branch: 1 branch
BranchTrueFalseTotal
if defined *{$pkg . '::' . $func;}{"CODE"} 8 4 12
181 }
182 20 @{ *{ $pkg . '::CARP_NOT' } }
183 0 = grep { $_ ne __PACKAGE__ } @{ *{ $pkg . '::CARP_NOT' } };
184 }
185 }
186  
187 5 ▶sub _global_unimport {
Subroutine _global_unimport: called CC=4 SCAR=13.9
188 11 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
189 11 ▶ no warnings 'redefine';
Subroutine BEGIN: called CC=1 SCAR=0.0
190 5 for my $func (qw/warn die/) {
191 10 ▶ if ( $KEEP->{'CORE::GLOBAL'}->{$func} ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($KEEP->{'CORE::GLOBAL'}{$func}) { } 2 8 10
elsif (defined *{'CORE::GLOBAL::' . $func;}{'CODE'}) { } 4 4 8
192 2 *{ 'CORE::GLOBAL::' . $func } = $KEEP->{'CORE::GLOBAL'}->{$func};
193 }
194 8 elsif ( defined *{ 'CORE::GLOBAL::' . $func }{CODE} ) {
195 4 undef &{ *{ 'CORE::GLOBAL::' . $func } };
196 }
197 }
198 # @_ = qw/carp croak/;
199 5 goto &_local_unimport;
200 }
201  
202 11 1; # Magic true value required at end of module
203 __END__
↑ Summary