blib/lib/auto/share/dist/Cmd-Dwarf/app/lib/Dwarf/Trigger.pm untested

filter: Statementstmt 0.0% 0 / 44 Branchbran 0.0% 0 / 8 Conditioncond 0.0% 0 / 7 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 0.0% 0 / 8 Podpod 0.0% 0 / 3 totaltotal 0.0% 0 / 74 CC 5 SCAR 21.6
CC
5
Cov
0%
add_trigger
20.0
get_trigger_code
6.0
call_trigger
2.0
CRAP 8.7
1 package Dwarf::Trigger;
2 use Dwarf::Pragma;
3 use parent qw/Exporter/;
4 use Scalar::Util ();
5  
6 our @EXPORT = qw/add_trigger call_trigger get_trigger_code/;
7  
8 sub add_trigger {
9 my ($class, %args) = @_;
10 if (ref $class) {
11 while (my ($hook, $code) = each %args) {
12 next unless ref $code eq 'CODE';
13 push @{$class->{_trigger}->{$hook}}, $code;
14 }
15 } else {
16 no strict 'refs';
17 while (my ($hook, $code) = each %args) {
18 next unless ref $code eq 'CODE';
19 push @{${"${class}::_trigger"}->{$hook}}, $code;
20 }
21 }
22 }
23  
24 sub call_trigger {
25 my ($class, $hook, @args) = @_;
26 my @code = $class->get_trigger_code($hook);
27 for my $code (@code) {
28 $code->(@args);
29 }
30 }
31  
32 sub get_trigger_code {
33 my ($class, $hook) = @_;
34 my @code;
35 if (Scalar::Util::blessed($class)) {
36 push @code, @{ $class->{_trigger}->{$hook} || [] };
37 $class = ref $class;
38 }
39 no strict 'refs';
40 my $klass = ref $class || $class;
41 push @code, @{${"${klass}::_trigger"}->{$hook} || []};
42 return @code;
43 }
44  
45 1;