File Coverage

lib/Rex/Hook.pm
Criterion Covered Total %
statement 28 28 100.0
branch 3 4 75.0
condition n/a
subroutine 6 6 100.0
pod 1 2 50.0
total 38 40 95.0


line stmt bran cond sub pod time code
1             #
2             # (c) Jan Gehring
3             #
4              
5             package Rex::Hook;
6              
7 73     73   984 use v5.12.5;
  73         245  
8 73     73   355 use warnings;
  73         156  
  73         3942  
9              
10             our $VERSION = '1.14.3'; # 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             require Exporter;
29 73     73   447 use base qw(Exporter);
  73         148  
  73         6311  
30 73     73   465 use vars qw(@EXPORT);
  73         160  
  73         21995  
31              
32             @EXPORT = qw(register_function_hooks);
33              
34             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             sub register_function_hooks {
51 1     1 1 1041 my ($hooks) = @_;
52              
53 1         2 for my $state ( keys %{$hooks} ) {
  1         6  
54 4         6 for my $func ( keys %{ $hooks->{$state} } ) {
  4         10  
55 4 50       8 if ( !exists $__hooks->{$state}->{$func} ) {
56 4         8 $__hooks->{$state}->{$func} = [];
57             }
58              
59 4         5 push @{ $__hooks->{$state}->{$func} }, $hooks->{$state}->{$func};
  4         9  
60             }
61             }
62             }
63              
64             sub run_hook {
65 301     301 0 4547 my ( $command, $state, @args ) = @_;
66              
67 301 100       2214 if ( !exists $__hooks->{$state}->{$command} ) {
68 289         1260 return;
69             }
70              
71 12         70 my $func_arr = $__hooks->{$state}->{$command};
72              
73 12         21 for my $func ( @{$func_arr} ) {
  12         67  
74 12         144 @args = $func->(@args);
75             }
76              
77 12         33756 return @args;
78             }
79              
80             1;