blib/lib/Hook/Queue.pm

filter: Statementstmt 97.5% 40 / 41 Branchbran 75.0% 3 / 4 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 9 / 9 Podpod 0.0% 0 / 1 totaltotal 94.5% 52 / 55 CC 5 SCAR 6.6
CC
5
Cov
96%
__ANON__
3.0
import
3.0
BEGIN
1.0
CRAP 1.9
↑ Summary
1 package Hook::Queue;
2 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use Devel::Peek qw(CvGV);
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 our $VERSION = 1.21;
6  
7 =head1 NAME
8  
9 Hook::Queue - define a queue of handlers
10  
11 =head1 SYNOPSIS
12  
13 # define a Liar class which always claims to be what you're asking
14 # about
15 package Liar;
16 use Hook::Queue 'UNIVERSAL::isa' => sub {
17 my $what = shift;
18 my $class = shift;
19 return 1 if (ref $what || $what) eq "Liar";
20 # it's not my call, pass it down the chain
21 return Hook::Queue->defer;
22 };
23  
24 =head1 DESCRIPTION
25  
26 Hook::Queue provides a mechanism for stacking global handlers in a
27 queue of routines that will take an attempt at answering the
28 subroutine call addressed to it.
29  
30 For each subroutine that joins the queue, it can either return a
31 canonical answer, or indicate that it's deferring along the queue by
32 calling the C<Hook::Queue->defer> method and returning.
33  
34 When you say C<use Hook::Queue> you join the queue at its current
35 head, and as such your position may very, depending on compilation
36 order of the Perl program. As such you should remember to C<defer>
37 even if your testing shows you to be at the end of the queue in test
38 circumstances.
39  
40 =cut
41  
42 1 my ($Defer, %Hooks);
43 1 ▶sub defer { $Defer = 1 }
Subroutine defer: called CC=1 SCAR=0.0
Pod undocumented
44  
45 1 ▶sub import {
Subroutine import: called CC=3 SCAR=11.0
46 1 my $class = shift;
47 1 my %hooks = @_;
48 1 for my $hook (keys %hooks) {
49 1 ▶ my $hooked = do { no strict 'refs'; \&$hook };
Subroutine BEGIN: called CC=1 SCAR=0.0
50 1 ▶ if (CvGV($hooked) ne $hook) {
Branch: 1 branch
BranchTrueFalseTotal
if (CvGV($hooked) ne $hook) 1 0 1
51 # something already lives there, save at the head of the
52 # queue and install
53 1 unshift @{ $Hooks{$hook} }, $hooked;
54 2 ▶ my $sub = sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
55 2 for my $segment (@{ $Hooks{ $hook } }) {
56 3 $Defer = 0;
57 3 my $ret = $segment->( @_ );
58 3 ▶ next if $Defer;
Branch: 1 branch
BranchTrueFalseTotal
if $Defer 1 2 3
59 2 return $ret;
60 }
61 0 die "Deferred past the end of the queue of $hook!";
62 1 };
63  
64 1 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
65 1 ▶ no warnings 'redefine';
Subroutine BEGIN: called CC=1 SCAR=0.0
66 1 *$hook = $sub;
67 }
68 1 unshift @{ $Hooks{$hook} }, $hooks{ $hook };
69 }
70 }
71  
72  
73 1 1;
74  
75 __END__
↑ Summary