in reply to Possible issue with Moose ScalarRef
I agree; this seems broken. Small test case...
use strict; use warnings; use Test::More; { package Thing; use Moose; has attr => (is => 'ro'); } { package ThingWithRef; use Moose; has attr => (is => 'ro'); sub translate_to_dutch { my $self = shift; ${ $self->attr } =~ s/e/a/; } } my $obj1 = Thing->new(attr => 'Hello'); my $obj2 = ThingWithRef->new(attr => \$obj1->attr); $obj2->translate_to_dutch; is($obj1->attr, 'Hallo'); done_testing;
If you use Moo instead of Moose, then things work as expected.
UPDATE: OK, not a bug. It's just that the reference \$obj1->attr is not guaranteed to be a reference to the slot within the object. So changing data via the reference will not necessarily effect $obj1. If it works, it works; if not, then it won't; no guarantees. Currently it works in Moo but not Moose, but the situation - in either - could change.
If you want what you want (for both objects to have access to the same piece of data) then you need it to be a scalar ref in both classes.
UPDATE 2: It also fails in Moo if Moo is in pure Perl mode (not XS).
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Possible issue with Moose ScalarRef
by greengaroo (Hermit) on Jan 11, 2013 at 15:12 UTC | |
by tobyink (Canon) on Jan 11, 2013 at 15:44 UTC | |
by greengaroo (Hermit) on Jan 11, 2013 at 15:52 UTC |