Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
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 contemplating the Monastery: (15)
As of 2015-07-31 20:59 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (282 votes), past polls