my $pkg = shift || caller();
return if $pkg eq ':no_export';
no strict 'refs';
- foreach my $export (qw(type subtype as where to coerce)) {
+ foreach my $export (qw(type subtype as where coerce from via)) {
*{"${pkg}::${export}"} = \&{"${export}"};
}
}
sub register_type_constraint {
my ($type_name, $type_constraint) = @_;
+ (not exists $TYPES{$type_name})
+ || confess "The type constraint '$type_name' has already been registered";
$TYPES{$type_name} = $type_constraint;
}
sub register_type_coercion {
my ($type_name, $type_coercion) = @_;
+ (not exists $COERCIONS{$type_name})
+ || confess "The type coercion for '$type_name' has already been registered";
$COERCIONS{$type_name} = $type_coercion;
}
}
foreach my $coercion (@coercions) {
my ($constraint, $converter) = @$coercion;
if (defined $constraint->($thing)) {
+ local $_ = $thing;
return $converter->($thing);
}
}
}
sub as ($) { $_[0] }
+sub from ($) { $_[0] }
sub where (&) { $_[0] }
-sub to (&) { $_[0] }
+sub via (&) { $_[0] }
# define some basic types
=> where { $_ < 10 };
coerce Num
- => as Str
- => to { 0+$_ };
+ => from Str
+ => via { 0+$_ };
=head1 DESCRIPTION
=item B<coerce>
-=item B<to>
+=item B<from>
+
+=item B<via>
=back