[Catalyst] Initialization

Tomas Doran bobtfish at bobtfish.net
Sat Mar 5 09:01:22 GMT 2011


On 5 Mar 2011, at 07:15, John M. Dlugosz wrote:

> I want to do something once ahead of time, not every time my Model  
> is used by a page.  Where is the correct place to put code that will  
> execute once during app set-up, and how can it know what the  
> configured root directory is?

The scope of a model is (if you don't change it) per application  
lifetime.

Ergo you can just do your work in the BUILD method for your model.

Just pass in the root you want the model to see as config, add an  
attribute, and the value will be there for you..

Something like:

package MyApp::Model::Foo;
use Moose;
use namespace::autoclean;

has root => (
     is => 'ro',
     required => 1,
);

sub BUILD {
     my $self = shift;
     ... your code here, and you can call $self->root  ...
}

and then in MyApp.pm:

__PACKAGE__->config(
     'Model::Foo' => {
         root => __PACKAGE__->path_to('root'),
   },
);

Cheers
t0m




More information about the Catalyst mailing list