lib/Rex/Hook.pm

filter: Statementstmt 100% 33 / 33 Branchbran 75.0% 3 / 4 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 6 / 6 Podpod 50.0% 1 / 2 totaltotal 95.5% 43 / 45 CC 6 SCAR 9.7
CC
6
Cov
97%
register_function_hooks
4.0
run_hook
3.0
BEGIN
1.0
CRAP 2.6
« lib/Rex/Inventory/DMIDecode/Memory.pm ↑ Summary lib/Rex/Interface/Connection/Base.pm »
1 #
2 # (c) Jan Gehring <jan.gehring@gmail.com>
3 #
4  
5 package Rex::Hook;
6  
7 77 ▶use v5.14.4;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 77 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 77 our $VERSION = '1.16.1'; # VERSION
11  
12 =head1 NAME
13  
14 Rex::Hook - manage Rex hooks
15  
16 =head1 DESCRIPTION
17  
18 This module manages hooks of various Rex functions.
19  
20 =head1 SYNOPSIS
21  
22 use Rex::Hook;
23  
24 register_function_hooks { $state => { $function => $coderef, }, };
25  
26 =cut
27  
28 77 require Exporter;
29 77 ▶use base qw(Exporter);
Subroutine BEGIN: called CC=1 SCAR=0.0
30 77 ▶use vars qw(@EXPORT);
Subroutine BEGIN: called CC=1 SCAR=0.0
31  
32 77 @EXPORT = qw(register_function_hooks);
33  
34 77 my $__hooks = {};
35  
36 =head1 EXPORTED FUNCTIONS
37  
38 =head2 register_function_hooks { $state => { $function => $coderef } };
39  
40 Registers a C<$coderef> to be called when C<$function> reaches C<$state> during its execution.
41  
42 For example:
43  
44 register_function_hooks { before_change => { file => \&backup } };
45  
46 C<$coderef> may get parameters passed to it depending on the hook in question. See the given hook's documentation about details.
47  
48 =cut
49  
50 1 ▶sub register_function_hooks {
Subroutine register_function_hooks: called CC=4 SCAR=13.9
Pod documented
51 1 my ($hooks) = @_;
52  
53 1 for my $state ( keys %{$hooks} ) {
54 4 for my $func ( keys %{ $hooks->{$state} } ) {
55 4 ▶ if ( !exists $__hooks->{$state}->{$func} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $__hooks->{$state}{$func}) 4 0 4
56 4 $__hooks->{$state}->{$func} = [];
57 }
58  
59 4 push @{ $__hooks->{$state}->{$func} }, $hooks->{$state}->{$func};
60 }
61 }
62 }
63  
64 325 ▶sub run_hook {
Subroutine run_hook: called CC=3 SCAR=11.0
Pod undocumented
65 325 my ( $command, $state, @args ) = @_;
66  
67 325 ▶ if ( !exists $__hooks->{$state}->{$command} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $__hooks->{$state}{$command}) 313 12 325
68 313 return;
69 }
70  
71 12 my $func_arr = $__hooks->{$state}->{$command};
72  
73 12 for my $func ( @{$func_arr} ) {
74 12 @args = $func->(@args);
75 }
76  
77 12 return @args;
78 }
79  
80 77 1;
« lib/Rex/Inventory/DMIDecode/Memory.pm ↑ Summary lib/Rex/Interface/Connection/Base.pm »