Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

comment on

( [id://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

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



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others contemplating the Monastery: (3)
As of 2024-04-24 20:18 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found