blib/lib/Type/Library.pm

filter: Statementstmt 88.3% 198 / 224 Branchbran 80.0% 64 / 80 Conditioncond 73.7% 45 / 61 MC/DCmcdc 54.2% 19 / 35 Subroutinesub 94.5% 35 / 37 Podpod 100% 11 / 11 totaltotal 83.0% 372 / 448 CC 71 SCAR 22.8
CC
71
Cov
84%
_remove_type
27.5
_exporter_expand_sub
17.9
add_type
15.1
CRAP 9.8
« lib/Types/Standard/ArrayRef.pm ↑ Summary blib/lib/Type/Params/Signature.pm »
1 package Type::Library;
2  
3 278 ▶use 5.008001;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 278 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 278 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 278 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
8 278 $Type::Library::AUTHORITY = 'cpan:TOBYINK';
9 278 $Type::Library::VERSION = '2.010001';
10 }
11  
12 278 $Type::Library::VERSION =~ tr/_//d;
13  
14 278 ▶use Eval::TypeTiny qw< eval_closure set_subname type_to_coderef NICE_PROTOTYPES >;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 278 ▶use Scalar::Util qw< blessed refaddr >;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 278 ▶use Type::Tiny ();
Subroutine BEGIN: called CC=1 SCAR=0.0
17 278 ▶use Types::TypeTiny ();
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19 278 require Exporter::Tiny;
20 278 our @ISA = 'Exporter::Tiny';
21  
22 8 ▶sub _croak ($;@) { require Error::TypeTiny; goto \&Error::TypeTiny::croak }
Subroutine _croak: called CC=1 SCAR=0.0
23  
24 ####
25 #### Hooks for Exporter::Tiny
26 ####
27  
28 # Handling for -base, -extends, and -utils tags.
29 #
30 1190 ▶sub _exporter_validate_opts {
Subroutine _exporter_validate_opts: called CC=3 SCAR=11.0
31 1190 my ( $class, $opts ) = ( shift, @_ );
32
33 443 $class->setup_type_library( @{$opts}{qw/ into utils extends /}, $opts )
34 1190 ▶ if $_[0]{base} || $_[0]{extends};
Branch: 1 branch
BranchTrueFalseTotal
if $_[0]{"base"} or $_[0]{"extends"} 443 747 1190
Condition100%3/3
$_[0]{"base"} or $_[0]{"extends"}
l!l&&r!l&&!r
41627747
MC/DC100%2/2
$_[0]{"base"} or $_[0]{"extends"}
$_[0]{"base"} $_[0]{"extends"}
Truth table100%3/3
A or B
A= $_[0]{"base"}B= $_[0]{"extends"}
ABresult
1X1
011
000
35
36 1190 return $class->SUPER::_exporter_validate_opts( @_ );
37 }
38  
39 # In Exporter::Tiny, this method takes a sub name, a 'value' (i.e.
40 # potentially an options hashref for the export), and some global
41 # options, and returns a list of name+coderef pairs to actually
42 # export. We override it to provide some useful features.
43 #
44 21130 ▶sub _exporter_expand_sub {
Subroutine _exporter_expand_sub: called CC=17 SCAR=28.9
45 21130 my ( $class, $name, $value, $globals ) = ( shift, @_ );
46
47 # Handle exporting '+Type'.
48 #
49 # Note that this recurses, so if used in conjunction with the other
50 # special cases handled by this method, will still work.
51 #
52 21130 ▶ if ( $name =~ /^\+(.+)/ and $class->has_type( "$1" ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($name =~ /^\+(.+)/ and $class->has_type("$1")) 13 21117 21130
Condition66%2/3
$name =~ /^\+(.+)/ and $class->has_type("$1")
!ll&&!rl&&r
21117013
MC/DC50%1/2
$name =~ /^\+(.+)/ and $class->has_type("$1")
$name =~ /^\+(.+)/ $class->has_type("$1")
Truth table66%2/3
A and B
A= $name =~ /^\+(.+)/B= $class->has_type("$1")
ABresult
0X0
100
111
53 13 my $type = $class->get_type( "$1" );
54 13 my $exported = $type->exportables;
55 return map $class->_exporter_expand_sub(
56 $_->{name},
57 13 ▶ +{ %{ $value || {} } },
Condition50%1/2
$value || {}
l!l
058
MC/DC0%0/1
$value || {}
$value
Truth table50%1/2
A
A= $value
Aresult
11
00
58 $globals,
59 ), @$exported;
60 }
61
62 # Is the function being exported one which is associated with a
63 # type constraint? If so, which one. If not, then forget the rest
64 # and just use the superclass method.
65 #
66 21117 ▶ if ( my $f = $class->meta->{'functions'}{$name}
Branch: 1 branch
BranchTrueFalseTotal
if (my $f = $class->meta->{"functions"}{$name} and defined $class->meta->{"functions"}{$name}{"type"}) 19928 1189 21117
Condition100%3/3
my $f = $class->meta->{"functions"}{$name} and defined $class->meta->{"functions"}{$name}{"type"}
!ll&&!rl&&r
11048519928
MC/DC100%2/2
my $f = $class->meta->{"functions"}{$name} and defined $class->meta->{"functions"}{$name}{"type"}
my $f = $class->meta->{"functions"}{$name} defined $class->meta->{"functions"}{$name}{"type"}
Truth table100%3/3
A and B
A= my $f = $class->meta->{"functions"}{$name}B= defined $class->meta->{"functions"}{$name}{"type"}
ABresult
0X0
100
111
67 and defined $class->meta->{'functions'}{$name}{'type'} ) {
68
69 19928 my $type = $f->{type};
70 19928 my $tag = $f->{tags}[0];
71 19928 my $typename = $type->name;
72
73 # If $value has `of` or `where` options, then this is a
74 # custom type.
75 #
76 19928 my $custom_type = 0;
77 19928 for my $param ( qw/ of where / ) {
78 39856 ▶ exists $value->{$param} or next;
Branch: 1 branch
BranchTrueFalseTotal
exists $value->{$param} or next 3 39853 39856
79 3 ▶ defined $value->{-as} or _croak( "Parameter '-as' not supplied" );
Branch: 1 branch
BranchTrueFalseTotal
defined $value->{"-as"} or &_croak("Parameter '-as' not supplied") 3 0 3
80 3 $type = $type->$param( $value->{$param} );
81 3 $name = $value->{-as};
82 3 ++$custom_type;
83 }
84
85 # If we're exporting a type itself, then export a custom
86 # function if they customized the type or want a Moose/Mouse
87 # type constraint.
88 #
89 19928 ▶ if ( $tag eq 'types' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($tag eq "types") 10076 9852 19928
90 10076 my $post_method = q();
91 10076 ▶ $post_method = '->mouse_type' if $globals->{mouse};
Branch: 1 branch
BranchTrueFalseTotal
if $globals->{"mouse"} 0 10076 10076
92 10076 ▶ $post_method = '->moose_type' if $globals->{moose};
Branch: 1 branch
BranchTrueFalseTotal
if $globals->{"moose"} 0 10076 10076
93 10076 ▶ return ( $name => type_to_coderef( $type, post_method => $post_method ) )
Branch: 1 branch
BranchTrueFalseTotal
if $post_method or $custom_type 2 10074 10076
Condition66%2/3
$post_method or $custom_type
l!l&&r!l&&!r
0210074
MC/DC50%1/2
$post_method or $custom_type
$post_method $custom_type
Truth table66%2/3
A or B
A= $post_methodB= $custom_type
ABresult
1X1
011
000
94 if $post_method || $custom_type;
95 }
96
97 # If they're exporting some other type of function, like
98 # 'to', 'is', or 'assert', then find the correct exportable
99 # by tag name, and return that.
100 #
101 # XXX: this will fail for tags like 'constants' where there
102 # will be multiple exportables which match!
103 #
104 19926 ▶ if ( $custom_type and $tag ne 'types' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($custom_type and $tag ne "types") 1 19925 19926
Condition66%2/3
$custom_type and $tag ne "types"
!ll&&!rl&&r
1992501
MC/DC50%1/2
$custom_type and $tag ne "types"
$custom_type $tag ne "types"
Truth table66%2/3
A and B
A= $custom_typeB= $tag ne "types"
ABresult
0X0
100
111
105 1 my $exportable = $type->exportables_by_tag( $tag, $typename );
106 1 ▶ return ( $value->{-as} || $exportable->{name}, $exportable->{code} );
Condition33%1/3
$value->{'-as'} || $exportable->{'name'}
l!l&&r!l&&!r
100
MC/DC0%0/2
$value->{'-as'} || $exportable->{'name'}
$value->{'-as'} $exportable->{'name'}
Truth table33%1/3
A || B
A= $value->{'-as'}B= $exportable->{'name'}
ABresult
1X1
011
000
107 }
108 }
109
110 # In all other cases, the superclass method will work.
111 #
112 21114 return $class->SUPER::_exporter_expand_sub( @_ );
113 }
114  
115 # Mostly just rely on superclass to do the actual export, but add
116 # a couple of useful behaviours.
117 #
118 21115 ▶sub _exporter_install_sub {
Subroutine _exporter_install_sub: called CC=11 SCAR=24.0
119 21115 my ( $class, $name, $value, $globals, $sym ) = ( shift, @_ );
120
121 21115 my $into = $globals->{into};
122 21115 my $type = $class->meta->{'functions'}{$name}{'type'};
123 21115 my $tags = $class->meta->{'functions'}{$name}{'tags'};
124
125 # Issue a warning if exporting a deprecated type constraint.
126 #
127 Exporter::Tiny::_carp(
128 "Exporting deprecated type %s to %s",
129 $type->qualified_name,
130 ref( $into ) ? "reference" : "package $into",
131 21115 ▶ ) if ( defined $type and $type->deprecated and not $globals->{allow_deprecated} );
Branch: 2 branches
BranchTrueFalseTotal
if defined $type and $type->deprecated and not $globals->{"allow_deprecated"} 1 21114 21115
ref $into ? : 0 1 1
Condition100%6/6
defined $type and $type->deprecated and not $globals->{"allow_deprecated"}
!ll&&!rl&&r
2111311
defined $type and $type->deprecated
!ll&&!rl&&r
1190199232
MC/DC100%3/3
defined $type and $type->deprecated and not $globals->{"allow_deprecated"}
defined $type $type->deprecated not $globals->{"allow_deprecated"}
Truth table100%4/4
A and B and not(C)
A= defined $typeB= $type->deprecatedC= not $globals->{"allow_deprecated"}
ABCresult
0XX0
10X0
1100
1111
132
133 # If exporting a type constraint into a real package, then
134 # add it to the package's type registry.
135 #
136 21115 ▶ if ( !ref $into
Branch: 1 branch
BranchTrueFalseTotal
if (not ref $into and $into ne "-lexical" and defined $type and grep(($_ eq "types"), @$tags)) 9374 11741 21115
Condition88%8/9
not ref $into and $into ne "-lexical" and defined $type and grep(($_ eq "types"), @$tags)
!ll&&!rl&&r
213096119374
not ref $into and $into ne "-lexical" and defined $type
!ll&&!rl&&r
949118118985
not ref $into and $into ne "-lexical"
!ll&&!rl&&r
949020166
MC/DC75%3/4
not ref $into and $into ne "-lexical" and defined $type and grep(($_ eq "types"), @$tags)
not ref $into $into ne "-lexical" defined $type grep(($_ eq "types"), @$tags)
Truth table80%4/5
not(A) and B and C and D
A= not ref $intoB= $into ne "-lexical"C= defined $typeD= grep(($_ eq "types"), @$tags)
ABCDresult
0XXX0
10XX0
110X0
11100
11111
137 and $into ne '-lexical'
138 and defined $type
139 and grep $_ eq 'types', @$tags ) {
140
141 # If they're renaming it, figure out what name, and use that.
142 # XXX: `-as` can be a coderef, and can be in $globals in that case.
143 9374 my ( $prefix ) = grep defined, $value->{-prefix}, $globals->{prefix}, q();
144 9374 my ( $suffix ) = grep defined, $value->{-suffix}, $globals->{suffix}, q();
145 9374 ▶ my $as = $prefix . ( $value->{-as} || $name ) . $suffix;
Condition66%2/3
$value->{'-as'} || $name
l!l&&r!l&&!r
393710
MC/DC0%0/2
$value->{'-as'} || $name
$value->{'-as'} $name
Truth table66%2/3
A || B
A= $value->{'-as'}B= $name
ABresult
1X1
011
000
146
147 $INC{'Type/Registry.pm'}
148 ? 'Type::Registry'->for_class( $into )->add_type( $type, $as )
149 9374 ▶ : ( $Type::Registry::DELAYED{$into}{$as} = $type );
Branch: 1 branch
BranchTrueFalseTotal
$INC{'Type/Registry.pm'} ? : 368 9006 9374
150 }
151
152 21115 $class->SUPER::_exporter_install_sub( @_ );
153 } #/ sub _exporter_install_sub
154  
155 1041 ▶sub _exporter_fail {
Subroutine _exporter_fail: called CC=3 SCAR=11.3
156 1041 my ( $class, $name, $value, $globals ) = ( shift, @_ );
157
158 # Passing the `-declare` flag means that if a type isn't found, then
159 # we export a placeholder function instead of failing.
160 1041 ▶ if ( $globals->{declare} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($globals->{"declare"}) 1039 2 1041
161 return (
162 $name,
163 type_to_coderef(
164 undef,
165 type_name => $name,
166 1039 ▶ type_library => $globals->{into} || _croak( "Parameter 'into' not supplied" ),
Condition33%1/3
$globals->{'into'} || &_croak(q[Parameter 'into' not supplied])
l!l&&r!l&&!r
103900
MC/DC0%0/2
$globals->{'into'} || &_croak(q[Parameter 'into' not supplied])
$globals->{'into'} &_croak(q[Parameter 'into' not supplied])
Truth table33%1/3
A || B
A= $globals->{'into'}B= &_croak(q[Parameter 'into' not supplied])
ABresult
1X1
011
000
167 ),
168 );
169 } #/ if ( $globals->{declare...})
170
171 2 return $class->SUPER::_exporter_fail( @_ );
172 } #/ sub _exporter_fail
173  
174 ####
175 #### Type library functionality
176 ####
177  
178 443 ▶sub setup_type_library {
Subroutine setup_type_library: called CC=9 SCAR=22.0
Pod documented
179 443 my ( $class, $type_library, $install_utils, $extends, $opts ) = ( shift, @_ );
180
181 443 ▶ my @extends = ref( $extends ) ? @$extends : $extends ? $extends : ();
Branch: 2 branches
BranchTrueFalseTotal
ref $extends ? : 27 416 443
$extends ? : 0 416 416
182 443 ▶ unshift @extends, $class if $class ne __PACKAGE__;
Branch: 1 branch
BranchTrueFalseTotal
if $class ne __PACKAGE__ 1 442 443
183
184 443 ▶ if ( not ref $type_library ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $type_library) 443 0 443
185 278 ▶ no strict "refs";
Subroutine BEGIN: called CC=1 SCAR=0.0
186 443 push @{"$type_library\::ISA"}, $class;
187 443 ( my $file = $type_library ) =~ s{::}{/}g;
188 443 ▶ $INC{"$file.pm"} ||= __FILE__;
Condition100%2/2
$INC{"$file.pm"} ||= __FILE__
l!l
41924
MC/DC100%1/1
$INC{"$file.pm"} ||= __FILE__
$INC{"$file.pm"}
Truth table100%2/2
A
A= $INC{"$file.pm"}
Aresult
11
00
189 }
190
191 443 ▶ if ( $install_utils ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($install_utils) 4 439 443
192 4 require Type::Utils;
193 4 'Type::Utils'->import(
194 { %$opts, into => $type_library },
195 '-default',
196 );
197 }
198
199 443 ▶ if ( @extends and not ref $type_library ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@extends and not ref $type_library) 28 415 443
Condition66%2/3
@extends and not ref $type_library
!ll&&!rl&&r
415028
MC/DC50%1/2
@extends and not ref $type_library
@extends not ref $type_library
Truth table66%2/3
A and not(B)
A= @extendsB= not ref $type_library
ABresult
0X0
100
111
200 28 require Type::Utils;
201 28 my $wrapper = eval "sub { package $type_library; &Type::Utils::extends; }";
202 28 $wrapper->( @extends );
203 }
204 }
205  
206 134198 ▶sub meta {
Subroutine meta: called CC=3 SCAR=11.0
Pod documented
207 278 ▶ no strict "refs";
Subroutine BEGIN: called CC=1 SCAR=0.0
208 278 ▶ no warnings "once";
Subroutine BEGIN: called CC=1 SCAR=0.0
209 134198 ▶ return $_[0] if blessed $_[0];
Branch: 1 branch
BranchTrueFalseTotal
if builtin::blessed($_[0]) 45969 88229 134198
210 88229 ▶ ${"$_[0]\::META"} ||= bless {}, $_[0];
Condition100%2/2
${"$_[0]::META";} ||= bless({}, $_[0])
l!l
87712517
MC/DC100%1/1
${"$_[0]::META";} ||= bless({}, $_[0])
${"$_[0]::META";}
Truth table100%2/2
A
A= ${"$_[0]::META";}
Aresult
11
00
211 }
212  
213 13817 ▶sub add_type {
Subroutine add_type: called CC=15 SCAR=27.1
Pod documented
214 13817 my $meta = shift->meta;
215 13817 my $class = blessed( $meta ) ;
216
217 13817 ▶ _croak( 'Type library is immutable' ) if $meta->{immutable};
Branch: 1 branch
BranchTrueFalseTotal
if $meta->{"immutable"} 4 13813 13817
218
219 my $type =
220 ref( $_[0] ) =~ /^Type::Tiny\b/ ? $_[0] :
221 blessed( $_[0] ) ? Types::TypeTiny::to_TypeTiny( $_[0] ) :
222 13813 ▶ ref( $_[0] ) eq q(HASH) ? 'Type::Tiny'->new( library => $class, %{ $_[0] } ) :
Branch: 3 branches
BranchTrueFalseTotal
ref($_[0]) =~ /^Type::Tiny\b/ ? : 2643 11170 13813
builtin::blessed($_[0]) ? : 0 11170 11170
ref $_[0] eq 'HASH' ? : 10138 1032 11170
223 "Type::Tiny"->new( library => $class, @_ );
224 13813 my $name = $type->{name};
225
226 13813 ▶ if ( $meta->has_type( $name ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($meta->has_type($name)) 1 13812 13813
227 1 my $existing = $meta->get_type( $name );
228 1 ▶ return if $type->{uniq} == $existing->{uniq};
Branch: 1 branch
BranchTrueFalseTotal
if $type->{"uniq"} == $existing->{"uniq"} 0 1 1
229 1 _croak( 'Type %s already exists in this library', $name );
230 }
231
232 13812 ▶ _croak( 'Type %s conflicts with coercion of same name', $name ) if $meta->has_coercion( $name );
Branch: 1 branch
BranchTrueFalseTotal
if $meta->has_coercion($name) 0 13812 13812
233 13812 ▶ _croak( 'Cannot add anonymous type to a library' ) if $type->is_anon;
Branch: 1 branch
BranchTrueFalseTotal
if $type->is_anon 1 13811 13812
234 13811 ▶ $meta->{types} ||= {};
Condition50%1/2
$meta->{"types"} ||= {}
l!l
138110
MC/DC0%0/1
$meta->{"types"} ||= {}
$meta->{"types"}
Truth table50%1/2
A
A= $meta->{"types"}
Aresult
11
00
235 13811 $meta->{types}{$name} = $type;
236
237 278 ▶ no strict "refs";
Subroutine BEGIN: called CC=1 SCAR=0.0
238 278 ▶ no warnings "redefine", "prototype";
Subroutine BEGIN: called CC=1 SCAR=0.0
239
240 13811 for my $exportable ( @{ $type->exportables } ) {
241 55253 my $name = $exportable->{name};
242 55253 my $code = $exportable->{code};
243 55253 my $tags = $exportable->{tags};
244 _croak( 'Function %s is provided by types %s and %s', $name, $meta->{'functions'}{$name}{'type'}->name, $type->name )
245 55253 ▶ if $meta->{'functions'}{$name};
Branch: 1 branch
BranchTrueFalseTotal
if $meta->{"functions"}{$name} 1 55252 55253
246 55252 *{"$class\::$name"} = set_subname( "$class\::$name", $code );
247 55252 push @{"$class\::EXPORT_OK"}, $name;
248 55252 ▶ push @{ ${"$class\::EXPORT_TAGS"}{$_} ||= [] }, $name for @$tags;
Condition100%2/2
do { "${class}::EXPORT_TAGS" }->{$_} ||= []
l!l
534901762
MC/DC100%1/1
do { "${class}::EXPORT_TAGS" }->{$_} ||= []
do { "${class}::EXPORT_TAGS" }->{$_}
Truth table100%2/2
A
A= do { "${class}::EXPORT_TAGS" }->{$_}
Aresult
11
00
249 55252 $meta->{'functions'}{$name} = { type => $type, tags => $tags };
250 }
251
252 $INC{'Type/Registry.pm'}
253 ? 'Type::Registry'->for_class( $class )->add_type( $type, $name )
254 13810 ▶ : ( $Type::Registry::DELAYED{$class}{$name} = $type );
Branch: 1 branch
BranchTrueFalseTotal
$INC{'Type/Registry.pm'} ? : 148 13662 13810
255
256 13810 return $type;
257 } #/ sub add_type
258  
259 # For Type::TinyX::Facets
260 # Only use this if you know what you're doing!
261 0 ▶sub _remove_type {
Subroutine _remove_type: not called CC=6 SCAR=33.1
262 0 my $meta = shift->meta;
263 0 my $type = $meta->get_type( $_[0] );
264 0 my $class = ref $meta;
265
266 0 ▶ _croak( 'Type library is immutable' ) if $meta->{immutable};
Branch: 1 branch
BranchTrueFalseTotal
if $meta->{"immutable"} 0 0 0
267
268 0 delete $meta->{types}{$type->name};
269
270 278 ▶ no strict "refs";
Subroutine BEGIN: called CC=1 SCAR=0.0
271 278 ▶ no warnings "redefine", "prototype";
Subroutine BEGIN: called CC=1 SCAR=0.0
272
273 0 my @clean;
274 0 ▶ my $_scrub = sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
275 0 my ( $arr, $name ) = @_;
276 0 @$arr = grep $_ ne $name, @$arr;
277 0 };
278 0 for my $exportable ( @{ $type->exportables } ) {
279 0 my $name = $exportable->{name};
280 0 push @clean, $name;
281 0 &$_scrub( \@{"$class\::EXPORT_OK"}, $name );
282 0 for my $t ( @{ $exportable->{tags} } ) {
283 0 ▶ &$_scrub( ${"$class\::EXPORT_TAGS"}{$t} ||= [], $name );
Condition0%0/2
do { "${class}::EXPORT_TAGS" }->{$t} ||= []
l!l
00
MC/DC0%0/1
do { "${class}::EXPORT_TAGS" }->{$t} ||= []
do { "${class}::EXPORT_TAGS" }->{$t}
Truth table0%0/2
A
A= do { "${class}::EXPORT_TAGS" }->{$t}
Aresult
11
00
284 }
285 0 delete $meta->{'functions'}{$name};
286 }
287 0 eval {
288 0 require namespace::clean;
289 0 'namespace::clean'->clean_subroutines( $class, @clean );
290 };
291
292 delete 'Type::Registry'->for_class( $class )->{$type->name}
293 0 ▶ if $INC{'Type/Registry.pm'};
Branch: 1 branch
BranchTrueFalseTotal
if $INC{"Type/Registry.pm"} 0 0 0
294 0 delete $Type::Registry::DELAYED{$class}{$type->name};
295
296 0 return $type;
297 } #/ sub _remove_type
298  
299 4202 ▶sub get_type {
Subroutine get_type: called CC=1 SCAR=0.0
Pod documented
300 4202 my $meta = shift->meta;
301 4202 $meta->{types}{ $_[0] };
302 }
303  
304 14889 ▶sub has_type {
Subroutine has_type: called CC=1 SCAR=0.0
Pod documented
305 14889 my $meta = shift->meta;
306 14889 exists $meta->{types}{ $_[0] };
307 }
308  
309 114 ▶sub type_names {
Subroutine type_names: called CC=1 SCAR=0.0
Pod documented
310 114 my $meta = shift->meta;
311 114 keys %{ $meta->{types} };
312 }
313  
314 989 ▶sub add_coercion {
Subroutine add_coercion: called CC=8 SCAR=20.9
Pod documented
315 989 my $meta = shift->meta;
316 989 my $class = blessed( $meta );
317
318 989 ▶ _croak( 'Type library is immutable' ) if $meta->{immutable};
Branch: 1 branch
BranchTrueFalseTotal
if $meta->{"immutable"} 0 989 989
319
320 989 require Type::Coercion;
321 989 ▶ my $c = blessed( $_[0] ) ? $_[0] : "Type::Coercion"->new( @_ );
Branch: 1 branch
BranchTrueFalseTotal
builtin::blessed($_[0]) ? : 166 823 989
322 989 my $name = $c->name;
323
324 989 ▶ _croak( 'Coercion %s already exists in this library', $name ) if $meta->has_coercion( $name );
Branch: 1 branch
BranchTrueFalseTotal
if $meta->has_coercion($name) 0 989 989
325 989 ▶ _croak( 'Coercion %s conflicts with type of same name', $name ) if $meta->has_type( $name );
Branch: 1 branch
BranchTrueFalseTotal
if $meta->has_type($name) 1 988 989
326 988 ▶ _croak( 'Cannot add anonymous type to a library' ) if $c->is_anon;
Branch: 1 branch
BranchTrueFalseTotal
if $c->is_anon 0 988 988
327
328 988 ▶ $meta->{coercions} ||= {};
Condition50%1/2
$meta->{"coercions"} ||= {}
l!l
9880
MC/DC0%0/1
$meta->{"coercions"} ||= {}
$meta->{"coercions"}
Truth table50%1/2
A
A= $meta->{"coercions"}
Aresult
11
00
329 988 $meta->{coercions}{$name} = $c;
330
331 278 ▶ no strict "refs";
Subroutine BEGIN: called CC=1 SCAR=0.0
332 278 ▶ no warnings "redefine", "prototype";
Subroutine BEGIN: called CC=1 SCAR=0.0
333
334 988 *{"$class\::$name"} = type_to_coderef( $c );
335 988 push @{"$class\::EXPORT_OK"}, $name;
336 988 ▶ push @{ ${"$class\::EXPORT_TAGS"}{'coercions'} ||= [] }, $name;
Condition100%2/2
do { "${class}::EXPORT_TAGS" }->{"coercions"} ||= []
l!l
668320
MC/DC100%1/1
do { "${class}::EXPORT_TAGS" }->{"coercions"} ||= []
do { "${class}::EXPORT_TAGS" }->{"coercions"}
Truth table100%2/2
A
A= do { "${class}::EXPORT_TAGS" }->{"coercions"}
Aresult
11
00
337 988 $meta->{'functions'}{$name} = { coercion => $c, tags => [ 'coercions' ] };
338  
339 988 return $c;
340 } #/ sub add_coercion
341  
342 135 ▶sub get_coercion {
Subroutine get_coercion: called CC=1 SCAR=0.0
Pod documented
343 135 my $meta = shift->meta;
344 135 $meta->{coercions}{ $_[0] };
345 }
346  
347 14801 ▶sub has_coercion {
Subroutine has_coercion: called CC=1 SCAR=0.0
Pod documented
348 14801 my $meta = shift->meta;
349 14801 exists $meta->{coercions}{ $_[0] };
350 }
351  
352 114 ▶sub coercion_names {
Subroutine coercion_names: called CC=1 SCAR=0.0
Pod documented
353 114 my $meta = shift->meta;
354 114 keys %{ $meta->{coercions} };
355 }
356  
357 390 ▶sub make_immutable {
Subroutine make_immutable: called CC=5 SCAR=16.1
Pod documented
358 390 my $meta = shift->meta;
359 390 my $class = ref( $meta );
360
361 278 ▶ no strict "refs";
Subroutine BEGIN: called CC=1 SCAR=0.0
362 278 ▶ no warnings "redefine", "prototype";
Subroutine BEGIN: called CC=1 SCAR=0.0
363
364 390 for my $type ( values %{ $meta->{types} } ) {
365 12875 $type->coercion->freeze;
366 12875 ▶ next unless $type->has_coercion && $type->coercion->frozen;
Branch: 1 branch
BranchTrueFalseTotal
unless $type->has_coercion and $type->coercion->frozen 12140 735 12875
Condition66%2/3
$type->has_coercion and $type->coercion->frozen
!ll&&!rl&&r
121400735
MC/DC50%1/2
$type->has_coercion and $type->coercion->frozen
$type->has_coercion $type->coercion->frozen
Truth table66%2/3
A and B
A= $type->has_coercionB= $type->coercion->frozen
ABresult
0X0
100
111
367 735 for my $e ( $type->exportables_by_tag( 'to' ) ) {
368 735 my $qualified_name = $class . '::' . $e->{name};
369 735 *$qualified_name = set_subname( $qualified_name, $e->{code} );
370 }
371 }
372
373 390 $meta->{immutable} = 1;
374 }
375  
376 278 1;
377  
378 __END__
« lib/Types/Standard/ArrayRef.pm ↑ Summary blib/lib/Type/Params/Signature.pm »