blib/lib/Scalar/Properties.pm

filter: Statementstmt 92.3% 169 / 183 Branchbran 63.1% 48 / 76 Conditioncond 78.5% 11 / 14 MC/DCmcdc 66.6% 6 / 9 Subroutinesub 91.9% 57 / 62 Podpod 100% 54 / 54 totaltotal 86.6% 345 / 398 CC 51 SCAR 10.4
CC
51
Cov
84%
AUTOLOAD
8.1
import
6.6
handle
5.0
CRAP 2.8
↑ Summary
1 2 ▶use 5.008;
Subroutine BEGIN: called CC=1 SCAR=0.0
2 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 package Scalar::Properties;
6 2 our $VERSION = '1.100860';
7 # ABSTRACT: Run-time properties on scalar variables
8 use overload
9 2 ▶ q{""} => \&value,
Subroutine BEGIN: called CC=1 SCAR=0.0
10 bool => \&is_true,
11 '+' => \&plus,
12 '-' => \&minus,
13 '*' => \&times,
14 '/' => \&divide,
15 '%' => \&modulo,
16 '**' => \&exp,
17 '<=>' => \&numcmp,
18 'cmp' => \&cmp,
19  
20 # the following would be autogenerated from 'cmp', but
21 # we want to make the methods available explicitly, along
22 # with case-insensitive versions
23 'eq' => \&eq,
24 'ne' => \&ne,
25 'lt' => \&lt,
26 'gt' => \&gt,
27 'le' => \&le,
28 2 'ge' => \&ge;
29  
30 2 ▶sub import {
Subroutine import: called CC=6 SCAR=18.8
31 2 my $pkg = shift;
32 2 my @defs = qw/integer float binary q qr/;
33 2 my @req;
34 2 ▶ @_ = ':all' unless @_;
Branch: 1 branch
BranchTrueFalseTotal
unless @_ 2 0 2
35 2 for my $key (@_) {
36 2 ▶ if ($key eq ':all') {
Branch: 1 branch
BranchTrueFalseTotal
if ($key eq ':all') { } 2 0 2
37 2 @req = @defs;
38 } else {
39 0 ▶ die __PACKAGE__ . " does not export '$key'"
Branch: 1 branch
BranchTrueFalseTotal
unless grep /^$key$/, @defs 0 0 0
40 unless grep /^$key$/ => @defs;
41 0 push @req => $key;
42 }
43 }
44 2 overload::constant map { $_ => \&handle } @req;
45  
46 # also manually export some routines
47 2 my $callpkg = caller(1);
48 2 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
49 2 *{"$callpkg\::$_"} = \&{"$pkg\::$_"} for qw/pass_on passed_on get_pass_on/;
50 }
51  
52 # object's hash keys that aren't properties (apart from those starting with
53 # and underscore, which are private anyway)
54 2 our %NON_PROPS = map { $_ => 1 } our @NON_PROPS = qw/true/;
55  
56 # property propagation
57 1 ▶sub pass_on {
Subroutine pass_on: called CC=1 SCAR=0.0
Pod documented
58 1 our %PASS_ON = map { $_ => 1 } our @PASS_ON = @_;
59 }
60 9 ▶sub passed_on { our %PASS_ON; exists $PASS_ON{ +shift } }
Subroutine passed_on: called CC=1 SCAR=0.0
Pod documented
61 1 ▶sub get_pass_on { our @PASS_ON }
Subroutine get_pass_on: called CC=1 SCAR=0.0
Pod documented
62  
63 466 ▶sub get_props {
Subroutine get_props: called CC=2 SCAR=6.9
Pod documented
64  
65 # get a list of the value's properties
66 466 my $self = shift;
67 466 our %NON_PROPS;
68 466 ▶ return grep { !(/^_/ || exists $NON_PROPS{$_}) } keys %$self;
Condition100%3/3
/^_/ || exists $Scalar::Properties::NON_PROPS{$_}
l!l&&r!l&&!r
139846618
MC/DC100%2/2
/^_/ || exists $Scalar::Properties::NON_PROPS{$_}
/^_/ exists $Scalar::Properties::NON_PROPS{$_}
Truth table100%3/3
A || B
A= /^_/B= exists $Scalar::Properties::NON_PROPS{$_}
ABresult
1X1
011
000
69 }
70  
71 1 ▶sub del_prop {
Subroutine del_prop: called CC=4 SCAR=14.2
Pod documented
72  
73 # delete one or more properties
74 1 my $self = shift;
75 1 our %NON_PROPS;
76 1 for my $prop (@_) {
77 1 ▶ die "$prop is private, not a property"
Branch: 1 branch
BranchTrueFalseTotal
if substr($prop, 0, 1) eq "_" 0 1 1
78 if substr($prop, 0, 1) eq '_';
79 1 ▶ die "$prop cannot be deleted" if exists $NON_PROPS{$prop};
Branch: 1 branch
BranchTrueFalseTotal
if exists $Scalar::Properties::NON_PROPS{$prop} 0 1 1
80 1 delete $self->{$prop};
81 }
82 }
83  
84 1 ▶sub del_all_props {
Subroutine del_all_props: called CC=2 SCAR=6.9
Pod documented
85 1 my $self = shift;
86 1 my @props = $self->get_props;
87 1 delete $self->{$_} for @props;
88 }
89  
90 564 ▶sub handle {
Subroutine handle: called CC=5 SCAR=16.1
Pod documented
91  
92 # create a new overloaded object
93 564 my ($orig, $interp, $context, $sub, @prop) = @_;
94 564 ▶ my $self = bless {
Branch: 1 branch
BranchTrueFalseTotal
$orig ? : 480 84 564
95 _value => $orig,
96 _interp => $interp,
97 _context => $context,
98 true => ($orig) ? 1 : 0,
99 },
100 __PACKAGE__;
101  
102 # propagate properties marked as such via pass_on from
103 # participating overloaded values passed in @prop
104 564 for my $val (grep { ref $_ eq __PACKAGE__ } @prop) {
105 461 for my $prop ($val->get_props) {
106 8 ▶ $self->{$prop} = $val->{$prop} if passed_on($prop);
Branch: 1 branch
BranchTrueFalseTotal
if passed_on($prop) 4 4 8
107 }
108 }
109 564 return $self;
110 }
111  
112 355 ▶sub create {
Subroutine create: called CC=1 SCAR=0.0
Pod documented
113  
114 # take a value and a list of participating values and create
115 # a new object from them by filling in the gaps that handle()
116 # expects with defaults. As seen from handle(), the participating
117 # values (i.e., the values that the first arg was derived from)
118 # are passed so that properties can be properly propagated
119 355 my ($val, @props) = @_;
120 355 ▶ handle($val, $val, '', sub { }, @props);
Subroutine __ANON__: not called CC=1 SCAR=0.0
121 }
122  
123 # call this as a sub, not a method as it also takes unblessed scalars
124 # anything not of this package is stringified to give any potential
125 # other overloading a chance to get at it's actual value
126 1561 ▶sub value {
Subroutine value: called CC=2 SCAR=6.9
Pod documented
127  
128 # my $v = ref $_[0] eq __PACKAGE__ ? $_[0]->{_value} : "$_[0]";
129 # $v =~ s/\\n/\n/gs; # no idea why newlines become literal '\n'
130 1561 ▶ my $v = ref $_[0] eq __PACKAGE__ ? $_[0]->{_interp} : "$_[0]";
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] eq __PACKAGE__ ? : 1354 207 1561
131 1561 return $v;
132 }
133  
134 # ==================== Generated methods ====================
135 # Generate some string, numeric and boolean methods
136 6 ▶sub gen_meth {
Subroutine gen_meth: called CC=3 SCAR=11.7
Pod documented
137 6 my $template = shift;
138 6 ▶ while (my ($name, $op) = splice(@_, 0, 2)) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = splice(@_, 0, 2)) 62 6 68
139 62 (my $code = $template) =~ s/NAME/$name/g;
140 62 $code =~ s/OP/$op/g;
141 0 ▶ eval $code;
Subroutine abs: called CC=1 SCAR=0.3
Subroutine append: called CC=2 SCAR=7.5
Subroutine cmp: not called CC=2 SCAR=7.5
Subroutine concat: called CC=2 SCAR=7.5
Subroutine divide: not called CC=2 SCAR=7.5
Subroutine eq: called CC=2 SCAR=7.5
Subroutine eqi: called CC=1 SCAR=0.3
Subroutine exp: not called CC=2 SCAR=7.5
Subroutine ge: called CC=2 SCAR=7.5
Subroutine gei: called CC=1 SCAR=0.3
Subroutine gt: called CC=2 SCAR=7.5
Subroutine gti: called CC=1 SCAR=0.3
Subroutine hex: called CC=1 SCAR=0.3
Subroutine lc: called CC=1 SCAR=0.3
Subroutine lcfirst: called CC=1 SCAR=0.3
Subroutine le: called CC=2 SCAR=7.5
Subroutine lei: called CC=1 SCAR=0.3
Subroutine length: called CC=1 SCAR=0.3
Subroutine lt: called CC=2 SCAR=7.5
Subroutine lti: called CC=1 SCAR=0.3
Subroutine minus: called CC=2 SCAR=7.5
Subroutine modulo: not called CC=2 SCAR=7.5
Subroutine ne: called CC=2 SCAR=7.5
Subroutine nei: called CC=1 SCAR=0.3
Subroutine numcmp: called CC=2 SCAR=7.5
Subroutine oct: called CC=1 SCAR=0.3
Subroutine plus: called CC=2 SCAR=7.5
Subroutine size: called CC=1 SCAR=0.3
Subroutine times: called CC=2 SCAR=7.5
Subroutine uc: called CC=1 SCAR=0.3
Subroutine ucfirst: called CC=1 SCAR=0.3
Pod documented
Pod documented
Pod documented
Pod documented
Pod documented
Pod documented
Pod documented
Pod documented
Pod documented
Pod documented
Pod documented
Pod documented
Pod documented
Pod documented
Pod documented
Pod documented
Pod documented
Pod documented
Pod documented
Pod documented
Pod documented
Pod documented
Pod documented
Pod documented
Pod documented
Pod documented
Pod documented
Pod documented
Pod documented
Pod documented
Pod documented
Branch: 16 branches
BranchTrueFalseTotal
if $_[2] 0 1 1
if $_[2] 0 0 0
if $_[2] 0 2 2
if $_[2] 0 0 0
if $_[2] 30 23 53
if $_[2] 0 0 0
if $_[2] 0 5 5
if $_[2] 0 3 3
if $_[2] 0 5 5
if $_[2] 0 3 3
if $_[2] 2 6 8
if $_[2] 0 0 0
if $_[2] 0 3 3
if $_[2] 13 42 55
if $_[2] 0 160 160
if $_[2] 0 2 2
142 62 ▶ die "Internal error: $@" if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 62 62
143 }
144 }
145 2 my $binop = 'sub NAME {
146 my($n, $m) = @_[0,1];
147 ($m, $n) = ($n, $m) if($_[2]);
148 create(value($n) OP value($m), $n, $m)
149 }';
150 2 gen_meth $binop, qw!
151 plus +
152 minus -
153 times *
154 divide /
155 modulo %
156 exp **
157 numcmp <=>
158 cmp cmp
159 eq eq
160 ne ne
161 lt lt
162 gt gt
163 le le
164 ge ge
165 concat .
166 append .
167 !;
168  
169 # needs 'CORE::lc', otherwise 'Ambiguous call resolved as CORE::lc()'
170 2 my $bool_i = 'sub NAME {
171 create( CORE::lc(value($_[0])) OP CORE::lc(value($_[1])), @_[0,1] )
172 }';
173 2 gen_meth $bool_i, qw!
174 eqi eq
175 nei ne
176 lti lt
177 gti gt
178 lei le
179 gei ge
180 !;
181 2 my $func = 'sub NAME {
182 create(OP(value($_[0])), $_[0])
183 }';
184 2 gen_meth $func, qw!
185 abs abs
186 length CORE::length
187 size CORE::length
188 uc uc
189 ucfirst ucfirst
190 lc lc
191 lcfirst lcfirst
192 hex hex
193 oct oct
194 !;
195  
196 # ==================== Miscellaneous Numeric methods ====================
197 4 ▶sub zero { create($_[0] == 0, $_[0]) }
Subroutine zero: called CC=1 SCAR=0.0
Pod documented
198  
199 # ==================== Miscellaneous Boolean methods ====================
200 116 ▶sub is_true { $_[0]->{true} }
Subroutine is_true: called CC=1 SCAR=0.0
Pod documented
201 2 ▶sub is_false { !$_[0]->{true} }
Subroutine is_false: called CC=1 SCAR=0.0
Pod documented
202  
203 7 ▶sub true {
Subroutine true: called CC=2 SCAR=6.9
Pod documented
204 7 my $self = shift;
205 7 ▶ $self->{true} = @_ ? shift : 1;
Branch: 1 branch
BranchTrueFalseTotal
@_ ? : 3 4 7
206 7 return $self;
207 }
208 3 ▶sub false { $_[0]->true(0) }
Subroutine false: called CC=1 SCAR=0.0
Pod documented
209  
210 # ==================== Miscellaneous String methods ====================
211 2 ▶sub reverse { create(scalar reverse(value($_[0])), $_[0]) }
Subroutine reverse: called CC=1 SCAR=0.0
Pod documented
212 1 ▶sub swapcase { my $s = shift; $s =~ y/A-Za-z/a-zA-Z/; return create($s) }
Subroutine swapcase: called CC=1 SCAR=0.0
Pod documented
213  
214 # $foo->split(/PATTERN/, LIMIT)
215 5 ▶sub split {
Subroutine split: called CC=4 SCAR=13.9
Pod documented
216 5 my ($orig, $pat, $limit) = @_;
217 5 ▶ $limit ||= 0;
Condition100%2/2
$limit ||= 0
l!l
14
MC/DC100%1/1
$limit ||= 0
$limit
Truth table100%2/2
A
A= $limit
Aresult
11
00
218 5 ▶ $pat = qr/\s+/ unless ref($pat) eq 'Regexp';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $pat eq "Regexp" 2 3 5
219  
220 # The following should work:
221 # map { create($_, $orig) } split $pat => value($orig), $limit;
222 # But there seems to be a bug in split
223 # (cf. p5p: 'Bug report: split splits on wrong pattern')
224 5 my @el;
225 5 eval '@el = split $pat => value($orig), $limit;';
226 5 ▶ die $@ if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 5 5
227 5 return map { create($_, $orig) } @el;
228 }
229  
230 # ==================== Code-execution methods ====================
231 2 ▶sub times_do {
Subroutine times_do: called CC=3 SCAR=11.1
Pod documented
232 2 my ($self, $sub) = @_;
233 2 ▶ die 'times_do() method expected a coderef' unless ref $sub eq 'CODE';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $sub eq "CODE" 0 2 2
234 2 for my $i (1 .. $self) {
235 8 $sub->($i);
236 }
237 }
238  
239 3 ▶sub do_upto_step {
Subroutine do_upto_step: called CC=3 SCAR=11.0
Pod documented
240 3 my ($self, $limit, $step, $sub) = @_;
241 3 ▶ die 'expected last arg to be a coderef'
Branch: 1 branch
BranchTrueFalseTotal
unless ref $sub eq "CODE" 0 3 3
242 unless ref $sub eq 'CODE';
243  
244 # for my $i ($self..$limit) { $sub->($i); }
245 3 my $i = $self;
246 3 ▶ while ($i <= $limit) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i <= $limit) 8 3 11
247 8 $sub->($i);
248 8 $i += $step;
249 }
250 }
251  
252 3 ▶sub do_downto_step {
Subroutine do_downto_step: called CC=3 SCAR=11.0
Pod documented
253 3 my ($self, $limit, $step, $sub) = @_;
254 3 ▶ die 'expected last arg to be a coderef'
Branch: 1 branch
BranchTrueFalseTotal
unless ref $sub eq "CODE" 0 3 3
255 unless ref $sub eq 'CODE';
256 3 my $i = $self;
257 3 ▶ while ($i >= $limit) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i >= $limit) 5 3 8
258 5 $sub->($i);
259 5 $i -= $step;
260 }
261 }
262 2 ▶sub do_upto { do_upto_step($_[0], $_[1], 1, $_[2]) }
Subroutine do_upto: called CC=1 SCAR=0.0
Pod documented
263 2 ▶sub do_downto { do_downto_step($_[0], $_[1], 1, $_[2]) }
Subroutine do_downto: called CC=1 SCAR=0.0
Pod documented
264  
265 401 ▶sub AUTOLOAD {
Subroutine AUTOLOAD: called CC=8 SCAR=20.9
266 401 my $self = shift;
267 401 (my $prop = our $AUTOLOAD) =~ s/.*:://;
268 401 ▶ return if $prop eq 'DESTROY' || substr($prop, 0, 1) eq '_';
Branch: 1 branch
BranchTrueFalseTotal
if $prop eq "DESTROY" or substr($prop, 0, 1) eq "_" 372 29 401
Condition66%2/3
$prop eq "DESTROY" or substr($prop, 0, 1) eq "_"
l!l&&r!l&&!r
372029
MC/DC50%1/2
$prop eq "DESTROY" or substr($prop, 0, 1) eq "_"
$prop eq "DESTROY" substr($prop, 0, 1) eq "_"
Truth table66%2/3
A or B
A= $prop eq "DESTROY"B= substr($prop, 0, 1) eq "_"
ABresult
1X1
011
000
269  
270 # $x->is_foo or $x->has_foo will return true if 'foo' is
271 # a hash key with a true value
272 return defined $self->{ substr($prop, 4) }
273 29 ▶ && $self->{ substr($prop, 4) }
Branch: 1 branch
BranchTrueFalseTotal
if substr($prop, 0, 4) eq "has_" 3 26 29
Condition66%2/3
defined $$self{substr $prop, 4} && $$self{substr $prop, 4}
!ll&&!rl&&r
102
MC/DC50%1/2
defined $$self{substr $prop, 4} && $$self{substr $prop, 4}
defined $$self{substr $prop, 4} $$self{substr $prop, 4}
Truth table66%2/3
A && B
A= defined $$self{substr $prop, 4}B= $$self{substr $prop, 4}
ABresult
0X0
100
111
274 if substr($prop, 0, 4) eq 'has_';
275 return defined $self->{ substr($prop, 3) }
276 26 ▶ && $self->{ substr($prop, 3) }
Branch: 1 branch
BranchTrueFalseTotal
if substr($prop, 0, 3) eq "is_" 8 18 26
Condition66%2/3
defined $$self{substr $prop, 3} && $$self{substr $prop, 3}
!ll&&!rl&&r
107
MC/DC50%1/2
defined $$self{substr $prop, 3} && $$self{substr $prop, 3}
defined $$self{substr $prop, 3} $$self{substr $prop, 3}
Truth table66%2/3
A && B
A= defined $$self{substr $prop, 3}B= $$self{substr $prop, 3}
ABresult
0X0
100
111
277 if substr($prop, 0, 3) eq 'is_';
278 18 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 12 6 18
279 12 $self->{$prop} = shift;
280 12 return $self;
281 }
282 6 return $self->{$prop};
283 }
284 2 1;
285  
286  
287 __END__
↑ Summary