blib/lib/Tie/CheckVariables.pm

filter: Statementstmt 100% 57 / 57 Branchbran 100% 24 / 24 Conditioncond 100% 6 / 6 MC/DCmcdc 100% 4 / 4 Subroutinesub 100% 14 / 14 Podpod 100% 2 / 2 totaltotal 100% 107 / 107 CC 15 SCAR 10.7
CC
15
Cov
100%
STORE
6.0
TIESCALAR
3.0
_check
3.0
CRAP 2.9
↑ Summary
1 package Tie::CheckVariables;
2  
3 # ABSTRACT: check/validate variables for their data type
4  
5 10 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 10 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 10 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 10 ▶use Scalar::Util qw(blessed);
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 10 our $VERSION = '0.07';
12
13 10 my %hash = (
14 integer => qr{^[-+]?\d+$},
15 float => qr{^[+-]?(\d+\.\d+|\d+\.|\.\d+|\d+)([eE][+-]?\d+)?$},
16 string => qr{.+},
17 );
18  
19 2 ▶my $error_code = sub { die "Invalid value $_[0]" };
Subroutine __ANON__: called CC=1 SCAR=0.0
20  
21 11 ▶sub TIESCALAR{
Subroutine TIESCALAR: called CC=3 SCAR=11.0
22 11 my ($class, $type) = @_;
23
24 11 my $self = {};
25 11 bless $self, $class;
26  
27 11 ▶ $self->_type(
Branch: 1 branch
BranchTrueFalseTotal
builtin::blessed($type) && $type->isa('Type::Tiny') ? : 2 9 11
Condition100%3/3
builtin::blessed($type) and $type->isa("Type::Tiny")
!ll&&!rl&&r
812
MC/DC100%2/2
builtin::blessed($type) and $type->isa("Type::Tiny")
builtin::blessed($type) $type->isa("Type::Tiny")
Truth table100%3/3
A and B
A= builtin::blessed($type)B= $type->isa("Type::Tiny")
ABresult
0X0
100
111
28 blessed $type && $type->isa('Type::Tiny') ?
29 $type->compiled_check :
30 $type
31 );
32
33 11 return $self;
34 }
35  
36 5 ▶sub FETCH {
Subroutine FETCH: called CC=1 SCAR=0.0
37 5 my $self = shift;
38 5 return $self->{VALUE};
39 }
40  
41 33 ▶sub STORE {
Subroutine STORE: called CC=6 SCAR=17.9
42 33 my ($self,$value) = @_;
43
44 33 my $check = $self->_check();
45  
46 33 my $success;
47 33 my $is_code = 'CODE' eq ref $check;
48  
49 33 ▶ if ( !defined $check ) {
Branch: 3 branches
BranchTrueFalseTotal
if (not defined $check) { } 5 28 33
elsif ($is_code) { } 7 21 28
elsif (not ref $value and $value =~ /$check/) { } 11 14 25
50 5 $self->{VALUE} = $value;
51 5 $success = 1;
52 }
53 elsif ( $is_code ) {
54 7 eval {
55 7 $success = $check->($value);
56 };
57 }
58 ▶ elsif ( !ref $value && $value =~ $check ) {
Condition100%3/3
not ref $value and $value =~ /$check/
!ll&&!rl&&r
4611
MC/DC100%2/2
not ref $value and $value =~ /$check/
not ref $value $value =~ /$check/
Truth table100%3/3
not(A) and B
A= not ref $valueB= $value =~ /$check/
ABresult
0X0
100
111
59 11 $success = 1;
60 }
61  
62 33 ▶ if ( $success ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($success) { } 20 13 33
63 20 $self->{VALUE} = $value;
64 }
65 else {
66 13 $self->{VALUE} = undef;
67 13 $error_code->( $value );
68 #croak "no valid input";
69 }
70 }
71  
72 2 ▶sub UNTIE {}
Subroutine UNTIE: called CC=1 SCAR=0.0
73  
74 33 ▶sub _check {
Subroutine _check: called CC=3 SCAR=11.0
75 33 my ($self) = @_;
76  
77 33 ▶ return $self->{CHECK} if $self->{CHECK};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"CHECK"} 18 15 33
78  
79 15 my $type = $self->_type;
80 15 my $is_code = grep{ $_ eq ref $type }qw(CODE Type::Tiny);
81 15 ▶ $self->{CHECK} = !$is_code ? _get_regex( $type ) : $type;
Branch: 1 branch
BranchTrueFalseTotal
!$is_code ? : 12 3 15
82  
83 15 return $self->{CHECK};
84 }
85  
86 26 ▶sub _type {
Subroutine _type: called CC=2 SCAR=6.9
87 26 my ($self,$type) = @_;
88  
89 26 ▶ $self->{TYPE} = $type if defined $type;
Branch: 1 branch
BranchTrueFalseTotal
if defined $type 10 16 26
90 26 return $self->{TYPE};
91 }
92  
93 12 ▶sub _get_regex {
Subroutine _get_regex: called CC=3 SCAR=11.0
94 12 my ($type) = @_;
95  
96 12 ▶ return if !$type;
Branch: 1 branch
BranchTrueFalseTotal
unless $type 5 7 12
97 7 ▶ return qr/.*/ if !exists $hash{$type};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $hash{$type} 2 5 7
98 5 return $hash{$type};
99 }
100  
101 2 ▶sub register {
Subroutine register: called CC=2 SCAR=6.9
Pod documented
102 2 my ($class,$type,$regex) = @_;
103  
104 2 ▶ return if $class ne 'Tie::CheckVariables';
Branch: 1 branch
BranchTrueFalseTotal
if $class ne "Tie::CheckVariables" 1 1 2
105  
106 1 $hash{$type} = qr{$regex};
107 }
108  
109 8 ▶sub on_error {
Subroutine on_error: called CC=2 SCAR=6.9
Pod documented
110 8 my ($class,$coderef) = @_;
111 8 ▶ $error_code = $coderef if 'CODE' eq ref $coderef;
Branch: 1 branch
BranchTrueFalseTotal
if "CODE" eq ref $coderef 7 1 8
112 }
113  
114  
115 10 1;
116  
117 __END__
↑ Summary