my $union = Moose::Meta::TypeConstraint::Union->new(type_constraints=>\@tc);
return Moose::Util::TypeConstraints::register_type_constraint($union);
},
+ ( $] >= 5.010 ? (
+ '~~' => sub {
+ my ( $self, $value ) = @_;
+ $self->__type_constraint->check($value);
+ },
+ ) : () ),
fallback => 1,
);
--- /dev/null
+#!/usr/bin/perl
+
+use strict;
+use warnings;
+
+use Test::More;
+
+BEGIN {
+ plan skip_all => "5.10.0 required" unless eval { require 5.010 };
+ plan tests => 8;
+}
+
+use MooseX::Types::Moose qw(Str HashRef ArrayRef);
+
+use 5.010;
+
+ok( "foo" ~~ Str, "string" );
+ok( Str() ~~ "foo", "string" );
+ok( !("foo" ~~ HashRef), "hashref" );
+ok( !(HashRef() ~~ "foo"), "hashref" );
+
+sub foo {
+ given ($_[0]) {
+ # unfortunately we can't actually have when(HashRef) etc, because that
+ # resolves as a boolean (checks whether the return value of HashRef etc
+ # is true)
+ when ($_ ~~ HashRef) { return "hash" }
+ when ($_ ~~ ArrayRef) { return "array" }
+ when ($_ ~~ Str) { return "string" }
+ default { return "something else" }
+ }
+}
+
+is( foo({}), "hash" );
+is( foo("foo"), "string" );
+is( foo([]), "array" );
+is( foo(sub { }), "something else" );