| 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
Condition100%3/3
builtin::blessed($type) and $type->isa("Type::Tiny")
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")
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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/
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/
| |||||||||||||||||||||||||||||
| 59 | 11 | $success = 1; | |||||||||||||||||||||||||||
| 60 | } | ||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||
| 62 | 33 | ▶ | if ( $success ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 97 | 7 | ▶ | return qr/.*/ if !exists $hash{$type}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 112 | } | ||||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||
| 115 | 10 | 1; | |||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||
| 117 | __END__ | ||||||||||||||||||||||||||||