}
if(!defined $name){
- if(!defined($name = $args{name})){
- $name = '__ANON__';
- }
+ $name = $args{name};
}
$args{name} = $name;
$parent = delete $args{as};
if(!$parent){
$parent = delete $args{name};
- $name = '__ANON__';
+ $name = undef;
}
}
- my $package_defined_in = $args{package_defined_in} ||= caller(1);
-
- my $existing = $TYPE{$name};
- if($existing && $existing->{package_defined_in} ne $package_defined_in){
- confess("The type constraint '$name' has already been created in "
- . "$existing->{package_defined_in} and cannot be created again in $package_defined_in");
+ if(defined $name){
+ my $package_defined_in = $args{package_defined_in} ||= caller(1);
+ my $existing = $TYPE{$name};
+ if($existing && $existing->{package_defined_in} ne $package_defined_in){
+ confess("The type constraint '$name' has already been created in "
+ . "$existing->{package_defined_in} and cannot be created again in $package_defined_in");
+ }
+ }
+ else{
+ $args{name} = '__ANON__';
}
$args{constraint} = delete $args{where} if exists $args{where};
$constraint = Mouse::Meta::TypeConstraint->new(%args);
}
- return $TYPE{$name} = $constraint;
+ if(defined $name){
+ return $TYPE{$name} = $constraint;
+ }
+ else{
+ return $constraint;
+ }
}
sub type {
#!/usr/bin/env perl
use strict;
use warnings;
-use Test::More tests => 11;
+use Test::More tests => 14;
use Test::Exception;
use Mouse::Util::TypeConstraints;
is => 'ro',
isa => 'NonemptyStr',
);
-
-
};
ok(My::Class->new(name => 'foo'));
ok!$tc->check([]);
ok!$tc->check(undef);
};
+
+package Foo;
+use Mouse::Util::TypeConstraints;
+
+$st = subtype as 'Int', where{ $_ > 0 };
+
+::ok $st->is_a_type_of('Int');
+::ok $st->check(10);
+::ok!$st->check(0);
+