sub _generate_simple_set {
my ($self, $me, $name, $spec, $value) = @_;
my $name_str = perlstring $name;
- my $simple = $self->_generate_core_set($me, $name, $spec, $value);
if ($spec->{weak_ref}) {
+ $value = '$preserve = '.$value;
+ my $simple = $self->_generate_core_set($me, $name, $spec, $value);
require Scalar::Util;
# Perl < 5.8.3 can't weaken refs to readonly vars
#
# but requires XS and is just too damn crazy
# so simply throw a better exception
- Moo::_Utils::lt_5_8_3() ? <<"EOC" : "Scalar::Util::weaken(${simple})";
+ my $weak_simple = "my \$preserve; Scalar::Util::weaken(${simple})";
+ Moo::_Utils::lt_5_8_3() ? <<"EOC" : $weak_simple;
+ my \$preserve;
eval { Scalar::Util::weaken($simple); 1 } or do {
if( \$@ =~ /Modification of a read-only value attempted/) {
require Carp;
};
EOC
} else {
- $simple;
+ $self->_generate_core_set($me, $name, $spec, $value);
}
}
use Moo;
has one => (is => 'ro', weak_ref => 1);
+
+ package Foo2;
+
+ use Moo;
+
+ has one => (is => 'lazy', weak_ref => 1, default => sub { {} });
}
my $ref = {};
is($foo->one, $ref, 'value present');
ok(Scalar::Util::isweak($foo->{one}), 'value weakened');
undef $ref;
-ok (!defined $foo->{one}, 'weak value gone');
+ok(!defined $foo->{one}, 'weak value gone');
+
+my $foo2 = Foo2->new;
+ok(my $ref2 = $foo2->one, 'value returned');
+ok(Scalar::Util::isweak($foo2->{one}), 'value weakened');
+undef $ref2;
+ok(!defined $foo->{one}, 'weak value gone');
# test readonly SVs
sub mk_ref { \ 'yay' };