http://www.perlmonks.org?node_id=1012711
greengaroo has asked for the wisdom of the Perl Monks concerning the following question:

Happy new year fellow Monks!

I am either doing something wrong or I just found a bug in Moose. I hope its me and not Moose!

What I am trying to do is to save a reference to a scalar in another object so if I change the value of the original scalar in my first object, it will be effective also in the second object.

About this syntax, keep in mind I am using MooseX::Declare, also in this case the "debug_level" in $self isa Int and the "debug_level" in the $obj (Something class) it isa ScalarRef[Int]. My code roughly look like this:

$self->debug_level( 4 ); print "Original: ", \$self->debug_level, " Value: ", $self->debug_ +level, "\n"; my $obj = Something->new( { 'debug_level' => \$self->debug_level, } ); print "Reference: ", $obj->debug_level, " Value: ", ${$obj->debug_ +level}, "\n"; $self->debug_level( 0 ); print "After change:\n"; print "Original: ", \$self->debug_level, " Value: ", $self->debug_ +level, "\n"; print "Reference: ", $obj->debug_level, " Value: ", ${$obj->debug_ +level}, "\n";

And the output is:

Original: SCALAR(0xd463da0) Value: 4 Reference: SCALAR(0xd468640) Value: 4 After change: Original: SCALAR(0xd463da0) Value: 0 Reference: SCALAR(0xd468640) Value: 4

Why is the SCALAR address not the same? And of course, that leads to the problem of the second instance not being updated!

Here is the definition of the attribute in the first class (it extends MooseX::App::Cmd::Command):

has 'debug_level' => ( traits => [qw(Getopt)], is => 'rw', isa => 'Int', default => 0, cmd_aliases => 'd', documentation => 'Debug mode: 0-4; Default: 0;' );

Here is the definition of the attribute in the second class (it uses MooseX::Declare):

has 'debug_level' => ( is => 'rw', isa => 'ScalarRef[Int]', );

What am I doing wrong??? Thank you all!

UPDATE: I found a workaround, and it seems to do what I want, but I still have a different SCALAR address for the original and the reference. I now do this:

my $obj = Something->new( { # Before: #'debug_level' => \$self->debug_level, # Now: 'debug_level' => \$self->{'debug_level'}, } );
Testing never proves the absence of faults, it only shows their presence.