in reply to Moose delegate methods, value not defined

The first error means that these are redundant:

has 'name' => ( is => 'rw', writer => 'set_name', reader => 'get_name', ); has 'email', is => 'rw'; sub set_name { my ($self, $name) = @_; $self->name = $name; } sub get_name { my $self = shift; return $self->name; }

Moose happily generates those methods for you. Let it.

That leaves:

Cannot delegate set_name to set_name because the value of author is not defined...

... which means that when you create your Post object and call $post->set_name( ... ), if $post contains no author, you get an error message to that effect. Pass in an author or create one (with a builder perhaps).


Improve your skills with Modern Perl: the free book.

Replies are listed 'Best First'.
Re^2: Moose delegate methods, value not defined
by monktopher (Novice) on Mar 05, 2012 at 05:23 UTC

    Thanks for the reply. From the documentation, I was under the impression that

    has 'author' => ( is => 'ro', isa => 'Author', handles => [qw( name )], );

    would create an Author object (using the isa command) in the background. I guess I'm incorrect in thinking this.

    I had tried letting Moose generate the accessory methods, but wasn't sure if handles was looking for an explicitly defined method in the module or not.

    Again, thanks for the reply.

      isa imposes a constraint on what can be placed in author. It doesn't place anything in author. Perhaps you want to add

      default => sub { Author->new() },