From: Shawn M Moore <sartak@gmail.com>
Date: Wed, 12 Aug 2009 01:00:06 +0000 (-0400)
Subject: Fix bug with $obj->new when $obj has stringify overloading
X-Git-Tag: 0.89~9
X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=5d27ac732591e1bb45270a185f71bb37d91a4a06;p=gitmo%2FMoose.git

Fix bug with $obj->new when $obj has stringify overloading
---

diff --git a/Changes b/Changes
index 96f4f38..038f786 100644
--- a/Changes
+++ b/Changes
@@ -27,6 +27,10 @@ next version
       - A trigger now receives the old value as a second argument, if
         the attribute had one.
 
+    * Moose::Meta::Method::Constructor
+      - Fix a bug with $obj->new when $obj has stringify overloading.
+        Reported by Andrew Suffield [rt.cpan.org #47882] (Sartak)
+
 0.88 Fri Jul 24, 2009
     * Moose::Manual::Contributing
       - Re-write the Moose::Manual::Contributing document to reflect
diff --git a/lib/Moose/Meta/Method/Constructor.pm b/lib/Moose/Meta/Method/Constructor.pm
index 1d20b73..7e8e2f2 100644
--- a/lib/Moose/Meta/Method/Constructor.pm
+++ b/lib/Moose/Meta/Method/Constructor.pm
@@ -60,7 +60,8 @@ sub _initialize_body {
     # requires some adaption on the part of
     # the author, after all, nothing is free)
     my $source = 'sub {';
-    $source .= "\n" . 'my $class = shift;';
+    $source .= "\n" . 'my $_instance = shift;';
+    $source .= "\n" . 'my $class = Scalar::Util::blessed($_instance) || $_instance;';
 
     $source .= "\n" . 'return $class->Moose::Object::new(@_)';
     $source .= "\n    if \$class ne '" . $self->associated_metaclass->name
diff --git a/t/100_bugs/027_constructor_object_overload.t b/t/100_bugs/027_constructor_object_overload.t
new file mode 100644
index 0000000..09c61d0
--- /dev/null
+++ b/t/100_bugs/027_constructor_object_overload.t
@@ -0,0 +1,19 @@
+#!/usr/bin/perl
+use strict;
+use warnings;
+use Test::More tests => 1;
+
+{
+    package Foo;
+
+    use Moose;
+
+    use overload '""' => sub {''};
+
+    sub bug { 'plenty' }
+
+    __PACKAGE__->meta->make_immutable;
+}
+
+ok(Foo->new()->bug(), 'call constructor on object reference with overloading');
+