Subject: | Weakened refs always clone as undef |
Date: | Mon, 14 Mar 2016 23:36:11 -0700 |
To: | bug-Clone [...] rt.cpan.org |
From: | Jim Avera <jim.avera [...] gmail.com> |
Hi,
Your very-useful module Clone does not seem to handle weakened refs:
They always clone as undef.
Please run the test script below to see the problem.
(bug observed in Clone version 0.38, the latest in CPAN)
Thanks,
-Jim Avera
P.S. The author email in the source (gsar@activestate.com) bounced.
#!/usr/bin/perl
use strict; use warnings;
package Parent;
sub new { bless { children => [] }, shift }
package Child;
use Scalar::Util qw(weaken);
sub new {
my ($class, $parent) = @_;
my $child = bless { parent => $parent }, $class;
weaken($child->{parent}); ### COMMENT OUT TO REMOVE BUG
push @{ $parent->{children} }, $child;
return $child;
}
package main;
use Data::Dumper;
use Clone qw(clone);
my $p = Parent->new(foo => 123, bar => 456);
my $c = Child->new($p);
my $c_clone = clone($c);
warn Data::Dumper->Dump([$c], ["c"]);
warn Data::Dumper->Dump([$c_clone], ["c_clone"]);
die "bug" if ! defined $c->{parent};
die "bug" if ! defined $c_clone->{parent};