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

Re^2: Moose and BUILD

by ikegami (Patriarch)
on Jun 15, 2011 at 20:56 UTC ( [id://909853]=note: print w/replies, xml ) Need Help??


in reply to Re: Moose and BUILD
in thread Moose and BUILD

The type is specified using isa, and you need lazy => 1 to make sure the default isn't calculated too soon.

use Moose; has 'x' => ( is => 'rw' isa => 'Num', ); has 'y' => ( is => 'rw', isa => 'Num', lazy => 1, default => sub { sqrt($_[0])->x) }, );

Replies are listed 'Best First'.
Re^3: Moose and BUILD
by tj_thompson (Monk) on Jun 15, 2011 at 21:04 UTC

    Good catch on the is, just a brain typo from rapidly throwing together running example code. It didn't matter in this case as I wasn't trying to use bad values in my test script.

    I will look into the lazy flag on the attribute and get a better idea of what it does.

      Always use lazy when the initialiser relies on another field of $self.

      It cause the initialiser to be called when the field is read instead of being called on construction.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://909853]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others musing on the Monastery: (7)
As of 2024-04-23 11:41 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found