| 1 | # PiFlash::Hook - named dispatch/hook library for PiFlash | ||||||||||||||||||||||||||||
| 2 | # by Ian Kluft | ||||||||||||||||||||||||||||
| 3 | |||||||||||||||||||||||||||||
| 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 | ▶ | use v5.14.0; # require 2011 or newer version of Perl | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||
| 8 | package PiFlash::Hook; | ||||||||||||||||||||||||||||
| 9 | 1 | $PiFlash::Hook::VERSION = '0.4.3'; | |||||||||||||||||||||||||||
| 10 | 1 | ▶ | use Carp qw(confess); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | 1 | ▶ | use autodie; # report errors instead of silently continuing ("die" actions are used as exceptions - caught & reported) | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | 1 | ▶ | use parent 'PiFlash::Object'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 13 | 1 | ▶ | use PiFlash::State; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | # ABSTRACT: named dispatch/hook library for PiFlash | ||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | # initialize hooks hash as empty | ||||||||||||||||||||||||||||
| 19 | ## no critic (ProhibitPackageVars) | ||||||||||||||||||||||||||||
| 20 | 1 | our %hooks; | |||||||||||||||||||||||||||
| 21 | ## use critic | ||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||
| 23 | # required parameter list | ||||||||||||||||||||||||||||
| 24 | # used by PiFlash::Object for new() method | ||||||||||||||||||||||||||||
| 25 | sub object_params | ||||||||||||||||||||||||||||
| 26 | 0 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
object_params: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 27 | 0 | return qw(name code origin); | |||||||||||||||||||||||||||
| 28 | } | ||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||
| 30 | # use AUTOLOAD to call a named hook as if it were a class method | ||||||||||||||||||||||||||||
| 31 | 1 | our $AUTOLOAD; | |||||||||||||||||||||||||||
| 32 | 0 | ▶ | sub AUTOLOAD { | ||||||||||||||||||||||||||
|
Subroutine
AUTOLOAD: not called CC=4 SCAR=30.0
| |||||||||||||||||||||||||||||
| 33 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||
| 35 | # Remove qualifier from original method name... | ||||||||||||||||||||||||||||
| 36 | 0 | my $called = $AUTOLOAD =~ s/.*:://r; | |||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||
| 38 | # differentiate between class and instance methods | ||||||||||||||||||||||||||||
| 39 | 0 | ▶ | if (defined $self and ref $self eq "PiFlash::Hook") { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
defined $self and ref $self eq "PiFlash::Hook"
MC/DC0%0/2
defined $self and ref $self eq "PiFlash::Hook"
defined $self
ref $self eq "PiFlash::Hook"
Truth table0%0/3
A and B
A= defined $selfB= ref $self eq "PiFlash::Hook"
| |||||||||||||||||||||||||||||
| 40 | # handle instance accessor | ||||||||||||||||||||||||||||
| 41 | # if likely to be used a lot, optimize this by creating accessor function upon first access | ||||||||||||||||||||||||||||
| 42 | 0 | ▶ | if (exists $self->{$called}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 43 | 0 | return $self->{$called}; | |||||||||||||||||||||||||||
| 44 | } | ||||||||||||||||||||||||||||
| 45 | 0 | return; | |||||||||||||||||||||||||||
| 46 | } else { | ||||||||||||||||||||||||||||
| 47 | # autoloaded class methods run hooks by name | ||||||||||||||||||||||||||||
| 48 | 0 | run($called, @_); | |||||||||||||||||||||||||||
| 49 | } | ||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||
| 51 | } | ||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||
| 53 | # add a code reference to a named hook | ||||||||||||||||||||||||||||
| 54 | sub add | ||||||||||||||||||||||||||||
| 55 | 0 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
add: not called CC=3 SCAR=24.8
Pod
undocumented
| |||||||||||||||||||||||||||||
| 56 | 0 | my $name = shift; | |||||||||||||||||||||||||||
| 57 | 0 | my $coderef = shift; | |||||||||||||||||||||||||||
| 58 | 0 | ▶ | if (ref $coderef ne "CODE") { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 59 | 0 | confess "PiFlash::Hook::add_hook(): can't add $name hook with non-code reference"; | |||||||||||||||||||||||||||
| 60 | } | ||||||||||||||||||||||||||||
| 61 | 0 | ▶ | if (!exists $hooks{$name}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 62 | 0 | $hooks{$name} = []; | |||||||||||||||||||||||||||
| 63 | } | ||||||||||||||||||||||||||||
| 64 | 0 | push @{$hooks{$name}}, PiFlash::Hook::new({name => $name, code => $coderef, origin => [caller]}); | |||||||||||||||||||||||||||
| 65 | } | ||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||
| 67 | # check if there are any hooks registered for a name | ||||||||||||||||||||||||||||
| 68 | sub has | ||||||||||||||||||||||||||||
| 69 | 0 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
has: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 70 | 0 | my $name = shift; | |||||||||||||||||||||||||||
| 71 | 0 | return exists $hooks{$name}; | |||||||||||||||||||||||||||
| 72 | } | ||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||
| 74 | # run the hook code | ||||||||||||||||||||||||||||
| 75 | sub run | ||||||||||||||||||||||||||||
| 76 | 0 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
run: not called CC=5 SCAR=34.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 77 | 0 | my $name = shift; | |||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||
| 79 | # Is there a hook of that name? | ||||||||||||||||||||||||||||
| 80 | 0 | ▶ | if (!exists $hooks{$name}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 81 | 0 | ▶ | if (PiFlash::State::verbose()) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 82 | 0 | say "PiFlash::Hook dispatch: no such hook $name - ignored"; | |||||||||||||||||||||||||||
| 83 | } | ||||||||||||||||||||||||||||
| 84 | 0 | return; | |||||||||||||||||||||||||||
| 85 | } | ||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||
| 87 | # call all functions registered in the list for this hook | ||||||||||||||||||||||||||||
| 88 | 0 | my @result; | |||||||||||||||||||||||||||
| 89 | 0 | ▶ | if (ref $hooks{$name} eq "ARRAY") { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 90 | 0 | foreach my $hook (@{$hooks{$name}}) { | |||||||||||||||||||||||||||
| 91 | 0 | push @result, $hook->{code}(@_); | |||||||||||||||||||||||||||
| 92 | } | ||||||||||||||||||||||||||||
| 93 | } | ||||||||||||||||||||||||||||
| 94 | 0 | return @result; | |||||||||||||||||||||||||||
| 95 | } | ||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||
| 97 | 1 | 1; | |||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||
| 99 | __END__ | ||||||||||||||||||||||||||||