From: Dagfinn Ilmari Mannsåker Date: Wed, 8 Aug 2012 21:36:36 +0000 (+0100) Subject: make setter for weak_ref attributes return the value X-Git-Tag: v1.000003~6 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=f3dc0ac648d1bfcd936b9e0f856f9303c3ea0064;p=gitmo%2FMoo.git make setter for weak_ref attributes return the value --- diff --git a/Changes b/Changes index 137efbd..5587876 100644 --- a/Changes +++ b/Changes @@ -1,3 +1,5 @@ + - make setter for weak_ref attributes return the value + 1.000002 - 2012-08-04 - remove Devel::GlobalDestruction fallback inlining because we can now depend on 0.08 which uses Sub::Exporter::Progressive diff --git a/lib/Method/Generate/Accessor.pm b/lib/Method/Generate/Accessor.pm index bfecf26..b5ee174 100644 --- a/lib/Method/Generate/Accessor.pm +++ b/lib/Method/Generate/Accessor.pm @@ -448,7 +448,7 @@ sub _generate_simple_set { # # but requires XS and is just too damn crazy # so simply throw a better exception - my $weak_simple = "my \$preserve; Scalar::Util::weaken(${simple})"; + my $weak_simple = "my \$preserve; Scalar::Util::weaken(${simple}); no warnings 'void'; \$preserve"; Moo::_Utils::lt_5_8_3() ? <<"EOC" : $weak_simple; my \$preserve; diff --git a/t/accessor-weaken.t b/t/accessor-weaken.t index ec5b069..89f49fa 100644 --- a/t/accessor-weaken.t +++ b/t/accessor-weaken.t @@ -6,28 +6,31 @@ use Test::More; use Moo; - has one => (is => 'ro', weak_ref => 1); + has one => (is => 'rw', weak_ref => 1); package Foo2; use Moo; - has one => (is => 'lazy', weak_ref => 1, default => sub { {} }); + has one => (is => 'rw', lazy => 1, weak_ref => 1, default => sub { {} }); } my $ref = {}; my $foo = Foo->new(one => $ref); is($foo->one, $ref, 'value present'); ok(Scalar::Util::isweak($foo->{one}), 'value weakened'); +is($foo->one($ref), $ref, 'value returned from setter'); undef $ref; 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'); +is($foo2->one($ref2), $ref2, 'value returned from setter'); undef $ref2; ok(!defined $foo->{one}, 'weak value gone'); + # test readonly SVs sub mk_ref { \ 'yay' }; my $foo_ro = eval { Foo->new(one => mk_ref()) };