Subject: | Cloning of Moose object fails silently if the instance has been placed in a fieldhash |
When cloning a Moose object, which have been placed in a fieldhash
first, the cloning fails silently. The object is cloned, but all the
attributes are empty.
I've a attached a test program, which demonstrates this bug. In our
setup, we get the following:
# =[ Versions ]=
# Clone v0.31
# Moose v1.24
# Hash::Util::FieldHash v1.04
ok 1 - Don't use a fieldhash before cloning
Use of uninitialized value in string eq at clone.t line 42.
not ok 2 - Using a fieldhash before cloning
# Failed test 'Using a fieldhash before cloning'
# at clone.t line 42.
# bless({ somevar => "Test" }, "Foobar") != bless({}, "Foobar")
1..2
# Looks like you failed 1 test of 2.
As Hash::Util::FieldHash is a native part of Perl, I've chosen to report
the bug here first.
Subject: | clone.t |
use strict;
use warnings;
use Test::More;
use Test::Exception;
use Data::Dump qw(pp);
use Hash::Util::FieldHash 'fieldhash';
use Clone 'clone';
#=============================================================================
# Declare a very simple package
#=============================================================================
{
package Foobar;
use Moose;
has somevar => (
is => "rw",
isa => "Str",
);
1;
}
#=============================================================================
# Test clone
#=============================================================================
{
fieldhash my %fh;
note "=[ Versions ]=\n";
note "Clone v", $Clone::VERSION, "\n";
note "Moose v", $Moose::VERSION, "\n";
note "Hash::Util::FieldHash v", $Hash::Util::FieldHash::VERSION, "\n";
sub clonetest {
my $use_fieldhash = shift;
my $foo = Foobar->new(somevar => "Test");
$fh{$foo} = $foo if $use_fieldhash;
my $bar = clone($foo);
ok (
($foo->somevar eq $bar->somevar),
join '', ($use_fieldhash ? "Using " : "Don't use "), "a fieldhash before cloning"
) or diag(pp($foo), ' != ', pp($bar));
};
clonetest(0);
clonetest(1);
}
done_testing;