line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Tie::CheckVariables; |
2
|
|
|
|
|
|
|
|
3
|
|
|
|
|
|
|
# ABSTRACT: check/validate variables for their datatype |
4
|
|
|
|
|
|
|
|
5
|
9
|
|
|
9
|
|
869674
|
use strict; |
|
9
|
|
|
|
|
58
|
|
|
9
|
|
|
|
|
266
|
|
6
|
9
|
|
|
9
|
|
53
|
use warnings; |
|
9
|
|
|
|
|
19
|
|
|
9
|
|
|
|
|
276
|
|
7
|
|
|
|
|
|
|
|
8
|
9
|
|
|
9
|
|
48
|
use Carp; |
|
9
|
|
|
|
|
17
|
|
|
9
|
|
|
|
|
579
|
|
9
|
9
|
|
|
9
|
|
63
|
use Scalar::Util qw(blessed); |
|
9
|
|
|
|
|
19
|
|
|
9
|
|
|
|
|
6414
|
|
10
|
|
|
|
|
|
|
|
11
|
|
|
|
|
|
|
our $VERSION = 0.05; |
12
|
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
my %hash = ( |
14
|
|
|
|
|
|
|
integer => qr{^[-+]?\d+$}, |
15
|
|
|
|
|
|
|
float => qr{^[+-]?(\d+\.\d+|\d+\.|\.\d+|\d+)([eE][+-]?\d+)?$}, |
16
|
|
|
|
|
|
|
string => qr{.+}, |
17
|
|
|
|
|
|
|
); |
18
|
|
|
|
|
|
|
|
19
|
|
|
|
|
|
|
my $error_code = sub { die "Invalid value $_[0]" }; |
20
|
|
|
|
|
|
|
|
21
|
|
|
|
|
|
|
sub TIESCALAR{ |
22
|
10
|
|
|
10
|
|
4136
|
my ($class, $type) = @_; |
23
|
|
|
|
|
|
|
|
24
|
10
|
|
|
|
|
22
|
my $self = {}; |
25
|
10
|
|
|
|
|
26
|
bless $self, $class; |
26
|
|
|
|
|
|
|
|
27
|
10
|
100
|
100
|
|
|
97
|
$self->_type( |
28
|
|
|
|
|
|
|
blessed $type && $type->isa('Type::Tiny') ? |
29
|
|
|
|
|
|
|
$type->compiled_check : |
30
|
|
|
|
|
|
|
$type |
31
|
|
|
|
|
|
|
); |
32
|
|
|
|
|
|
|
|
33
|
10
|
|
|
|
|
33
|
return $self; |
34
|
|
|
|
|
|
|
} |
35
|
|
|
|
|
|
|
|
36
|
|
|
|
|
|
|
sub FETCH { |
37
|
5
|
|
|
5
|
|
83
|
my $self = shift; |
38
|
5
|
|
|
|
|
17
|
return $self->{VALUE}; |
39
|
|
|
|
|
|
|
} |
40
|
|
|
|
|
|
|
|
41
|
|
|
|
|
|
|
sub STORE { |
42
|
31
|
|
|
31
|
|
9262
|
my ($self,$value) = @_; |
43
|
|
|
|
|
|
|
|
44
|
31
|
|
|
|
|
89
|
my $check = $self->_check(); |
45
|
|
|
|
|
|
|
|
46
|
31
|
|
|
|
|
60
|
my $success; |
47
|
31
|
|
|
|
|
100
|
my $is_code = 'CODE' eq ref $check; |
48
|
|
|
|
|
|
|
|
49
|
31
|
100
|
100
|
|
|
230
|
if ( !defined $check ) { |
|
|
100
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
50
|
5
|
|
|
|
|
10
|
$self->{VALUE} = $value; |
51
|
5
|
|
|
|
|
10
|
$success = 1; |
52
|
|
|
|
|
|
|
} |
53
|
|
|
|
|
|
|
elsif ( $is_code ) { |
54
|
7
|
|
|
|
|
13
|
eval { |
55
|
7
|
|
|
|
|
16
|
$success = $check->($value); |
56
|
|
|
|
|
|
|
}; |
57
|
|
|
|
|
|
|
} |
58
|
|
|
|
|
|
|
elsif ( !ref $value && $value =~ $check ) { |
59
|
10
|
|
|
|
|
19
|
$success = 1; |
60
|
|
|
|
|
|
|
} |
61
|
|
|
|
|
|
|
|
62
|
31
|
100
|
|
|
|
108
|
if ( $success ) { |
63
|
19
|
|
|
|
|
74
|
$self->{VALUE} = $value; |
64
|
|
|
|
|
|
|
} |
65
|
|
|
|
|
|
|
else { |
66
|
12
|
|
|
|
|
28
|
$self->{VALUE} = undef; |
67
|
12
|
|
|
|
|
33
|
$error_code->( $value ); |
68
|
|
|
|
|
|
|
#croak "no valid input"; |
69
|
|
|
|
|
|
|
} |
70
|
|
|
|
|
|
|
} |
71
|
|
|
|
|
|
|
|
72
|
|
|
|
2
|
|
|
sub UNTIE {} |
73
|
|
|
|
|
|
|
|
74
|
|
|
|
|
|
|
sub _check { |
75
|
31
|
|
|
31
|
|
57
|
my ($self) = @_; |
76
|
|
|
|
|
|
|
|
77
|
31
|
100
|
|
|
|
100
|
return $self->{CHECK} if $self->{CHECK}; |
78
|
|
|
|
|
|
|
|
79
|
14
|
|
|
|
|
44
|
my $type = $self->_type; |
80
|
14
|
|
|
|
|
33
|
my $is_code = grep{ $_ eq ref $type }qw(CODE Type::Tiny); |
|
28
|
|
|
|
|
87
|
|
81
|
14
|
100
|
|
|
|
56
|
$self->{CHECK} = !$is_code ? _get_regex( $type ) : $type; |
82
|
|
|
|
|
|
|
|
83
|
14
|
|
|
|
|
55
|
return $self->{CHECK}; |
84
|
|
|
|
|
|
|
} |
85
|
|
|
|
|
|
|
|
86
|
|
|
|
|
|
|
sub _type { |
87
|
24
|
|
|
24
|
|
110
|
my ($self,$type) = @_; |
88
|
|
|
|
|
|
|
|
89
|
24
|
100
|
|
|
|
84
|
$self->{TYPE} = $type if defined $type; |
90
|
24
|
|
|
|
|
55
|
return $self->{TYPE}; |
91
|
|
|
|
|
|
|
} |
92
|
|
|
|
|
|
|
|
93
|
|
|
|
|
|
|
sub _get_regex { |
94
|
11
|
|
|
11
|
|
27
|
my ($type) = @_; |
95
|
|
|
|
|
|
|
|
96
|
11
|
100
|
|
|
|
35
|
return if !$type; |
97
|
6
|
100
|
|
|
|
36
|
return qr/.*/ if !exists $hash{$type}; |
98
|
4
|
|
|
|
|
22
|
return $hash{$type}; |
99
|
|
|
|
|
|
|
} |
100
|
|
|
|
|
|
|
|
101
|
|
|
|
|
|
|
sub register { |
102
|
2
|
|
|
2
|
1
|
10
|
my ($class,$type,$regex) = @_; |
103
|
|
|
|
|
|
|
|
104
|
2
|
100
|
|
|
|
10
|
return if $class ne 'Tie::CheckVariables'; |
105
|
|
|
|
|
|
|
|
106
|
1
|
|
|
|
|
7
|
$hash{$type} = qr{$regex}; |
107
|
|
|
|
|
|
|
} |
108
|
|
|
|
|
|
|
|
109
|
|
|
|
|
|
|
sub on_error { |
110
|
8
|
|
|
8
|
1
|
1440
|
my ($class,$coderef) = @_; |
111
|
8
|
100
|
|
|
|
74
|
$error_code = $coderef if 'CODE' eq ref $coderef; |
112
|
|
|
|
|
|
|
} |
113
|
|
|
|
|
|
|
|
114
|
|
|
|
|
|
|
|
115
|
|
|
|
|
|
|
1; |
116
|
|
|
|
|
|
|
|
117
|
|
|
|
|
|
|
__END__ |