| 1 | package Module::Notify; | ||||||||||||||||||||
| 2 | |||||||||||||||||||||
| 3 | 1 | ▶ | use 5.008; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 4 | 1 | ▶ | use strict; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 5 | 1 | ▶ | use warnings; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 6 | 1 | ▶ | no warnings qw( void once uninitialized numeric ); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 7 | |||||||||||||||||||||
| 8 | 1 | ▶ | BEGIN { | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 9 | 1 | $Module::Notify::AUTHORITY = 'cpan:TOBYINK'; | |||||||||||||||||||
| 10 | 1 | $Module::Notify::VERSION = '0.002'; | |||||||||||||||||||
| 11 | } | ||||||||||||||||||||
| 12 | |||||||||||||||||||||
| 13 | 1 | ▶ | use Carp qw(croak); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 14 | 5 | ▶ | sub _refaddr { 0+$_[0] }; | ||||||||||||||||||
|
Subroutine
_refaddr: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 15 | |||||||||||||||||||||
| 16 | sub _module_notional_filename | ||||||||||||||||||||
| 17 | 3 | ▶ | { | ||||||||||||||||||
|
Subroutine
_module_notional_filename: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 18 | 3 | my $module = $_[0]; | |||||||||||||||||||
| 19 | 3 | $module =~ s{::}{/}g; | |||||||||||||||||||
| 20 | 3 | return "$module\.pm"; | |||||||||||||||||||
| 21 | } | ||||||||||||||||||||
| 22 | |||||||||||||||||||||
| 23 | sub _unmodule_notional_filename | ||||||||||||||||||||
| 24 | 3 | ▶ | { | ||||||||||||||||||
|
Subroutine
_unmodule_notional_filename: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 25 | 3 | my $module = $_[0]; | |||||||||||||||||||
| 26 | 3 | $module =~ s{\.pmc?$}{}; | |||||||||||||||||||
| 27 | 3 | $module =~ s{/}{::}g; | |||||||||||||||||||
| 28 | 3 | return $module; | |||||||||||||||||||
| 29 | } | ||||||||||||||||||||
| 30 | |||||||||||||||||||||
| 31 | sub _clean_err | ||||||||||||||||||||
| 32 | 1 | ▶ | { | ||||||||||||||||||
|
Subroutine
_clean_err: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 33 | 1 | chomp(my $err = shift); | |||||||||||||||||||
| 34 | 1 | $err =~ s/ at \(eval [0-9]+\) line [0-9]+\.?$//g; | |||||||||||||||||||
| 35 | 1 | return "$err"; | |||||||||||||||||||
| 36 | } | ||||||||||||||||||||
| 37 | |||||||||||||||||||||
| 38 | 1 | our %NOTIFICATIONS; | |||||||||||||||||||
| 39 | |||||||||||||||||||||
| 40 | sub new | ||||||||||||||||||||
| 41 | 3 | ▶ | { | ||||||||||||||||||
|
Subroutine
new: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||||||||||||
| 42 | 3 | my $class = shift; | |||||||||||||||||||
| 43 | 3 | my ($module, $callback) = @_; | |||||||||||||||||||
| 44 | |||||||||||||||||||||
| 45 | # Make sure hook is installed | ||||||||||||||||||||
| 46 | 3 | my $H = $class->_inc_hook; | |||||||||||||||||||
| 47 | 3 | ▶ | @INC = ($H, grep $_ != $H, @INC) unless $INC[0]==$H; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 48 | |||||||||||||||||||||
| 49 | 3 | ▶ | push @{$NOTIFICATIONS{$module} ||= []}, $callback; | ||||||||||||||||||
|
Condition100%2/2
$Module::Notify::NOTIFICATIONS{$module} ||= []
MC/DC100%1/1
$Module::Notify::NOTIFICATIONS{$module} ||= []
$Module::Notify::NOTIFICATIONS{$module}
Truth table100%2/2
A
A= $Module::Notify::NOTIFICATIONS{$module}
| |||||||||||||||||||||
| 50 | |||||||||||||||||||||
| 51 | return $class->_run_notifications(module => $module) | ||||||||||||||||||||
| 52 | 3 | ▶ | if $INC{_module_notional_filename($module)}; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 53 | |||||||||||||||||||||
| 54 | 3 | bless [ $module, _refaddr($callback) ], $class; | |||||||||||||||||||
| 55 | } | ||||||||||||||||||||
| 56 | |||||||||||||||||||||
| 57 | sub cancel | ||||||||||||||||||||
| 58 | 1 | ▶ | { | ||||||||||||||||||
|
Subroutine
cancel: called CC=2 SCAR=7.0
Pod
documented
| |||||||||||||||||||||
| 59 | 1 | my $self = shift; | |||||||||||||||||||
| 60 | 1 | my ($module, $refaddr) = @$self; | |||||||||||||||||||
| 61 | $NOTIFICATIONS{$module} = [ | ||||||||||||||||||||
| 62 | 1 | ▶ | grep _refaddr($_)!=$refaddr, @{$NOTIFICATIONS{$module} || []} | ||||||||||||||||||
|
Condition50%1/2
$Module::Notify::NOTIFICATIONS{$module} || []
MC/DC0%0/1
$Module::Notify::NOTIFICATIONS{$module} || []
$Module::Notify::NOTIFICATIONS{$module}
Truth table50%1/2
A
A= $Module::Notify::NOTIFICATIONS{$module}
| |||||||||||||||||||||
| 63 | ]; | ||||||||||||||||||||
| 64 | 1 | return; | |||||||||||||||||||
| 65 | } | ||||||||||||||||||||
| 66 | |||||||||||||||||||||
| 67 | sub _run_notifications | ||||||||||||||||||||
| 68 | 1 | ▶ | { | ||||||||||||||||||
|
Subroutine
_run_notifications: called CC=4 SCAR=14.0
| |||||||||||||||||||||
| 69 | 1 | my $class = shift; | |||||||||||||||||||
| 70 | 1 | my ($type, $module) = @_; | |||||||||||||||||||
| 71 | 1 | ▶ | $module = _unmodule_notional_filename($module) if $type eq "filename"; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 72 | |||||||||||||||||||||
| 73 | 1 | ▶ | while (my $code = shift @{$NOTIFICATIONS{$module} || []}) | ||||||||||||||||||
|
Branch: 1 branch
Condition50%1/2
$Module::Notify::NOTIFICATIONS{$module} || []
MC/DC0%0/1
$Module::Notify::NOTIFICATIONS{$module} || []
$Module::Notify::NOTIFICATIONS{$module}
Truth table50%1/2
A
A= $Module::Notify::NOTIFICATIONS{$module}
| |||||||||||||||||||||
| 74 | { | ||||||||||||||||||||
| 75 | 1 | $code->($module); | |||||||||||||||||||
| 76 | } | ||||||||||||||||||||
| 77 | |||||||||||||||||||||
| 78 | 1 | return; | |||||||||||||||||||
| 79 | } | ||||||||||||||||||||
| 80 | |||||||||||||||||||||
| 81 | sub _has_notifications | ||||||||||||||||||||
| 82 | 2 | ▶ | { | ||||||||||||||||||
|
Subroutine
_has_notifications: called CC=3 SCAR=11.3
| |||||||||||||||||||||
| 83 | 2 | my $class = shift; | |||||||||||||||||||
| 84 | 2 | my ($type, $module) = @_; | |||||||||||||||||||
| 85 | 2 | ▶ | $module = _unmodule_notional_filename($module) if $type eq "filename"; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 86 | |||||||||||||||||||||
| 87 | 2 | ▶ | !!@{$NOTIFICATIONS{$module} || []}; | ||||||||||||||||||
|
Condition50%1/2
$Module::Notify::NOTIFICATIONS{$module} || []
MC/DC0%0/1
$Module::Notify::NOTIFICATIONS{$module} || []
$Module::Notify::NOTIFICATIONS{$module}
Truth table50%1/2
A
A= $Module::Notify::NOTIFICATIONS{$module}
| |||||||||||||||||||||
| 88 | } | ||||||||||||||||||||
| 89 | |||||||||||||||||||||
| 90 | { | ||||||||||||||||||||
| 91 | 1 | my $hook; | |||||||||||||||||||
| 92 | sub _inc_hook | ||||||||||||||||||||
| 93 | 3 | ▶ | { | ||||||||||||||||||
|
Subroutine
_inc_hook: called CC=2 SCAR=7.0
| |||||||||||||||||||||
| 94 | 3 | my $class = shift; | |||||||||||||||||||
| 95 | 2 | ▶ | $hook ||= sub { | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=11.0
| |||||||||||||||||||||
| 96 | 2 | my $self = shift; | |||||||||||||||||||
| 97 | 2 | ▶ | return unless $class->_has_notifications(filename => $_[0]); | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 98 | 2 | @INC = grep $_ != $self, @INC; | |||||||||||||||||||
| 99 | 2 | my $r = eval "require '$_[0]'"; | |||||||||||||||||||
| 100 | 2 | unshift @INC, $self; | |||||||||||||||||||
| 101 | 2 | ▶ | $r ? $class->_run_notifications(filename => $_[0]) : croak(_clean_err $@); | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 102 | 1 | my @lines = ($r, undef); | |||||||||||||||||||
| 103 | 1 | ▶ | return sub { $_ = shift @lines; !!@lines }; | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 104 | 3 | ▶ | }; | ||||||||||||||||||
|
Condition66%2/3
$hook ||= sub {
my $self = shift();
return unless $class->_has_notifications("filename", $_[0]);
@INC = grep(($_ != $self), @INC);
my $r = eval "require '$_[0]'";
unshift @INC, $self;
$r ? $class->_run_notifications("filename", $_[0]) : croak(_clean_err($@));
my(@lines) = ($r, undef);
return sub {
$_ = shift @lines;
not !@lines;
}
;
}
MC/DC0%0/2
$hook ||= sub {
my $self = shift();
return unless $class->_has_notifications("filename", $_[0]);
@INC = grep(($_ != $self), @INC);
my $r = eval "require '$_[0]'";
unshift @INC, $self;
$r ? $class->_run_notifications("filename", $_[0]) : croak(_clean_err($@));
my(@lines) = ($r, undef);
return sub {
$_ = shift @lines;
not !@lines;
}
;
}
$hook
sub {
my $self = shift();
return unless $class->_has_notifications("filename", $_[0]);
@INC = grep(($_ != $self), @INC);
my $r = eval "require '$_[0]'";
unshift @INC, $self;
$r ? $class->_run_notifications("filename", $_[0]) : croak(_clean_err($@));
my(@lines) = ($r, undef);
return sub {
$_ = shift @lines;
not !@lines;
}
;
}
Truth table66%2/3
A ||= B
A= $hookB= sub {
my $self = shift();
return unless $class->_has_notifications("filename", $_[0]);
@INC = grep(($_ != $self), @INC);
my $r = eval "require '$_[0]'";
unshift @INC, $self;
$r ? $class->_run_notifications("filename", $_[0]) : croak(_clean_err($@));
my(@lines) = ($r, undef);
return sub {
$_ = shift @lines;
not !@lines;
}
;
}
| |||||||||||||||||||||
| 105 | } | ||||||||||||||||||||
| 106 | } | ||||||||||||||||||||
| 107 | |||||||||||||||||||||
| 108 | 1 | 1; | |||||||||||||||||||
| 109 | |||||||||||||||||||||
| 110 | __END__ | ||||||||||||||||||||