/usr/share/perl5/Types/Standard/Dict.pm is in libtype-tiny-perl 1.000005-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 | package Types::Standard::Dict;
use 5.006001;
use strict;
use warnings;
BEGIN {
$Types::Standard::Dict::AUTHORITY = 'cpan:TOBYINK';
$Types::Standard::Dict::VERSION = '1.000005';
}
use Types::Standard ();
use Types::TypeTiny ();
sub _croak ($;@) { require Error::TypeTiny; goto \&Error::TypeTiny::croak }
my $_optional = Types::Standard::Optional;
my $_hash = Types::Standard::HashRef;
my $_map = Types::Standard::Map;
my $_any = Types::Standard::Any;
no warnings;
sub __constraint_generator
{
my $slurpy = ref($_[-1]) eq q(HASH) ? pop(@_)->{slurpy} : undef;
my %constraints = @_;
my %is_optional;
while (my ($k, $v) = each %constraints)
{
$constraints{$k} = Types::TypeTiny::to_TypeTiny($v);
$is_optional{$k} = !!$constraints{$k}->is_strictly_a_type_of($_optional);
Types::TypeTiny::TypeTiny->check($v)
or _croak("Parameter to Dict[`a] for key '$k' expected to be a type constraint; got $v");
}
return sub
{
my $value = $_[0];
if ($slurpy)
{
my %tmp = map {
exists($constraints{$_}) ? () : ($_ => $value->{$_})
} keys %$value;
return unless $slurpy->check(\%tmp);
}
else
{
exists($constraints{$_}) || return for sort keys %$value;
}
for my $k (sort keys %constraints) {
exists($value->{$k}) or ($is_optional{$k} ? next : return);
$constraints{$k}->check($value->{$k}) or return;
}
return !!1;
};
}
sub __inline_generator
{
# We can only inline a parameterized Dict if all the
# constraints inside can be inlined.
my $slurpy = ref($_[-1]) eq q(HASH) ? pop(@_)->{slurpy} : undef;
return if $slurpy && !$slurpy->can_be_inlined;
# Is slurpy a very loose type constraint?
# i.e. Any, Item, Defined, Ref, or HashRef
my $slurpy_is_any = $slurpy && $_hash->is_a_type_of( $slurpy );
# Is slurpy a parameterized Map, or expressable as a parameterized Map?
my $slurpy_is_map = $slurpy
&& $slurpy->is_parameterized
&& ((
$slurpy->parent->strictly_equals($_map)
&& $slurpy->parameters
)||(
$slurpy->parent->strictly_equals($_hash)
&& [ $_any, $slurpy->parameters->[0] ]
));
my %constraints = @_;
for my $c (values %constraints)
{
next if $c->can_be_inlined;
return;
}
my $regexp = join "|", map quotemeta, sort keys %constraints;
return sub
{
require B;
my $h = $_[1];
join " and ",
"ref($h) eq 'HASH'",
( $slurpy_is_any ? ()
: $slurpy_is_map ? do {
'(not grep {'
."my \$v = ($h)->{\$_};"
.sprintf(
'not((/\\A(?:%s)\\z/) or ((%s) and (%s)))',
$regexp,
$slurpy_is_map->[0]->inline_check('$_'),
$slurpy_is_map->[1]->inline_check('$v'),
) ."} keys \%{$h})"
}
: $slurpy ? do {
'do {'
. "my \$slurpy_tmp = +{ map /\\A(?:$regexp)\\z/ ? () : (\$_ => ($h)->{\$_}), keys \%{$h} };"
. $slurpy->inline_check('$slurpy_tmp')
. '}'
}
: "not(grep !/\\A(?:$regexp)\\z/, keys \%{$h})" ),
( map {
my $k = B::perlstring($_);
$constraints{$_}->is_strictly_a_type_of( $_optional )
? sprintf('(!exists %s->{%s} or %s)', $h, $k, $constraints{$_}->inline_check("$h\->{$k}"))
: ( "exists($h\->{$k})", $constraints{$_}->inline_check("$h\->{$k}") )
} sort keys %constraints ),
}
}
sub __deep_explanation
{
require B;
my ($type, $value, $varname) = @_;
my @params = @{ $type->parameters };
my $slurpy = ref($params[-1]) eq q(HASH) ? pop(@params)->{slurpy} : undef;
my %constraints = @params;
for my $k (sort keys %constraints)
{
next if $constraints{$k}->parent == Types::Standard::Optional && !exists $value->{$k};
next if $constraints{$k}->check($value->{$k});
return [
sprintf('"%s" requires key %s to appear in hash', $type, B::perlstring($k))
] unless exists $value->{$k};
return [
sprintf('"%s" constrains value at key %s of hash with "%s"', $type, B::perlstring($k), $constraints{$k}),
@{ $constraints{$k}->validate_explain($value->{$k}, sprintf('%s->{%s}', $varname, B::perlstring($k))) },
];
}
if ($slurpy)
{
my %tmp = map {
exists($constraints{$_}) ? () : ($_ => $value->{$_})
} keys %$value;
my $explain = $slurpy->validate_explain(\%tmp, '$slurpy');
return [
sprintf('"%s" requires the hashref of additional key/value pairs to conform to "%s"', $type, $slurpy),
@$explain,
] if $explain;
}
else
{
for my $k (sort keys %$value)
{
return [
sprintf('"%s" does not allow key %s to appear in hash', $type, B::perlstring($k))
] unless exists $constraints{$k};
}
}
# This should never happen...
return; # uncoverable statement
}
my $label_counter = 0;
our ($keycheck_counter, @KEYCHECK) = -1;
sub __coercion_generator
{
my $slurpy = ref($_[-1]) eq q(HASH) ? pop(@_)->{slurpy} : undef;
my ($parent, $child, %dict) = @_;
my $C = "Type::Coercion"->new(type_constraint => $child);
my $all_inlinable = 1;
for my $tc (values %dict)
{
$all_inlinable = 0 if !$tc->can_be_inlined;
$all_inlinable = 0 if $tc->has_coercion && !$tc->coercion->can_be_inlined;
last if!$all_inlinable;
}
$all_inlinable = 0 if $slurpy && !$slurpy->can_be_inlined;
$all_inlinable = 0 if $slurpy && $slurpy->has_coercion && !$slurpy->coercion->can_be_inlined;
if ($all_inlinable)
{
$C->add_type_coercions($parent => Types::Standard::Stringable {
require B;
my $keycheck = join "|", map quotemeta, sort { length($b) <=> length($a) or $a cmp $b } keys %dict;
$keycheck = $KEYCHECK[++$keycheck_counter] = qr{^($keycheck)$}ms; # regexp for legal keys
my $label = sprintf("DICTLABEL%d", ++$label_counter);
my @code;
push @code, 'do { my ($orig, $return_orig, $tmp, %new) = ($_, 0);';
push @code, "$label: {";
if ($slurpy)
{
push @code, sprintf('my $slurped = +{ map +($_=~$%s::KEYCHECK[%d])?():($_=>$orig->{$_}), keys %%$orig };', __PACKAGE__, $keycheck_counter);
if ($slurpy->has_coercion)
{
push @code, sprintf('my $coerced = %s;', $slurpy->coercion->inline_coercion('$slurped'));
push @code, sprintf('((%s)&&(%s))?(%%new=%%$coerced):(($return_orig = 1), last %s);', $_hash->inline_check('$coerced'), $slurpy->inline_check('$coerced'), $label);
}
else
{
push @code, sprintf('(%s)?(%%new=%%$slurped):(($return_orig = 1), last %s);', $slurpy->inline_check('$slurped'), $label);
}
}
else
{
push @code, sprintf('($_ =~ $%s::KEYCHECK[%d])||(($return_orig = 1), last %s) for sort keys %%$orig;', __PACKAGE__, $keycheck_counter, $label);
}
for my $k (keys %dict)
{
my $ct = $dict{$k};
my $ct_coerce = $ct->has_coercion;
my $ct_optional = $ct->is_a_type_of($_optional);
my $K = B::perlstring($k);
push @code, sprintf(
'if (exists $orig->{%s}) { $tmp = %s; (%s) ? ($new{%s}=$tmp) : ($return_orig=1 and last %s) }',
$K,
$ct_coerce
? $ct->coercion->inline_coercion("\$orig->{$K}")
: "\$orig->{$K}",
$ct->inline_check('$tmp'),
$K,
$label,
);
}
push @code, '}';
push @code, '$return_orig ? $orig : \\%new';
push @code, '}';
#warn "CODE:: @code";
"@code";
});
}
else
{
my %is_optional = map {
; $_ => !!$dict{$_}->is_strictly_a_type_of($_optional)
} sort keys %dict;
$C->add_type_coercions(
$parent => sub {
my $value = @_ ? $_[0] : $_;
my %new;
if ($slurpy)
{
my %slurped = map exists($dict{$_}) ? () : ($_ => $value->{$_}), keys %$value;
if ($slurpy->check(\%slurped))
{
%new = %slurped;
}
elsif ($slurpy->has_coercion)
{
my $coerced = $slurpy->coerce(\%slurped);
$slurpy->check($coerced) ? (%new = %$coerced) : (return $value);
}
else
{
return $value;
}
}
else
{
for my $k (keys %$value)
{
return $value unless exists $dict{$k};
}
}
for my $k (keys %dict)
{
next if $is_optional{$k} and not exists $value->{$k};
my $ct = $dict{$k};
my $x = $ct->has_coercion ? $ct->coerce($value->{$k}) : $value->{$k};
return $value unless $ct->check($x);
$new{$k} = $x;
}
return \%new;
},
);
}
return $C;
}
1;
__END__
=pod
=encoding utf-8
=head1 NAME
Types::Standard::Dict - internals for the Types::Standard Dict type constraint
=head1 STATUS
This module is considered part of Type-Tiny's internals. It is not
covered by the
L<Type-Tiny stability policy|Type::Tiny::Manual::Policies/"STABILITY">.
=head1 DESCRIPTION
This file contains some of the guts for L<Types::Standard>.
It will be loaded on demand. You may ignore its presence.
=head1 BUGS
Please report any bugs to
L<http://rt.cpan.org/Dist/Display.html?Queue=Type-Tiny>.
=head1 SEE ALSO
L<Types::Standard>.
=head1 AUTHOR
Toby Inkster E<lt>tobyink@cpan.orgE<gt>.
=head1 COPYRIGHT AND LICENCE
This software is copyright (c) 2013-2014 by Toby Inkster.
This is free software; you can redistribute it and/or modify it under
the same terms as the Perl 5 programming language system itself.
=head1 DISCLAIMER OF WARRANTIES
THIS PACKAGE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED
WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
|