Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Re: Can't call method "MoveBook" on an undefined value

by tobyink (Abbot)
on Jan 24, 2013 at 19:52 UTC ( #1015219=note: print w/ replies, xml ) Need Help??


in reply to Can't call method "MoveBook" on an undefined value

You should use a default or builder for the attributes. Defaults work like this:

package LibraryTest; use Moose; use Book; has unread_books => ( is =>'rw', isa => 'ArrayRef[Book]', default => sub { [] }, ); has read_books => ( is =>'rw', isa => 'ArrayRef[Book]', default => sub { [] }, );

Builders work like this:

package LibraryTest; use Moose; use Book; has unread_books => ( is =>'rw', isa => 'ArrayRef[Book]', builder => '_build_unread_books', ); has read_books => ( is =>'rw', isa => 'ArrayRef[Book]', builder => '_build_read_books', ); sub _build_unread_books { return []; } sub _build_read_books { return []; }

Defaults are very convenient to define, but builders are arguably a better way to do things: the builder is a regular method, so subclasses can easily override it, which is handy.

Builders are a very handy thing when you get used to them - especially when you use them in conjunction with Moose's lazy attribute initialisation feature.

package Cow { use Moo; has name => (is => 'lazy', default => sub { 'Mooington' }) } say Cow->new->name


Comment on Re: Can't call method "MoveBook" on an undefined value
Select or Download Code

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (7)
As of 2014-12-28 06:20 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (178 votes), past polls