1 package ## Hide from PAUSE
2 MooseX::Meta::TypeCoercion::Parameterizable;
5 extends 'Moose::Meta::TypeCoercion';
9 MooseX::Meta::TypeCoercion::Parameterizable - Coerce Parameterizable type constraints.
13 Coercion Meta Class, intended to make sure coercions work correctly with
14 parameterized types. You probably won't consume or subclass this class directly
18 This class defines the following methods.
20 =head add_type_coercions
22 method modification to throw exception should we try to add a coercion on a
23 parameterizable type that is already defined by a constraining value. We do
24 this since defined parameterizable type constraints inherit their coercion from
25 the parent constraint. It makes no sense to even be using parameterizable
26 types if you know the constraining value beforehand!
30 around 'add_type_coercions' => sub {
31 my ($add_type_coercions, $self, @args) = @_;
32 if($self->type_constraint->has_constraining_value) {
33 Moose->throw_error("Cannot add type coercions to a parameterizable type constraint that's been defined.");
35 return $self->$add_type_coercions(@args);
40 ## These two are here until I can merge change upstream to Moose. These are two
41 ## very minor changes we can probably just put into Moose without breaking stuff.
42 ## Hopefully can can eventually stop doing this.
46 my $coderef = $self->_compiled_type_coercion;
47 return $coderef->(@_); ## <== in Moose we don't call on @_, but $_[1]
50 sub compile_type_coercion {
52 my @coercion_map = @{$self->type_coercion_map};
54 while (@coercion_map) {
55 my ($constraint_name, $action) = splice(@coercion_map, 0, 2);
57 my $type_constraint = ref $constraint_name
59 : Moose::Util::TypeConstraints::find_or_parse_type_constraint($constraint_name);
61 unless ( defined $type_constraint ) {
63 Moose->throw_error("Could not find the type constraint ($constraint_name) to coerce from");
67 $type_constraint->_compiled_type_constraint,
71 $self->_compiled_type_coercion(sub {
73 foreach my $coercion (@coercions) {
74 my ($constraint, $converter) = @$coercion;
75 if ($constraint->($thing)) {
77 return $converter->($thing, @_); ## <== Here also we pass @_ which Moose doesn't
86 The following modules or resources may be of interest.
88 L<Moose>, L<Moose::Meta::TypeCoercion>
92 John Napiorkowski, C<< <jjnapiork@cpan.org> >>
94 =head1 COPYRIGHT & LICENSE
96 This program is free software; you can redistribute it and/or modify
97 it under the same terms as Perl itself.
101 __PACKAGE__->meta->make_immutable(inline_constructor => 0);