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