/usr/share/perl5/Type/Tiny/Class.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 351 352 353 354 355 356 357 358 359 360 | package Type::Tiny::Class;
use 5.006001;
use strict;
use warnings;
BEGIN {
if ($] < 5.008) { require Devel::TypeTiny::Perl56Compat };
}
BEGIN {
$Type::Tiny::Class::AUTHORITY = 'cpan:TOBYINK';
$Type::Tiny::Class::VERSION = '1.000005';
}
use Scalar::Util qw< blessed >;
sub _croak ($;@) { require Error::TypeTiny; goto \&Error::TypeTiny::croak }
use Type::Tiny ();
our @ISA = 'Type::Tiny';
sub new {
my $proto = shift;
return $proto->class->new(@_) if blessed $proto; # DWIM
my %opts = (@_==1) ? %{$_[0]} : @_;
_croak "Class type constraints cannot have a parent constraint passed to the constructor" if exists $opts{parent};
_croak "Class type constraints cannot have a constraint coderef passed to the constructor" if exists $opts{constraint};
_croak "Class type constraints cannot have a inlining coderef passed to the constructor" if exists $opts{inlined};
_croak "Need to supply class name" unless exists $opts{class};
if (Type::Tiny::_USE_XS)
{
my $xsub = Type::Tiny::XS::get_coderef_for("InstanceOf[".$opts{class}."]");
$opts{compiled_type_constraint} = $xsub if $xsub;
}
elsif (Type::Tiny::_USE_MOUSE)
{
require Mouse::Util::TypeConstraints;
my $maker = "Mouse::Util::TypeConstraints"->can("generate_isa_predicate_for");
$opts{compiled_type_constraint} = $maker->($opts{class}) if $maker;
}
return $proto->SUPER::new(%opts);
}
sub class { $_[0]{class} }
sub inlined { $_[0]{inlined} ||= $_[0]->_build_inlined }
sub has_inlined { !!1 }
sub _build_constraint
{
my $self = shift;
my $class = $self->class;
return sub { blessed($_) and $_->isa($class) };
}
sub _build_inlined
{
my $self = shift;
my $class = $self->class;
if (Type::Tiny::_USE_XS)
{
my $xsub = Type::Tiny::XS::get_subname_for("InstanceOf[$class]");
return sub { my $var = $_[1]; "$xsub\($var\)" } if $xsub;
}
sub {
my $var = $_[1];
qq{Scalar::Util::blessed($var) and $var->isa(q[$class])};
};
}
sub _build_default_message
{
no warnings 'uninitialized';
my $self = shift;
my $c = $self->class;
return sub { sprintf '%s did not pass type constraint (not isa %s)', Type::Tiny::_dd($_[0]), $c } if $self->is_anon;
my $name = "$self";
return sub { sprintf '%s did not pass type constraint "%s" (not isa %s)', Type::Tiny::_dd($_[0]), $name, $c };
}
sub _instantiate_moose_type
{
my $self = shift;
my %opts = @_;
delete $opts{parent};
delete $opts{constraint};
delete $opts{inlined};
require Moose::Meta::TypeConstraint::Class;
return "Moose::Meta::TypeConstraint::Class"->new(%opts, class => $self->class);
}
sub plus_constructors
{
my $self = shift;
unless (@_)
{
require Types::Standard;
push @_, Types::Standard::HashRef(), "new";
}
require B;
require Types::TypeTiny;
my $class = B::perlstring($self->class);
my @r;
while (@_)
{
my $source = shift;
Types::TypeTiny::TypeTiny->check($source)
or _croak "Expected type constraint; got $source";
my $constructor = shift;
Types::TypeTiny::StringLike->check($constructor)
or _croak "Expected string; got $constructor";
push @r, $source, sprintf('%s->%s($_)', $class, $constructor);
}
return $self->plus_coercions(\@r);
}
sub has_parent
{
!!1;
}
sub parent
{
$_[0]{parent} ||= $_[0]->_build_parent;
}
sub _build_parent
{
my $self = shift;
my $class = $self->class;
# Some classes (I'm looking at you, Math::BigFloat) include a class in
# their @ISA to inherit methods, but then override isa() to return false,
# so that they don't appear to be a subclass.
#
# In these cases, we don't want to list the parent class as a parent
# type constraint.
#
my @isa = grep $class->isa($_), do { no strict "refs"; no warnings; @{"$class\::ISA"} };
if (@isa == 0)
{
require Types::Standard;
return Types::Standard::Object();
}
if (@isa == 1)
{
return ref($self)->new(class => $isa[0])
}
require Type::Tiny::Intersection;
"Type::Tiny::Intersection"->new(
type_constraints => [ map ref($self)->new(class => $_), @isa ],
);
}
*__get_linear_isa_dfs = eval { require mro }
? \&mro::get_linear_isa
: sub {
no strict 'refs';
my $classname = shift;
my @lin = ($classname);
my %stored;
foreach my $parent (@{"$classname\::ISA"})
{
my $plin = __get_linear_isa_dfs($parent);
foreach (@$plin) {
next if exists $stored{$_};
push(@lin, $_);
$stored{$_} = 1;
}
}
return \@lin;
};
sub validate_explain
{
my $self = shift;
my ($value, $varname) = @_;
$varname = '$_' unless defined $varname;
return undef if $self->check($value);
return ["Not a blessed reference"] unless blessed($value);
my @isa = @{ __get_linear_isa_dfs(ref $value) };
my $display_var = $varname eq q{$_} ? '' : sprintf(' (in %s)', $varname);
require Type::Utils;
return [
sprintf('"%s" requires that the reference isa %s', $self, $self->class),
sprintf('The reference%s isa %s', $display_var, Type::Utils::english_list(@isa)),
];
}
1;
__END__
=pod
=encoding utf-8
=head1 NAME
Type::Tiny::Class - type constraints based on the "isa" method
=head1 STATUS
This module is covered by the
L<Type-Tiny stability policy|Type::Tiny::Manual::Policies/"STABILITY">.
=head1 DESCRIPTION
Type constraints of the general form C<< { $_->isa("Some::Class") } >>.
This package inherits from L<Type::Tiny>; see that for most documentation.
Major differences are listed below:
=head2 Constructor
=over
=item C<new>
When the constructor is called on an I<instance> of Type::Tiny::Class, it
passes the call through to the constructor of the class for the constraint.
So for example:
my $type = Type::Tiny::Class->new(class => "Foo::Bar");
my $obj = $type->new(hello => "World");
say ref($obj); # prints "Foo::Bar"
This little bit of DWIM was borrowed from L<MooseX::Types::TypeDecorator>,
but Type::Tiny doesn't take the idea quite as far.
=back
=head2 Attributes
=over
=item C<class>
The class for the constraint.
=item C<constraint>
Unlike Type::Tiny, you I<cannot> pass a constraint coderef to the constructor.
Instead rely on the default.
=item C<inlined>
Unlike Type::Tiny, you I<cannot> pass an inlining coderef to the constructor.
Instead rely on the default.
=item C<parent>
Parent is automatically calculated, and cannot be passed to the constructor.
=back
=head2 Methods
=over
=item C<< plus_constructors($source, $method_name) >>
Much like C<plus_coercions> but adds coercions that go via a constructor.
(In fact, this is implemented as a wrapper for C<plus_coercions>.)
Example:
package MyApp::Minion;
use Moose; extends "MyApp::Person";
use Types::Standard qw( HashRef Str );
use Type::Utils qw( class_type );
my $Person = class_type({ class => "MyApp::Person" });
has boss => (
is => "ro",
isa => $Person->plus_constructors(
HashRef, "new",
Str, "_new_from_name",
),
coerce => 1,
);
package main;
MyApp::Minion->new(
...,
boss => "Bob", ## via MyApp::Person->_new_from_name
);
MyApp::Minion->new(
...,
boss => { name => "Bob" }, ## via MyApp::Person->new
);
Because coercing C<HashRef> via constructor is a common desire, if
you call C<plus_constructors> with no arguments at all, this is the
default.
$classtype->plus_constructors(Types::Standard::HashRef, "new")
$classtype->plus_constructors() ## identical to above
This is handy for Moose/Mouse/Moo-based classes.
=back
=head1 BUGS
Please report any bugs to
L<http://rt.cpan.org/Dist/Display.html?Queue=Type-Tiny>.
=head1 SEE ALSO
L<Type::Tiny::Manual>.
L<Type::Tiny>.
L<Moose::Meta::TypeConstraint::Class>.
=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.
|