blib/lib/Type/Tie.pm

filter: Statementstmt 97.8% 179 / 183 Branchbran 76.0% 38 / 50 Conditioncond 54.5% 12 / 22 MC/DCmcdc 23.0% 3 / 13 Subroutinesub 93.7% 45 / 48 Podpod 100% 1 / 1 totaltotal 87.7% 278 / 317 CC 31 SCAR 11.4
CC
31
Cov
90%
_set_type
8.2
coerce_and_check_value
7.0
TIESCALAR
5.2
CRAP 3.1
« blib/lib/Types/Common/String.pm ↑ Summary blib/lib/Type/Coercion/Union.pm »
1 9 ▶use 5.008001;
Subroutine BEGIN: called CC=1 SCAR=0.0
2 9 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 9 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 9 ▶use Carp ();
Subroutine BEGIN: called CC=1 SCAR=0.0
6 9 ▶use Exporter::Tiny ();
Subroutine BEGIN: called CC=1 SCAR=0.0
7 9 ▶use Scalar::Util ();
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 9 ++$Carp::CarpInternal{"Type::Tie::$_"} for qw( BASE SCALAR ARRAY HASH );
10  
11 {
12 package Type::Tie;
13 9 our $AUTHORITY = 'cpan:TOBYINK';
14 9 our $VERSION = '2.010001';
15 9 our @ISA = qw( Exporter::Tiny );
16 9 our @EXPORT = qw( ttie );
17
18 9 $VERSION =~ tr/_//d;
19
20 sub ttie (\[$@%]@)#>&%*/&<%\$[]^!@;@)
21 17 ▶ {
Subroutine ttie: called CC=3 SCAR=11.0
Pod documented
22 17 my ( $ref, $type, @vals ) = @_;
23
24 17 ▶ if ( 'HASH' eq ref $ref ) {
Branch: 2 branches
BranchTrueFalseTotal
if ('HASH' eq ref $ref) { } 5 12 17
elsif ('ARRAY' eq ref $ref) { } 5 7 12
25 5 tie %$ref, "Type::Tie::HASH", $type, @vals;
26 }
27 elsif ( 'ARRAY' eq ref $ref ) {
28 5 tie @$ref, "Type::Tie::ARRAY", $type, @vals;
29 }
30 else {
31 7 tie $$ref, "Type::Tie::SCALAR", $type, @vals;
32 }
33 17 return $ref;
34 }
35 9 };
36  
37 {
38 package Type::Tie::BASE;
39 9 our $AUTHORITY = 'cpan:TOBYINK';
40 9 our $VERSION = '2.010001';
41
42 9 $VERSION =~ tr/_//d;
43
44 # Type::Tie::BASE is an array-based object. If you need to subclass it
45 # and store more attributes, use $yourclass->SUPER::_NEXT_SLOT to find
46 # the next available slot, then override _NEXT_SLOT so that other people
47 # can subclass your class too.
48 #
49 sub _REF { $_[0][0] } # ro
50 sub _TYPE { ( @_ == 2 ) ? ( $_[0][1] = $_[1] ) : $_[0][1] } # rw
51 sub _CHECK { ( @_ == 2 ) ? ( $_[0][2] = $_[1] ) : $_[0][2] } # rw
52 sub _COERCE { ( @_ == 2 ) ? ( $_[0][3] = $_[1] ) : $_[0][3] } # rw
53 1 ▶ sub _NEXT_SLOT { 4 }
Subroutine _NEXT_SLOT: called CC=1 SCAR=0.0
54
55 sub type { shift->_TYPE }
56 3 ▶ sub _INIT_REF { $_[0][0] ||= $_[0]->_DEFAULT }
Subroutine _INIT_REF: called CC=2 SCAR=9.2
Condition33%1/3
$_[0][0] ||= $_[0]->_DEFAULT
l!l&&r!l&&!r
030
MC/DC0%0/2
$_[0][0] ||= $_[0]->_DEFAULT
$_[0][0] $_[0]->_DEFAULT
Truth table33%1/3
A ||= B
A= $_[0][0]B= $_[0]->_DEFAULT
ABresult
1X1
011
000
57
58 {
59 9 my $try_xs =
60 exists( $ENV{PERL_TYPE_TINY_XS} ) ? !!$ENV{PERL_TYPE_TINY_XS} :
61 exists( $ENV{PERL_ONLY} ) ? !$ENV{PERL_ONLY} :
62 9 ▶ !!1;
Branch: 2 branches
BranchTrueFalseTotal
exists $ENV{'PERL_TYPE_TINY_XS'} ? : 0 9 9
exists $ENV{'PERL_ONLY'} ? : 0 9 9
63 9 ▶ eval {
Branch: 1 branch
BranchTrueFalseTotal
if $try_xs 9 0 9
64 9 require Class::XSAccessor::Array;
65 9 'Class::XSAccessor::Array'->import(
66 replace => !!1,
67 getters => { _REF => 0, type => 1 },
68 accessors => { _TYPE => 1, _CHECK => 2, _COERCE => 3 },
69 );
70 } if $try_xs;
71 }
72
73 27 ▶ sub _set_type {
Subroutine _set_type: called CC=8 SCAR=21.1
74 27 my $self = shift;
75 27 my $type = $_[0];
76
77 27 $self->_TYPE( $type );
78
79 27 ▶ if ( Scalar::Util::blessed( $type ) and $type->isa( 'Type::Tiny' ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed($type) and $type->isa('Type::Tiny')) { } 23 4 27
Condition66%2/3
builtin::blessed($type) and $type->isa("Type::Tiny")
!ll&&!rl&&r
0423
MC/DC50%1/2
builtin::blessed($type) and $type->isa("Type::Tiny")
builtin::blessed($type) $type->isa("Type::Tiny")
Truth table66%2/3
A and B
A= builtin::blessed($type)B= $type->isa("Type::Tiny")
ABresult
0X0
100
111
80 23 $self->_CHECK( $type->compiled_check );
81 23 ▶ $self->_COERCE(
Branch: 1 branch
BranchTrueFalseTotal
$type->has_coercion ? : 2 21 23
82 $type->has_coercion
83 ? $type->coercion->compiled_coercion
84 : undef
85 );
86 }
87 else {
88 $self->_CHECK(
89 $type->can( 'compiled_check' )
90 ? $type->compiled_check
91 26 ▶ : sub { $type->check( $_[0] ) }
Subroutine __ANON__: called CC=1 SCAR=0.0
92 4 ▶ );
Branch: 1 branch
BranchTrueFalseTotal
$type->can('compiled_check') ? : 0 4 4
93 $self->_COERCE(
94 $type->can( 'has_coercion' ) && $type->can( 'coerce' ) && $type->has_coercion
95 20 ▶ ? sub { $type->coerce( $_[0] ) }
Subroutine __ANON__: called CC=1 SCAR=0.0
96 : undef
97 4 ▶ );
Branch: 1 branch
BranchTrueFalseTotal
$type->can('has_coercion') && $type->can('coerce') && $type->has_coercion ? : 2 2 4
Condition66%4/6
$type->can("has_coercion") and $type->can("coerce") and $type->has_coercion
!ll&&!rl&&r
202
$type->can("has_coercion") and $type->can("coerce")
!ll&&!rl&&r
202
MC/DC33%1/3
$type->can("has_coercion") and $type->can("coerce") and $type->has_coercion
$type->can("has_coercion") $type->can("coerce") $type->has_coercion
Truth table50%2/4
A and B and C
A= $type->can("has_coercion")B= $type->can("coerce")C= $type->has_coercion
ABCresult
0XX0
10X0
1100
1111
98 }
99 }
100
101 # Only used if the type has no get_message method
102 1 ▶ sub _dd {
Subroutine _dd: called CC=1 SCAR=0.0
103 1 require Type::Tiny;
104 1 goto \&Type::Tiny::_dd;
105 }
106
107 83 ▶ sub coerce_and_check_value {
Subroutine coerce_and_check_value: called CC=7 SCAR=19.5
108 83 my $self = shift;
109 83 my $check = $self->_CHECK;
110 83 my $coerce = $self->_COERCE;
111
112 my @vals = map {
113 83 ▶ my $val = $coerce ? $coerce->( $_ ) : $_;
Branch: 1 branch
BranchTrueFalseTotal
$coerce ? : 40 62 102
114 102 ▶ if ( not $check->( $val ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (&$check($val)) 26 76 102
115 26 my $type = $self->_TYPE;
116 26 ▶ Carp::croak(
Branch: 1 branch
BranchTrueFalseTotal
$type && $type->can('get_message') ? : 25 1 26
Condition60%3/5
$type and $type->can("get_message")
!ll&&!rl&&r
0125
$type || 'Unknown'
l!l
10
MC/DC50%1/2
$type and $type->can("get_message")
$type $type->can("get_message")
MC/DC0%0/1
$type || 'Unknown'
$type
Truth table66%2/3
A and B
A= $typeB= $type->can("get_message")
ABresult
0X0
100
111
Truth table50%1/2
A
A= $type
Aresult
11
00
117 $type && $type->can( 'get_message' )
118 ? $type->get_message( $val )
119 : sprintf( '%s does not meet type constraint %s', _dd($_), $type || 'Unknown' )
120 );
121 }
122 76 $val;
123 } ( my @cp = @_ ); # need to copy @_ for Perl < 5.14
124
125 57 ▶ wantarray ? @vals : $vals[0];
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 23 34 57
126 }
127
128 # store the $type for the exiting instances so the type can be set
129 # (uncloned) in the clone too. A clone process could be cloning several
130 # instances of this class, so use a hash to hold the types during
131 # cloning. These types are reference counted, so the last reference to
132 # a particular type deletes its key.
133 9 my %tmp_clone_types;
134 3 ▶ sub STORABLE_freeze {
Subroutine STORABLE_freeze: called CC=3 SCAR=11.2
135 3 my ( $o, $cloning ) = @_;
136 3 ▶ Carp::croak( "Storable::freeze only supported for dclone-ing" )
Branch: 1 branch
BranchTrueFalseTotal
unless $cloning 0 3 3
137 unless $cloning;
138
139 3 my $type = $o->_TYPE;
140 3 my $refaddr = Scalar::Util::refaddr( $type );
141 3 ▶ $tmp_clone_types{$refaddr} ||= [ $type, 0 ];
Condition50%1/2
$tmp_clone_types{$refaddr} ||= [$type, 0]
l!l
03
MC/DC0%0/1
$tmp_clone_types{$refaddr} ||= [$type, 0]
$tmp_clone_types{$refaddr}
Truth table50%1/2
A
A= $tmp_clone_types{$refaddr}
Aresult
11
00
142 3 ++$tmp_clone_types{$refaddr}[1];
143
144 3 return ( $refaddr, $o->_REF );
145 }
146
147 3 ▶ sub STORABLE_thaw {
Subroutine STORABLE_thaw: called CC=3 SCAR=11.2
148 3 my ( $o, $cloning, $refaddr, $o2 ) = @_;
149 3 ▶ Carp::croak( "Storable::thaw only supported for dclone-ing" )
Branch: 1 branch
BranchTrueFalseTotal
unless $cloning 0 3 3
150 unless $cloning;
151
152 3 $o->_THAW( $o2 ); # implement in child classes
153
154 3 my $type = $tmp_clone_types{$refaddr}[0];
155 --$tmp_clone_types{$refaddr}[1]
156 3 ▶ or delete $tmp_clone_types{$refaddr};
Branch: 1 branch
BranchTrueFalseTotal
--$tmp_clone_types{$refaddr}[1] or delete $tmp_clone_types{$refaddr} 0 3 3
157 3 $o->_set_type($type);
158 }
159 9 };
160  
161 {
162 package Type::Tie::ARRAY;
163 9 our $AUTHORITY = 'cpan:TOBYINK';
164 9 our $VERSION = '2.010001';
165 9 our @ISA = qw( Type::Tie::BASE );
166
167 9 $VERSION =~ tr/_//d;
168
169 7 ▶ sub TIEARRAY {
Subroutine TIEARRAY: called CC=2 SCAR=6.9
170 7 my $class = shift;
171 7 my $type = shift;
172 7 my $self = bless( [ $class->_DEFAULT ], $class );
173 7 $self->_set_type( $type );
174 7 ▶ if ( @_ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 4 3 7
175 4 my $R = $self->_REF;
176 4 @$R = map { $self->coerce_and_check_value( $_ ) } @_;
177 }
178 7 $self;
179 }
180 8 ▶ sub _DEFAULT { [] }
Subroutine _DEFAULT: called CC=1 SCAR=0.0
181 68 ▶ sub FETCHSIZE { scalar @{ $_[0]->_REF } }
Subroutine FETCHSIZE: called CC=1 SCAR=0.0
182 1 ▶ sub STORESIZE { $#{ $_[0]->_REF } = $_[1] }
Subroutine STORESIZE: called CC=1 SCAR=0.0
183 8 ▶ sub STORE { $_[0]->_REF->[ $_[1] ] = $_[0]->coerce_and_check_value( $_[2] ) }
Subroutine STORE: called CC=1 SCAR=0.0
184 57 ▶ sub FETCH { $_[0]->_REF->[ $_[1] ] }
Subroutine FETCH: called CC=1 SCAR=0.0
185 0 ▶ sub CLEAR { @{ $_[0]->_REF } = () }
Subroutine CLEAR: not called CC=1 SCAR=6.9
186 1 ▶ sub POP { pop @{ $_[0]->_REF } }
Subroutine POP: called CC=1 SCAR=0.0
187 11 ▶ sub PUSH { my $s = shift; push @{$s->_REF}, $s->coerce_and_check_value( @_ ) }
Subroutine PUSH: called CC=1 SCAR=0.0
188 3 ▶ sub SHIFT { shift @{ $_[0]->_REF } }
Subroutine SHIFT: called CC=1 SCAR=0.0
189 7 ▶ sub UNSHIFT { my $s = shift; unshift @{$s->_REF}, $s->coerce_and_check_value( @_ ) }
Subroutine UNSHIFT: called CC=1 SCAR=0.0
190 1 ▶ sub EXISTS { exists $_[0]->_REF->[ $_[1] ] }
Subroutine EXISTS: called CC=1 SCAR=0.0
191 1 ▶ sub DELETE { delete $_[0]->_REF->[ $_[1] ] }
Subroutine DELETE: called CC=1 SCAR=0.0
192 0 ▶ sub EXTEND {}
Subroutine EXTEND: not called CC=1 SCAR=0.0
193 2 ▶ sub SPLICE {
Subroutine SPLICE: called CC=4 SCAR=14.2
194 2 my $o = shift;
195 2 my $sz = scalar @{$o->_REF};
196 2 ▶ my $off = @_ ? shift : 0;
Branch: 1 branch
BranchTrueFalseTotal
@_ ? : 2 0 2
197 2 ▶ $off += $sz if $off < 0;
Branch: 1 branch
BranchTrueFalseTotal
if $off < 0 0 2 2
198 2 ▶ my $len = @_ ? shift : $sz-$off;
Branch: 1 branch
BranchTrueFalseTotal
@_ ? : 2 0 2
199 2 splice @{$o->_REF}, $off, $len, $o->coerce_and_check_value( @_ );
200 }
201 1 ▶ sub _THAW { @{ $_[0]->_INIT_REF } = @{ $_[1] } }
Subroutine _THAW: called CC=1 SCAR=0.0
202 9 };
203  
204 {
205 package Type::Tie::HASH;
206 9 our $AUTHORITY = 'cpan:TOBYINK';
207 9 our $VERSION = '2.010001';
208 9 our @ISA = qw( Type::Tie::BASE );
209
210 9 $VERSION =~ tr/_//d;
211
212 7 ▶ sub TIEHASH {
Subroutine TIEHASH: called CC=3 SCAR=11.0
213 7 my $class = shift;
214 7 my $type = shift;
215 7 my $self = bless( [ $class->_DEFAULT ], $class );
216 7 $self->_set_type( $type );
217 7 ▶ if ( @_ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 4 3 7
218 4 my $R = $self->_REF;
219 4 my %H = @_;
220 4 %$R = ();
221 4 ▶ while ( my ( $K, $V ) = each %H ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %H) 5 4 9
222 5 $R->{$K} = $self->coerce_and_check_value( $V );
223 }
224 }
225 7 $self;
226 }
227 8 ▶ sub _DEFAULT { +{} }
Subroutine _DEFAULT: called CC=1 SCAR=0.0
228 13 ▶ sub STORE { $_[0]->_REF->{ $_[1] } = $_[0]->coerce_and_check_value( $_[2] ) }
Subroutine STORE: called CC=1 SCAR=0.0
229 13 ▶ sub FETCH { $_[0]->_REF->{ $_[1] } }
Subroutine FETCH: called CC=1 SCAR=0.0
230 8 ▶ sub FIRSTKEY { my $a = scalar keys %{ $_[0]->_REF }; each %{ $_[0]->_REF } }
Subroutine FIRSTKEY: called CC=1 SCAR=0.0
231 13 ▶ sub NEXTKEY { each %{ $_[0]->_REF } }
Subroutine NEXTKEY: called CC=1 SCAR=0.0
232 13 ▶ sub EXISTS { exists $_[0]->_REF->{ $_[1] } }
Subroutine EXISTS: called CC=1 SCAR=0.0
233 3 ▶ sub DELETE { delete $_[0]->_REF->{ $_[1] } }
Subroutine DELETE: called CC=1 SCAR=0.0
234 0 ▶ sub CLEAR { %{ $_[0]->_REF } = () }
Subroutine CLEAR: not called CC=1 SCAR=6.9
235 1 ▶ sub SCALAR { scalar %{ $_[0]->_REF } }
Subroutine SCALAR: called CC=1 SCAR=0.0
236 1 ▶ sub _THAW { %{ $_[0]->_INIT_REF } = %{ $_[1] } }
Subroutine _THAW: called CC=1 SCAR=0.0
237 9 };
238  
239 {
240 package Type::Tie::SCALAR;
241 9 our $AUTHORITY = 'cpan:TOBYINK';
242 9 our $VERSION = '2.010001';
243 9 our @ISA = qw( Type::Tie::BASE );
244
245 9 $VERSION =~ tr/_//d;
246
247 10 ▶ sub TIESCALAR {
Subroutine TIESCALAR: called CC=5 SCAR=16.5
248 10 my $class = shift;
249 10 my $type = shift;
250 10 my $self = bless( [ $class->_DEFAULT ], $class );
251 10 $self->_set_type($type);
252 10 ▶ if ( @_ ) {
Branch: 2 branches
BranchTrueFalseTotal
if (@_) { } 6 4 10
elsif ($type->can('type_default') and my $d = $type->type_default) { } 4 0 4
253 6 ▶ Carp::croak( 'Too many initial values provided for SCALAR' ) if @_ > 1;
Branch: 1 branch
BranchTrueFalseTotal
if @_ > 1 0 6 6
254 6 ${ $self->_REF } = $self->coerce_and_check_value( $_[0] );
255 }
256 ▶ elsif ( $type->can('type_default') and my $d = $type->type_default ) {
Condition33%1/3
$type->can("type_default") and my $d = $type->type_default
!ll&&!rl&&r
004
MC/DC0%0/2
$type->can("type_default") and my $d = $type->type_default
$type->can("type_default") my $d = $type->type_default
Truth table33%1/3
A and B
A= $type->can("type_default")B= my $d = $type->type_default
ABresult
0X0
100
111
257 4 ${ $self->_REF } = $d->();
258 }
259 10 $self;
260 }
261 11 ▶ sub _DEFAULT { my $x; \$x }
Subroutine _DEFAULT: called CC=1 SCAR=0.0
262 19 ▶ sub STORE { ${ $_[0]->_REF } = $_[0]->coerce_and_check_value( $_[1] ) }
Subroutine STORE: called CC=1 SCAR=0.0
263 17 ▶ sub FETCH { ${ $_[0]->_REF } }
Subroutine FETCH: called CC=1 SCAR=0.0
264 1 ▶ sub _THAW { ${ $_[0]->_INIT_REF } = ${ $_[1] } }
Subroutine _THAW: called CC=1 SCAR=0.0
265 9 };
266  
267 9 1;
268  
269 __END__
« blib/lib/Types/Common/String.pm ↑ Summary blib/lib/Type/Coercion/Union.pm »