Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery

Comment on

( #3333=superdoc: print w/replies, xml ) Need Help??

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.

In reply to Possible issue with Moose ScalarRef by greengaroo

Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":

  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?

    What's my password?
    Create A New User
    and all is quiet...

    How do I use this? | Other CB clients
    Other Users?
    Others contemplating the Monastery: (4)
    As of 2018-04-21 08:48 GMT
    Find Nodes?
      Voting Booth?