| 1 | package Variable::Strongly::Typed; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 8 | ▶ | use version; $VERSION = qv('1.1.0'); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||
| 5 | 8 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 8 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 8 | ▶ | use Carp; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | # Tie any 'TYPE' attributes to me | ||||||||||||||||||||||||||||
| 10 | 8 | ▶ | use Attribute::Handlers; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||
| 12 | # Our 3 friends for scalars & arrays & hashes | ||||||||||||||||||||||||||||
| 13 | 8 | ▶ | use Variable::Strongly::Typed::Scalar; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 14 | 8 | ▶ | use Variable::Strongly::Typed::Array; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 15 | 8 | ▶ | use Variable::Strongly::Typed::Hash; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 16 | 8 | ▶ | use Variable::Strongly::Typed::Validators; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 17 | 8 | ▶ | use Variable::Strongly::Typed::Code; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||
| 19 | # Can't use Class::Std as it wants the 'ATTR' attribute | ||||||||||||||||||||||||||||
| 20 | # as does Attribute::Handlers... Bummer | ||||||||||||||||||||||||||||
| 21 | 8 | ▶ | use Class::Std::Utils; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 22 | { | ||||||||||||||||||||||||||||
| 23 | # class variables | ||||||||||||||||||||||||||||
| 24 | 8 | my %actual_object_of; # The actual object ref | |||||||||||||||||||||||||||
| 25 | my %validator_sub_of; # Validate subroutine | ||||||||||||||||||||||||||||
| 26 | 8 | my %type_of; # Strong Type of this variable | |||||||||||||||||||||||||||
| 27 | 8 | my %error_handler_of; # Error handling function to call when | |||||||||||||||||||||||||||
| 28 | # assignment/return goes wrong | ||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||
| 30 | do { | ||||||||||||||||||||||||||||
| 31 | 8 | ▶ | no warnings 'redefine'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 32 | 3 | ▶ | sub UNIVERSAL::TYPE : ATTR(SCALAR) { | ||||||||||||||||||||||||||
|
Subroutine
TYPE: called CC=1 SCAR=0.4
Pod
documented
| |||||||||||||||||||||||||||||
| 33 | 3 | my ($package, $symbol, $referent, $attr, $data) = @_; | |||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||
| 35 | 3 | tie $$referent, 'Variable::Strongly::Typed::Scalar', $data; | |||||||||||||||||||||||||||
| 36 | 0 | return; | |||||||||||||||||||||||||||
| 37 | 8 | ▶ | } | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||
| 39 | 1 | ▶ | sub UNIVERSAL::TYPE : ATTR(ARRAY) { | ||||||||||||||||||||||||||
|
Subroutine
TYPE: called CC=1 SCAR=0.4
Pod
documented
| |||||||||||||||||||||||||||||
| 40 | 1 | my ($package, $symbol, $referent, $attr, $data) = @_; | |||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||
| 42 | 1 | tie @$referent, 'Variable::Strongly::Typed::Array', $data; | |||||||||||||||||||||||||||
| 43 | 0 | return; | |||||||||||||||||||||||||||
| 44 | 8 | ▶ | } | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||
| 46 | 1 | ▶ | sub UNIVERSAL::TYPE : ATTR(HASH) { | ||||||||||||||||||||||||||
|
Subroutine
TYPE: called CC=1 SCAR=0.4
Pod
documented
| |||||||||||||||||||||||||||||
| 47 | 1 | my ($package, $symbol, $referent, $attr, $data) = @_; | |||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||
| 49 | 1 | tie %$referent, 'Variable::Strongly::Typed::Hash', $data; | |||||||||||||||||||||||||||
| 50 | 0 | return; | |||||||||||||||||||||||||||
| 51 | 8 | ▶ | } | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||
| 53 | 2 | ▶ | sub UNIVERSAL::TYPE : ATTR(CODE) { | ||||||||||||||||||||||||||
|
Subroutine
TYPE: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 54 | 2 | my ($package, $symbol, $referent, $attr, $data) = @_; | |||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||
| 56 | 2 | Variable::Strongly::Typed::Code->new($symbol, $referent, $data); | |||||||||||||||||||||||||||
| 57 | 1 | return; | |||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||
| 59 | 8 | ▶ | } | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 60 | 8 | }; | |||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||
| 62 | # Set up our class variables | ||||||||||||||||||||||||||||
| 63 | 7 | ▶ | sub _init { | ||||||||||||||||||||||||||
|
Subroutine
_init: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||
| 64 | 7 | my($self, $object, $type) = @_; | |||||||||||||||||||||||||||
| 65 | 7 | my $ident = ident $self; | |||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||
| 67 | 7 | my $error_handler; | |||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||
| 69 | 7 | ▶ | if (ref $type eq 'ARRAY') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 70 | 7 | ($type, $error_handler) = @$type; | |||||||||||||||||||||||||||
| 71 | 7 | ▶ | croak("Error handler must be a CODE ref!") | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 72 | if (ref $error_handler ne 'CODE'); | ||||||||||||||||||||||||||||
| 73 | } | ||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||
| 75 | 1 | $actual_object_of {$ident} = $object; | |||||||||||||||||||||||||||
| 76 | 1 | $validator_sub_of {$ident} = _make_validator_sub($type); | |||||||||||||||||||||||||||
| 77 | 1 | $type_of {$ident} = $type; | |||||||||||||||||||||||||||
| 78 | 1 | $error_handler_of {$ident} = $error_handler; | |||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||
| 80 | 1 | return $self; | |||||||||||||||||||||||||||
| 81 | } | ||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||
| 83 | # Get actual object ref | ||||||||||||||||||||||||||||
| 84 | 7 | ▶ | sub _get_object { | ||||||||||||||||||||||||||
|
Subroutine
_get_object: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 85 | 7 | my($self) = @_; | |||||||||||||||||||||||||||
| 86 | 7 | return $actual_object_of{ident $self}; | |||||||||||||||||||||||||||
| 87 | } | ||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||
| 89 | 1 | ▶ | sub _get_type { | ||||||||||||||||||||||||||
|
Subroutine
_get_type: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 90 | 1 | my($self) = @_; | |||||||||||||||||||||||||||
| 91 | 1 | return $type_of{ident $self}; | |||||||||||||||||||||||||||
| 92 | } | ||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||
| 94 | 0 | ▶ | sub _get_valid_sub { | ||||||||||||||||||||||||||
|
Subroutine
_get_valid_sub: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 95 | 0 | my($self) = @_; | |||||||||||||||||||||||||||
| 96 | 0 | return $validator_sub_of{ident $self}; | |||||||||||||||||||||||||||
| 97 | } | ||||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||
| 99 | 0 | ▶ | sub _get_error_handler { | ||||||||||||||||||||||||||
|
Subroutine
_get_error_handler: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 100 | 0 | my($self) = @_; | |||||||||||||||||||||||||||
| 101 | 0 | return $error_handler_of{ident $self}; | |||||||||||||||||||||||||||
| 102 | } | ||||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||
| 104 | 0 | ▶ | sub _error { | ||||||||||||||||||||||||||
|
Subroutine
_error: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||
| 105 | 0 | my($self, $val) = @_; | |||||||||||||||||||||||||||
| 106 | 0 | my $eh = $self->_get_error_handler; | |||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||
| 108 | 0 | ▶ | if ($eh) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 109 | 0 | $eh->($val); | |||||||||||||||||||||||||||
| 110 | } else { | ||||||||||||||||||||||||||||
| 111 | 0 | croak($self->_make_error_message($val)); | |||||||||||||||||||||||||||
| 112 | } | ||||||||||||||||||||||||||||
| 113 | } | ||||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||
| 115 | # Check if a value is okay... | ||||||||||||||||||||||||||||
| 116 | # IF this returns w/o croaking then all is okay... | ||||||||||||||||||||||||||||
| 117 | 0 | ▶ | sub _check_values { | ||||||||||||||||||||||||||
|
Subroutine
_check_values: not called CC=3 SCAR=24.8
| |||||||||||||||||||||||||||||
| 118 | 0 | my ($self, @values) = @_; | |||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||
| 120 | 0 | my($valid_sub) = $self->_get_valid_sub; | |||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||
| 122 | # if valid or failed sub says it's okay then ok | ||||||||||||||||||||||||||||
| 123 | # otherwise undef | ||||||||||||||||||||||||||||
| 124 | # Note this works slightly differently then Tie::Constrained | ||||||||||||||||||||||||||||
| 125 | # as we have no notion of an 'original' or 'default' value | ||||||||||||||||||||||||||||
| 126 | # 'failed' sub will croak if not valid... | ||||||||||||||||||||||||||||
| 127 | 0 | foreach my $val (@values) { | |||||||||||||||||||||||||||
| 128 | |||||||||||||||||||||||||||||
| 129 | # This'll croak (by default) if a value is not valid | ||||||||||||||||||||||||||||
| 130 | 0 | ▶ | $valid_sub->($val) || $self->_error($val); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 131 | } | ||||||||||||||||||||||||||||
| 132 | |||||||||||||||||||||||||||||
| 133 | # if we got this far all must be good | ||||||||||||||||||||||||||||
| 134 | 0 | return \@values; | |||||||||||||||||||||||||||
| 135 | } | ||||||||||||||||||||||||||||
| 136 | |||||||||||||||||||||||||||||
| 137 | 0 | ▶ | sub _make_error_message { | ||||||||||||||||||||||||||
|
Subroutine
_make_error_message: not called CC=7 SCAR=40.3
| |||||||||||||||||||||||||||||
| 138 | 0 | my($self, $val) = @_; | |||||||||||||||||||||||||||
| 139 | |||||||||||||||||||||||||||||
| 140 | # 'num', 'string', 'IO::File' or CODE ref if user defined | ||||||||||||||||||||||||||||
| 141 | 0 | my $valid_type = $self->_get_type; | |||||||||||||||||||||||||||
| 142 | |||||||||||||||||||||||||||||
| 143 | # Scalar, Array, or Hash | ||||||||||||||||||||||||||||
| 144 | 0 | my ($variable_type) = $self =~ /:: ([^:=]+) =/xms; | |||||||||||||||||||||||||||
| 145 | 0 | $variable_type = lc $variable_type; | |||||||||||||||||||||||||||
| 146 | |||||||||||||||||||||||||||||
| 147 | # String-ify value | ||||||||||||||||||||||||||||
| 148 | 0 | ▶ | if (ref $val) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 149 | 0 | $val = (ref $val) . ' reference'; | |||||||||||||||||||||||||||
| 150 | } else { | ||||||||||||||||||||||||||||
| 151 | 0 | $val = "'$val'"; | |||||||||||||||||||||||||||
| 152 | } | ||||||||||||||||||||||||||||
| 153 | |||||||||||||||||||||||||||||
| 154 | 0 | ▶ | my $ww = $variable_type eq 'code' ? 'return' : 'assign'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 155 | 0 | ▶ | my $from_or_to = $variable_type eq 'code' ? 'from' : 'to'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 156 | 0 | ▶ | $variable_type = 'function or method' if $variable_type eq 'code'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 157 | |||||||||||||||||||||||||||||
| 158 | 0 | my $msg; | |||||||||||||||||||||||||||
| 159 | 0 | ▶ | if (ref $valid_type eq 'CODE') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 160 | 0 | $msg = "Cannot $ww $val $from_or_to a user-validated" | |||||||||||||||||||||||||||
| 161 | . " $variable_type variable!"; | ||||||||||||||||||||||||||||
| 162 | } else { | ||||||||||||||||||||||||||||
| 163 | 0 | ▶ | $msg = "Cannot $ww $val $from_or_to a" | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 164 | . ($variable_type eq 'array' ? 'n' : '') | ||||||||||||||||||||||||||||
| 165 | . " $variable_type of type $valid_type!"; | ||||||||||||||||||||||||||||
| 166 | } | ||||||||||||||||||||||||||||
| 167 | |||||||||||||||||||||||||||||
| 168 | 0 | return $msg; | |||||||||||||||||||||||||||
| 169 | } | ||||||||||||||||||||||||||||
| 170 | |||||||||||||||||||||||||||||
| 171 | 1 | ▶ | sub _make_validator_sub { | ||||||||||||||||||||||||||
|
Subroutine
_make_validator_sub: called CC=5 SCAR=18.3
| |||||||||||||||||||||||||||||
| 172 | 1 | my($val) = @_; | |||||||||||||||||||||||||||
| 173 | |||||||||||||||||||||||||||||
| 174 | # User supplied?? | ||||||||||||||||||||||||||||
| 175 | 1 | ▶ | return $val if ($val && (ref($val) eq 'CODE')); | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$val and ref $val eq "CODE"
MC/DC0%0/2
$val and ref $val eq "CODE"
$val
ref $val eq "CODE"
Truth table33%1/3
A and B
A= $valB= ref $val eq "CODE"
| |||||||||||||||||||||||||||||
| 176 | |||||||||||||||||||||||||||||
| 177 | 1 | my $condition; | |||||||||||||||||||||||||||
| 178 | |||||||||||||||||||||||||||||
| 179 | # Stuff like 'int', 'string', 'float' ... 'primitive' types | ||||||||||||||||||||||||||||
| 180 | 1 | $condition = $Variable::Strongly::Typed::Validators::conditions{$val}; | |||||||||||||||||||||||||||
| 181 | |||||||||||||||||||||||||||||
| 182 | 1 | ▶ | unless ($condition) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 183 | # It's either SCALAR, ARRAY, HASH the exact object type | ||||||||||||||||||||||||||||
| 184 | # or an isa... A reference type basically | ||||||||||||||||||||||||||||
| 185 | 0 | $condition = | |||||||||||||||||||||||||||
| 186 | 'ref && ((ref eq ' . "'$val'" . ')' | ||||||||||||||||||||||||||||
| 187 | . ' || (ref ne "SCALAR" && ref ne "ARRAY"' | ||||||||||||||||||||||||||||
| 188 | . ' && ref ne "HASH" && $_->isa(' . "'$val'" . ')))'; | ||||||||||||||||||||||||||||
| 189 | } | ||||||||||||||||||||||||||||
| 190 | |||||||||||||||||||||||||||||
| 191 | # Concoct validate sub | ||||||||||||||||||||||||||||
| 192 | 1 | my $sub = | |||||||||||||||||||||||||||
| 193 | eval 'sub { local $_ = shift;' . ' !$_ || (' . $condition . ') }'; | ||||||||||||||||||||||||||||
| 194 | |||||||||||||||||||||||||||||
| 195 | 1 | ▶ | if ($@) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 196 | 0 | croak("Unable to create a Strongly Typed $val!!"); | |||||||||||||||||||||||||||
| 197 | } | ||||||||||||||||||||||||||||
| 198 | |||||||||||||||||||||||||||||
| 199 | 1 | return $sub; | |||||||||||||||||||||||||||
| 200 | } | ||||||||||||||||||||||||||||
| 201 | |||||||||||||||||||||||||||||
| 202 | # Clean Up, Clean Up, Everybody's Doing Some, Clean Up | ||||||||||||||||||||||||||||
| 203 | # -Walter (26 months) | ||||||||||||||||||||||||||||
| 204 | 6 | ▶ | sub DESTROY { | ||||||||||||||||||||||||||
|
Subroutine
DESTROY: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 205 | 6 | my($self) = @_; | |||||||||||||||||||||||||||
| 206 | 6 | my $ident = ident $self; | |||||||||||||||||||||||||||
| 207 | |||||||||||||||||||||||||||||
| 208 | 6 | my $obj = $self->_get_object; | |||||||||||||||||||||||||||
| 209 | 6 | undef $obj; | |||||||||||||||||||||||||||
| 210 | 6 | delete $actual_object_of{$ident}; | |||||||||||||||||||||||||||
| 211 | 6 | delete $validator_sub_of{$ident}; | |||||||||||||||||||||||||||
| 212 | 6 | delete $type_of {$ident}; | |||||||||||||||||||||||||||
| 213 | } | ||||||||||||||||||||||||||||
| 214 | |||||||||||||||||||||||||||||
| 215 | } | ||||||||||||||||||||||||||||
| 216 | |||||||||||||||||||||||||||||
| 217 | 8 | 1; # Magic true value required at end of module | |||||||||||||||||||||||||||
| 218 | __END__ | ||||||||||||||||||||||||||||