Class/Base.pm

filter: Statementstmt 98.5% 68 / 69 Branchbran 70.0% 21 / 30 Conditioncond 66.6% 2 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 100% 14 / 14 Podpod 0.0% 0 / 2 totaltotal 87.5% 105 / 120 CC 19 SCAR 14.8
CC
19
Cov
89%
has
8.1
hold
6.2
__ANON__
4.0
CRAP 4.4
« Class/Prototype.pm ↑ Summary Class/Errors.pm »
1 package Validation::Class::Base;
2 {
3 56 $Validation::Class::Base::VERSION = '7.10_01';
4 }
5  
6 56 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 56 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 56 our $VERSION = '7.10_01'; # VERSION
10  
11 56 ▶use Exporter ();
Subroutine BEGIN: called CC=1 SCAR=0.0
12 56 ▶use Carp 'confess';
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 56 our @ISA = qw(Exporter);
15 56 our @EXPORT = qw(
16  
17 has
18 hold
19  
20 );
21  
22 sub has {
23
24 2184 ▶ my ($attrs, $default) = @_;
Subroutine has: called CC=8 SCAR=20.9
Pod undocumented
25  
26 2184 ▶ return unless $attrs;
Branch: 1 branch
BranchTrueFalseTotal
unless $attrs 0 2184 2184
27  
28 2184 ▶ confess "Error creating accessor, default must be a coderef or constant"
Branch: 1 branch
BranchTrueFalseTotal
if ref $default and ref $default ne "CODE" 0 2184 2184
Condition66%2/3
ref $default and ref $default ne "CODE"
!ll&&!rl&&r
19042800
MC/DC0%0/2
ref $default and ref $default ne "CODE"
ref $default ref $default ne "CODE"
Truth table66%2/3
A and B
A= ref $defaultB= ref $default ne "CODE"
ABresult
0X0
100
111
29 if ref $default && ref $default ne 'CODE';
30  
31 2184 ▶ $attrs = [$attrs] unless ref $attrs eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $attrs eq "ARRAY" 2184 0 2184
32  
33 2184 for my $attr (@$attrs) {
34  
35 2184 ▶ confess "Error creating accessor '$attr', name has invalid characters"
Branch: 1 branch
BranchTrueFalseTotal
unless $attr =~ /^[a-zA-Z_]\w*$/ 0 2184 2184
36 unless $attr =~ /^[a-zA-Z_]\w*$/;
37
38 2184 my $code ;
39
40 2184 ▶ if (defined $default) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $default) { } 560 1624 2184
41
42 $code = sub {
43
44 2052 ▶ if (@_ == 1) {
Subroutine __ANON__: called CC=4 SCAR=13.9
Branch: 1 branch
BranchTrueFalseTotal
if (@_ == 1) 1977 75 2052
45 1977 ▶ return $_[0]->{$attr} if exists $_[0]->{$attr};
Branch: 1 branch
BranchTrueFalseTotal
if exists $_[0]{$attr} 1597 380 1977
46 380 ▶ return $_[0]->{$attr} = ref $default eq 'CODE' ?
Branch: 1 branch
BranchTrueFalseTotal
ref $default eq 'CODE' ? : 289 91 380
47 $default->($_[0]) : $default;
48 }
49 75 $_[0]->{$attr} = $_[1]; $_[0];
50
51 560 };
52
53 }
54
55 else {
56
57 $code = sub {
58
59 1352 ▶ return $_[0]->{$attr} if @_ == 1;
Subroutine __ANON__: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
if @_ == 1 1337 15 1352
60 15 $_[0]->{$attr} = $_[1]; $_[0];
61
62 1624 };
63
64 }
65
66 56 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
67 56 ▶ no warnings 'redefine';
Subroutine BEGIN: called CC=1 SCAR=0.0
68
69 2184 my $class = caller(0);
70
71 2184 *{"$class\::$attr"} = $code;
72
73 }
74
75 }
76  
77 sub hold {
78
79 560 ▶ my ($attrs, $default) = @_;
Subroutine hold: called CC=6 SCAR=18.3
Pod undocumented
80  
81 560 ▶ return unless $attrs;
Branch: 1 branch
BranchTrueFalseTotal
unless $attrs 0 560 560
82  
83 560 ▶ confess "Error creating accessor, default is required and must be a coderef"
Branch: 1 branch
BranchTrueFalseTotal
if ref $default ne "CODE" 0 560 560
84 if ref $default ne 'CODE';
85  
86 560 ▶ $attrs = [$attrs] unless ref $attrs eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $attrs eq "ARRAY" 560 0 560
87  
88 560 for my $attr (@$attrs) {
89  
90 560 ▶ confess "Error creating accessor '$attr', name has invalid characters"
Branch: 1 branch
BranchTrueFalseTotal
unless $attr =~ /^[a-zA-Z_]\w*$/ 0 560 560
91 unless $attr =~ /^[a-zA-Z_]\w*$/;
92
93 560 my $code ;
94
95 $code = sub {
96
97 14046 ▶ if (@_ == 1) {
Subroutine __ANON__: called CC=3 SCAR=11.4
Branch: 1 branch
BranchTrueFalseTotal
if (@_ == 1) 14046 0 14046
98 14046 ▶ return $_[0]->{$attr} if exists $_[0]->{$attr};
Branch: 1 branch
BranchTrueFalseTotal
if exists $_[0]{$attr} 13920 126 14046
99 126 return $_[0]->{$attr} = $default->($_[0]);
100 }
101
102 # values are read-only cannot be changed
103 0 confess "Error attempting to modify the read-only attribute ($attr)";
104
105 560 };
106
107 56 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
108 56 ▶ no warnings 'redefine';
Subroutine BEGIN: called CC=1 SCAR=0.0
109
110 560 my $class = caller(0);
111
112 560 *{"$class\::$attr"} = $code;
113
114 }
115
116 }
117  
118 sub import {
119
120 112 ▶ strict->import;
Subroutine import: called CC=1 SCAR=0.0
121 112 warnings->import;
122
123 112 __PACKAGE__->export_to_level(1, @_);
124
125 }
126  
127 56 1;
« Class/Prototype.pm ↑ Summary Class/Errors.pm »