use Carp ();
use Scalar::Util qw/blessed looks_like_number openhandle/;
+
+use Mouse::Util;
use Mouse::Meta::TypeConstraint;
our @EXPORT = qw(
my %COERCE_KEYS;
sub as ($) {
- as => $_[0]
+ return(as => $_[0]);
}
sub where (&) {
- where => $_[0]
+ return(where => $_[0])
}
sub message (&) {
- message => $_[0]
+ return(message => $_[0])
}
-sub from { @_ }
-sub via (&) {
- $_[0]
-}
+sub from { @_ }
+sub via (&) { $_[0] }
BEGIN {
no warnings 'uninitialized';
%TYPE = (
Any => sub { 1 },
Item => sub { 1 },
- Bool => sub {
- !defined($_[0]) || $_[0] eq "" || "$_[0]" eq '1' || "$_[0]" eq '0'
- },
+
+ Bool => sub { $_[0] ? $_[0] eq '1' : 1 },
Undef => sub { !defined($_[0]) },
Defined => sub { defined($_[0]) },
Value => sub { defined($_[0]) && !ref($_[0]) },
Num => sub { !ref($_[0]) && looks_like_number($_[0]) },
Int => sub { defined($_[0]) && !ref($_[0]) && $_[0] =~ /^-?[0-9]+$/ },
Str => sub { defined($_[0]) && !ref($_[0]) },
- ClassName => sub { Mouse::is_class_loaded($_[0]) },
Ref => sub { ref($_[0]) },
ScalarRef => sub { ref($_[0]) eq 'SCALAR' },
},
Object => sub { blessed($_[0]) && blessed($_[0]) ne 'Regexp' },
+
+ ClassName => sub { Mouse::Util::is_class_loaded($_[0]) },
+ RoleName => sub { (Mouse::Util::class_of($_[0]) || return 0)->isa('Mouse::Meta::Role') },
);
while (my ($name, $code) = each %TYPE) {
- $TYPE{$name} = Mouse::Meta::TypeConstraint->new( _compiled_type_constraint => $code, name => $name );
+ $TYPE{$name} = Mouse::Meta::TypeConstraint->new(
+ name => $name,
+ _compiled_type_constraint => $code,
+ );
}
sub optimized_constraints { \%TYPE }
}
sub type {
- my $pkg = caller(0);
- my($name, %conf) = @_;
+ my $name;
+ my %conf;
+
+ if(@_ == 1 && ref $_[0]){ # type { where => ... }
+ %conf = %{$_[0]};
+ }
+ elsif(@_ == 2 && ref $_[1]){ # type $name => { where => ... }*
+ $name = $_[0];
+ %conf = %{$_[1]};
+ }
+ elsif(@_ % 2){ # odd number of arguments
+ $name = shift;
+ %conf = @_;
+ }
+ else{
+ %conf = @_;
+ }
+
+ $name = '__ANON__' if !defined $name;
+
+ my $pkg = caller;
+
if ($TYPE{$name} && $TYPE_SOURCE{$name} ne $pkg) {
Carp::croak "The type constraint '$name' has already been created in $TYPE_SOURCE{$name} and cannot be created again in $pkg";
- };
+ }
my $constraint = $conf{where} || do {
my $as = delete $conf{as} || 'Any';
if (! exists $TYPE{$as}) {
}
sub subtype {
- my $pkg = caller(0);
- my($name, %conf) = @_;
+ my $name;
+ my %conf;
+
+ if(@_ == 1 && ref $_[0]){ # type { where => ... }
+ %conf = %{$_[0]};
+ }
+ elsif(@_ == 2 && ref $_[1]){ # type $name => { where => ... }*
+ $name = $_[0];
+ %conf = %{$_[1]};
+ }
+ elsif(@_ % 2){ # odd number of arguments
+ $name = shift;
+ %conf = @_;
+ }
+ else{
+ %conf = @_;
+ }
+
+ $name = '__ANON__' if !defined $name;
+
+ my $pkg = caller;
+
+
if ($TYPE{$name} && $TYPE_SOURCE{$name} ne $pkg) {
Carp::croak "The type constraint '$name' has already been created in $TYPE_SOURCE{$name} and cannot be created again in $pkg";
- };
+ }
my $constraint = delete $conf{where};
my $as_constraint = find_or_create_isa_type_constraint(delete $conf{as} || 'Any');
}
sub coerce {
- my($name, %conf) = @_;
+ my $name = shift;
Carp::croak "Cannot find type '$name', perhaps you forgot to load it."
unless $TYPE{$name};
$COERCE{$name} = {};
$COERCE_KEYS{$name} = [];
}
- while (my($type, $code) = each %conf) {
+
+ while (my($type, $code) = splice @_, 0, 2) {
Carp::croak "A coercion action already exists for '$type'"
if $COERCE{$name}->{$type};
}
}
- unshift @{ $COERCE_KEYS{$name} }, $type;
+ push @{ $COERCE_KEYS{$name} }, $type;
$COERCE{$name}->{$type} = $code;
}
+ return;
}
sub class_type {
sub find_or_create_isa_type_constraint {
my $type_constraint = shift;
+ Carp::confess("Got isa => type_constraints, but Mouse does not yet support parameterized types for containers other than ArrayRef and HashRef and Maybe (rt.cpan.org #39795)")
+ if $type_constraint =~ /\A ( [^\[]+ ) \[\.+\] \z/xms &&
+ $1 ne 'ArrayRef' &&
+ $1 ne 'HashRef' &&
+ $1 ne 'Maybe'
+ ;
+
my $code;
$type_constraint =~ s/\s+//g;