Remove extra newline
[gitmo/Moose.git] / lib / Moose / Meta / TypeConstraint.pm
index 989ed2b..3e370dc 100644 (file)
@@ -12,6 +12,7 @@ use overload '0+'     => sub { refaddr(shift) }, # id an object
 
 use Scalar::Util qw(blessed refaddr);
 use Sub::Name qw(subname);
+use Try::Tiny;
 
 use base qw(Class::MOP::Object);
 
@@ -42,6 +43,11 @@ __PACKAGE__->meta->add_attribute('hand_optimized_type_constraint' => (
     predicate => 'has_hand_optimized_type_constraint',
 ));
 
+__PACKAGE__->meta->add_attribute('inlined' => (
+    accessor  => 'inlined',
+    predicate => 'has_inlined_type_constraint',
+));
+
 sub parents {
     my $self;
     $self->parent;
@@ -121,6 +127,15 @@ sub validate {
     }
 }
 
+sub _inline_check {
+    my $self = shift;
+
+    die 'Cannot inline a type constraint check for ' . $self->name
+        unless $self->has_inlined_type_constraint;
+
+    return $self->inlined->( $self, @_ );
+}
+
 sub assert_valid {
     my ($self, $value) = @_;
 
@@ -138,7 +153,18 @@ sub get_message {
         return $msg->($value);
     }
     else {
-        $value = (defined $value ? overload::StrVal($value) : 'undef');
+        # have to load it late like this, since it uses Moose itself
+        my $can_partialdump = try {
+            # versions prior to 0.14 had a potential infinite loop bug
+            Class::MOP::load_class('Devel::PartialDump', { -version => 0.14 });
+            1;
+        };
+        if ($can_partialdump) {
+            $value = Devel::PartialDump->new->dump($value);
+        }
+        else {
+            $value = (defined $value ? overload::StrVal($value) : 'undef');
+        }
         return "Validation failed for '" . $self->name . "' with value $value";
     }
 }
@@ -227,7 +253,6 @@ sub _compile_hand_optimized_type_constraint {
 
     unless ( ref $type_constraint ) {
         require Moose;
-        Carp::confess ("Hand optimized type constraint for " . $self->name . " is not a code reference");
         Moose->throw_error("Hand optimized type constraint is not a code reference");
     }