[Catalyst] Moose attributes not initializing when including FormHandler form

Tomas Doran bobtfish at bobtfish.net
Wed Jan 4 18:25:20 GMT 2012


On 4 Jan 2012, at 17:17, Rippl, Steve wrote:
> Thanks for the offer of help!  Here's the error message and Stack  
> Trace (excuse the html mail, but I'm thinking it makes it easier to  
> read?)...

Ah, right!

has details => (
     is      => 'rw',
     isa     => 'HashRef',
     builder => '_build_details',
);

has _temp_details => (
     is      => 'rw',
     isa     => 'HashRef',
     default => sub{ {} },
);

sub _build_details {
     my $self = shift;
     # irrelevant code removed
     $self->_temp_details->{foo} # Fails
}

This is because initialization or the slots in the instance will  
happen in a random order..

So what's happening is that 'details' is being initialized (and so  
calling it's _build_details method) before '_temp_details' is  
initialized, which results in the temp_details accessor returning  
nothing when it's called (as _temp_details hasn't been initialized yet).

You need to add a 'lazy => 1' to the 'details' attribute, and all  
should be well...

(If you _really_ want to ensure that the details attribute is built at  
construction time, then also add a BUILD method that just calls the  
accessor - ensuring the value gets built).

HTH
Cheers
t0m




More information about the Catalyst mailing list