Filter.pm

filter: Statementstmt 100% 69 / 69 Branchbran 96.6% 29 / 30 Conditioncond 100% 3 / 3 MC/DCmcdc 100% 2 / 2 Subroutinesub 100% 14 / 14 Podpod n/a totaltotal 99.1% 117 / 118 CC 18 SCAR 22.9
CC
18
Cov
99%
import
17.0
_queue_sub
2.0
BEGIN
1.0
CRAP 9.8
↑ Summary Filter/RulePool.pm »
1 #################################################################
2 #
3 # Hook::Filter - A runtime filtering layer on top of subroutine calls
4 #
5 # $Id: Filter.pm,v 1.12 2008/08/26 08:13:30 erwan_lemonnier Exp $
6 #
7 # 051105 erwan Created
8 # 060301 erwan Recreated
9 # 070516 erwan Updated POD and license, added flush_rules and add_rule
10 # 070522 erwan More POD + don't use rule file unless 'rules' specified in import
11 # 070523 erwan Can use 'rules' multiple time if same rule file specified
12 # 070523 erwan POD updates
13 # 070524 erwan Import parameter 'hook' is now mandatory
14 #
15  
16 package Hook::Filter;
17  
18 7 ▶use 5.006;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 7 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 7 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 7 ▶use Carp qw(confess croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
22 7 ▶use File::Spec;
Subroutine BEGIN: called CC=1 SCAR=0.0
23 7 ▶use Hook::Filter::Rule;
Subroutine BEGIN: called CC=1 SCAR=0.0
24 7 ▶use Hook::Filter::RulePool qw(get_rule_pool);
Subroutine BEGIN: called CC=1 SCAR=0.0
25 7 ▶use Hook::Filter::Hooker qw(filter_sub);
Subroutine BEGIN: called CC=1 SCAR=0.0
26 7 ▶use base qw(Exporter);
Subroutine BEGIN: called CC=1 SCAR=0.0
27 7 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
28  
29 7 our @EXPORT = qw();
30  
31 7 our $VERSION = '0.10';
32  
33 #----------------------------------------------------------------
34 #
35 # Global vars
36 #
37 #----------------------------------------------------------------
38  
39 # the rule file actually used by Hook::Filter, and as declared with parameter 'rules'
40 7 my $RULES_FILE;
41  
42 # list of subs to hijack
43 my %HOOK_SUBS;
44  
45 19 ▶sub _queue_sub {
Subroutine _queue_sub: called CC=2 SCAR=6.9
46 19 my ($pkg,$name) = @_;
47 19 ▶ ($name =~ /::/) ? $HOOK_SUBS{$name}=1 : $HOOK_SUBS{$pkg."::".$name}=1;
Branch: 1 branch
BranchTrueFalseTotal
$name =~ /::/ ? : 6 13 19
48 }
49  
50 #----------------------------------------------------------------
51 #
52 # import - verify import parameters, filter the subs and load the rule file
53 #
54  
55 17 ▶sub import {
Subroutine import: called CC=17 SCAR=28.3
56 17 my($class,%args) = @_;
57 17 my $pkg = caller(0);
58  
59 #
60 # check parameter 'rules', indicating path to the rule file
61 #
62  
63 17 ▶ if (exists $args{rules}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $args{"rules"}) 7 10 17
64  
65 croak "import parameter 'rules' for Hook::Filter should be a string, but was undef."
66 7 ▶ if (!defined $args{rules});
Branch: 1 branch
BranchTrueFalseTotal
unless defined $args{"rules"} 1 6 7
67  
68 croak "import parameter 'rules' for Hook::Filter should be a string, but was [".Dumper($args{rules})."]."
69 6 ▶ if (ref \$args{rules} ne 'SCALAR');
Branch: 1 branch
BranchTrueFalseTotal
if ref \$args{"rules"} ne "SCALAR" 1 5 6
70  
71 croak "you tried to specify 2 different Hook::Filter rule file: [$RULES_FILE] and [".$args{rules}."]. you may have only 1 rule file."
72 5 ▶ if (defined $RULES_FILE && $RULES_FILE ne $args{rules});
Branch: 1 branch
BranchTrueFalseTotal
if defined $RULES_FILE and $RULES_FILE ne $args{"rules"} 1 4 5
Condition100%3/3
defined $RULES_FILE and $RULES_FILE ne $args{"rules"}
!ll&&!rl&&r
221
MC/DC100%2/2
defined $RULES_FILE and $RULES_FILE ne $args{"rules"}
defined $RULES_FILE $RULES_FILE ne $args{"rules"}
Truth table100%3/3
A and B
A= defined $RULES_FILEB= $RULES_FILE ne $args{"rules"}
ABresult
0X0
100
111
73  
74 4 $RULES_FILE = $args{rules};
75 4 delete $args{rules};
76  
77 #
78 # load the rule file, if any
79 #
80  
81 4 my $pool = get_rule_pool();
82  
83 4 ▶ if (-f $RULES_FILE) {
Branch: 1 branch
BranchTrueFalseTotal
if (-f $RULES_FILE) 1 3 4
84 # TODO: support runtime monitoring of rules file and update of rules upon changes in file
85  
86 1 ▶ open(IN,"$RULES_FILE")
Branch: 1 branch
BranchTrueFalseTotal
open IN, "$RULES_FILE" or confess("failed to open Hook::Filter rules file [$RULES_FILE]: $!") 1 0 1
87 or confess "failed to open Hook::Filter rules file [$RULES_FILE]: $!";
88 1 ▶ while (my $line = <IN>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $line = readline IN)) 9 1 10
89 9 chomp $line;
90 9 ▶ next if ($line =~ /^\s*\#/);
Branch: 1 branch
BranchTrueFalseTotal
if $line =~ /^\s*\#/ 3 6 9
91 6 ▶ next if ($line =~ /^\s*$/);
Branch: 1 branch
BranchTrueFalseTotal
if $line =~ /^\s*$/ 1 5 6
92  
93 5 my $rule = new Hook::Filter::Rule($line);
94 5 $rule->source($RULES_FILE);
95 5 $pool->add_rule($rule);
96 }
97 1 close(IN);
98 }
99 }
100  
101 #
102 # check parameter 'hook', indicating which subroutines to filter
103 #
104  
105 croak "you must call Hook::Filter with the import parameter 'hook' set to something"
106 14 ▶ if (!exists $args{hook});
Branch: 1 branch
BranchTrueFalseTotal
unless exists $args{"hook"} 2 12 14
107  
108 croak "Invalid parameter: 'hook' should be a string or an array of strings, but was undef."
109 12 ▶ if (!defined $args{hook});
Branch: 1 branch
BranchTrueFalseTotal
unless defined $args{"hook"} 1 11 12
110  
111 11 ▶ if (ref $args{hook} eq 'ARRAY') {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $args{'hook'} eq 'ARRAY') { } 5 6 11
elsif (ref \$args{'hook'} eq 'SCALAR') { } 5 1 6
112 5 foreach my $name (@{$args{hook}}) {
113 15 ▶ if (ref \$name ne 'SCALAR') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref \$name ne "SCALAR") 1 14 15
114 1 croak "Invalid parameter: 'hook' for Hook::Filter should be a string or an array of strings, but was [".Dumper($args{hook})."].";
115 }
116 14 _queue_sub($pkg,$name);
117 }
118 } elsif (ref \$args{hook} eq 'SCALAR') {
119 5 _queue_sub($pkg,$args{hook});
120 } else {
121 1 croak "Invalid parameter: 'hook' for Hook::Filter should be a string or an array of strings, but was [".Dumper($args{hook})."].";
122 }
123  
124 9 delete $args{hook};
125  
126 # propagate super class's import
127 9 $class->export_to_level(1,undef,());
128 }
129  
130 #
131 # when all is compiled, do filter all the subs
132 #
133  
134 6 ▶sub _filter_subs {
Subroutine _filter_subs: called CC=1 SCAR=0.0
135 6 map { filter_sub($_) } keys %HOOK_SUBS;
136 }
137  
138 # this init block won't be executed if Hook::Filter is used from an eval/require
139 5 ▶INIT {
Subroutine INIT: called CC=1 SCAR=0.0
140 # add a filtering closure around each sub
141 5 _filter_subs;
142 }
143  
144 7 1;
145  
146 __END__
↑ Summary Filter/RulePool.pm »