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   1015 use v5.12.5;
  73         256  
8 73     73   391 use warnings;
  73         167  
  73         3982  
9              
10             our $VERSION = '1.14.2.2'; # TRIAL 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   463 use base qw(Exporter);
  73         148  
  73         6301  
30 73     73   481 use vars qw(@EXPORT);
  73         192  
  73         22103  
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 1300 my ($hooks) = @_;
52              
53 1         3 for my $state ( keys %{$hooks} ) {
  1         7  
54 4         5 for my $func ( keys %{ $hooks->{$state} } ) {
  4         10  
55 4 50       13 if ( !exists $__hooks->{$state}->{$func} ) {
56 4         7 $__hooks->{$state}->{$func} = [];
57             }
58              
59 4         6 push @{ $__hooks->{$state}->{$func} }, $hooks->{$state}->{$func};
  4         11  
60             }
61             }
62             }
63              
64             sub run_hook {
65 301     301 0 5211 my ( $command, $state, @args ) = @_;
66              
67 301 100       2591 if ( !exists $__hooks->{$state}->{$command} ) {
68 289         1670 return;
69             }
70              
71 12         48 my $func_arr = $__hooks->{$state}->{$command};
72              
73 12         23 for my $func ( @{$func_arr} ) {
  12         80  
74 12         155 @args = $func->(@args);
75             }
76              
77 12         42544 return @args;
78             }
79              
80             1;