ctflags/memory.pm

filter: Statementstmt 48.8% 41 / 84 Branchbran 25.0% 4 / 16 Conditioncond 60.0% 3 / 5 MC/DCmcdc 33.3% 1 / 3 Subroutinesub 53.3% 8 / 15 Podpod 0.0% 0 / 10 totaltotal 42.8% 57 / 133 CC 12 SCAR 15.1
CC
12
Cov
46%
extend_flagsetext
12.0
allowed_ctflags
6.0
set_ctflag_alias
6.0
CRAP 4.5
« ctflags/check.pm ↑ Summary ctflags.pm »
1 package ctflags::memory;
2  
3 1 our $VERSION = '0.01';
4  
5 1 ▶use 5.006;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 1 require Exporter;
10  
11 1 our @ISA = qw(Exporter);
12  
13 # this package is supposed to be private to ctflags and companion
14 # packages, not used from any other module so it uses directly
15 # @EXPORT. Anyway, the function names used are not going to cause
16 # too many namespace pollution problems.
17 1 our @EXPORT = qw( set_ctflag
18 get_ctflag
19 restrict_ctflags
20 allowed_ctflags
21 is_ctflag_allowed
22 set_ctflag_alias
23 resolve_ctflag_alias
24 set_ctflag_call
25 get_ctflag_call );
26  
27  
28 1 ▶use ctflags::check;
Subroutine BEGIN: called CC=1 SCAR=0.0
29  
30 1 my %memory;
31 my %meta;
32 1 my %alias;
33 1 my %call;
34  
35 # In most of ctflags subrutines, argument checking is done indirectly
36 # when subrutines here, in ctflags::memory are called, and so all of
37 # the public subrutines here implement checks for the validity of its
38 # arguments.
39 #
40 # Only helper functions which name begins with an underscore, like
41 # '_is_allowed' bellow, are exent from these checks and their use is
42 # discouraged outside this module
43  
44  
45 # checks that the use of a flag has not been forbided inside a
46 # namespace or dies.
47  
48 21 ▶sub _is_allowed ($$) {
Subroutine _is_allowed: called CC=3 SCAR=13.1
49 21 my ($ns, $flag)=@_;
50 !exists $meta{$ns.':restricted'}
51 21 ▶ or index($meta{$ns.':restricted'}, $flag)>=0
Branch: 1 branch
BranchTrueFalseTotal
not exists $meta{$ns . ':restricted'} or index($meta{$ns . ':restricted'}, $flag) != -1 or die "ctflag '${flag}' is not allowed in namespace '${ns}'\n" 21 0 21
Condition33%1/3
not exists $meta{$ns . ':restricted'} or index($meta{$ns . ':restricted'}, $flag) != -1
l!l&&r!l&&!r
2100
MC/DC0%0/2
not exists $meta{$ns . ':restricted'} or index($meta{$ns . ':restricted'}, $flag) != -1
not exists $meta{$ns . ':restricted'} index($meta{$ns . ':restricted'}, $flag) != -1
Truth table33%1/3
not(A) or B
A= not exists $meta{$ns . ':restricted'}B= index($meta{$ns . ':restricted'}, $flag) != -1
ABresult
1X1
011
000
52 or die "ctflag '$flag' is not allowed in namespace '$ns'\n";
53 }
54  
55  
56 # public interface for _is_allowed. Just checks for the validity of
57 # its arguments
58  
59 0 ▶sub is_ctflag_allowed ($$) {
Subroutine is_ctflag_allowed: not called CC=1 SCAR=6.9
Pod undocumented
60 0 check_ns $_[0];
61 0 check_flag $_[1];
62 0 &_is_allowed
63 }
64  
65  
66 # change value of ctflag
67  
68 4 ▶sub set_ctflag ($$$ ) {
Subroutine set_ctflag: called CC=1 SCAR=0.0
Pod undocumented
69 4 my ($ns, $flag, $value)=@_;
70 4 check_ns $ns;
71 4 check_flag $flag;
72 4 check_value $value;
73 4 _is_allowed $ns, $flag;
74 4 $memory{$ns.':'.$flag}=int($value);
75 }
76  
77  
78 # retrieve value of ctflag
79  
80 17 ▶sub get_ctflag ($$) {
Subroutine get_ctflag: called CC=3 SCAR=11.0
Pod undocumented
81 17 my ($ns, $flagext)=@_;
82 17 check_ns $ns;
83 17 my ($flag, $default)=$flagext=~/($flag_re)($value_re)?/o;
84 17 check_flag $flag;
85 17 check_value $default;
86 17 _is_allowed $ns, $flag;
87 17 my $m=$memory{$ns.":".$flag};
88 17 ▶ int(defined $m ? $m : ($default || 0));
Branch: 1 branch
BranchTrueFalseTotal
defined $m ? : 11 6 17
Condition100%2/2
$default or 0
l!l
24
MC/DC100%1/1
$default or 0
$default
Truth table100%2/2
A
A= $default
Aresult
11
00
89 }
90  
91 0 ▶sub extend_flagsetext ($$) {
Subroutine extend_flagsetext: not called CC=3 SCAR=24.8
Pod undocumented
92 0 my ($fse, $ns)=@_;
93 0 check_ns($ns);
94 0 check_flagsetext($fse);
95  
96 0 ▶ if ($fse eq '*') {
Branch: 1 branch
BranchTrueFalseTotal
if ($fse eq "*") 0 0 0
97 0 return join ('', allowed_ctflags($ns))
98 }
99  
100 0 ▶ if ($fse=~/^!(.*)/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($fse =~ /^!(.*)/) 0 0 0
101 0 my $inv=$1;
102 return join('',
103 0 (grep {index($inv, $_)<0 } allowed_ctflags($ns)));
104 }
105  
106 0 return $fse;
107 }
108  
109 # restrict which ctflags are allowed inside a namepace
110  
111  
112 0 ▶sub restrict_ctflags ($$) {
Subroutine restrict_ctflags: not called CC=1 SCAR=6.9
Pod undocumented
113 0 my $ns=shift;
114 0 check_ns $ns;
115 0 my $flagset=extend_flagsetext(shift, $ns);
116 0 $meta{$ns.':restricted'}=$flagset;
117 }
118  
119  
120 # returns an array with the allowed ctflags inside a namespace
121  
122 0 ▶sub allowed_ctflags ($ ) {
Subroutine allowed_ctflags: not called CC=2 SCAR=17.9
Pod undocumented
123 0 my $ns=shift;
124 0 check_ns $ns;
125 return split('', $meta{$ns.':restricted'})
126 0 ▶ if (exists $meta{$ns.':restricted'});
Branch: 1 branch
BranchTrueFalseTotal
if exists $meta{$ns . ':restricted'} 0 0 0
127 0 return ('a'..'z','A'..'Z')
128 }
129  
130  
131 # creates an alias (long name composed of more than one letter) for a
132 # ctflag
133  
134 0 ▶sub set_ctflag_alias ($$$ ) {
Subroutine set_ctflag_alias: not called CC=2 SCAR=17.9
Pod undocumented
135 0 my ($ns, $alias, $flag)=@_;
136 0 check_alias $alias;
137 0 check_ns $ns;
138 0 ▶ unless (defined $flag) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $flag) 0 0 0
139 0 delete $alias{$ns.':'.$alias};
140 return
141 0 }
142 0 check_flag $flag;
143 0 _is_allowed $ns, $flag;
144 0 $alias{$ns.':'.$alias}=$flag;
145 }
146  
147  
148 # returns the value of an aliased ctflag from its alias name
149  
150 0 ▶sub resolve_ctflag_alias ($$) {
Subroutine resolve_ctflag_alias: not called CC=2 SCAR=17.9
Pod undocumented
151 0 my ($ns, $alias)=@_;
152 0 check_ns $ns;
153 0 check_alias $alias;
154 0 ▶ exists $alias{$ns.':'.$alias}
Branch: 1 branch
BranchTrueFalseTotal
exists $alias{$ns . ':' . $alias} or die "ctflag alias '${alias}' not defined in namespace '${ns}'\n" 0 0 0
155 or die "ctflag alias '$alias' not defined in namespace '$ns'\n";
156 0 return get_ctflag($ns, $alias{$ns.':'.$alias});
157 }
158  
159 0 ▶sub set_ctflag_call ($$$) {
Subroutine set_ctflag_call: not called CC=2 SCAR=17.9
Pod undocumented
160 0 my ($ns, $flagsetext, $sub)=@_;
161 0 check_ns $ns;
162 0 my $flags=extend_flagsetext($flagsetext, $ns);
163 0 check_sub $sub;
164 0 foreach my $f (split //, $flags) {
165 0 _is_allowed $ns, $f;
166 0 $call{$ns.':'.$f}=$sub;
167 }
168 }
169  
170 6 ▶sub get_ctflag_call ($$) {
Subroutine get_ctflag_call: called CC=2 SCAR=7.1
Pod undocumented
171 6 my ($ns, $flag) =@_;
172 6 check_ns($ns);
173 6 check_flag($flag);
174 6 my $n=$ns.':'.$flag;
175 6 ▶ if (exists $call{$n}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $call{$n}) 0 6 6
176 0 return $call{$n};
177 }
178 6 return undef;
179 }
180  
181 1 1;
182 __END__
« ctflags/check.pm ↑ Summary ctflags.pm »