[Catalyst] How and where to run a method at Catalyst start up?

Rodrigo rodrigolive at gmail.com
Fri May 15 12:44:52 GMT 2009


On Fri, May 15, 2009 at 12:12 PM, Emmanuel Quevillon <tuco at pasteur.fr>wrote:

> Hi all,
>
> I am developing a Catalyst App and I'd like to know how to launch a
> method at Catalyst start up?


In MyApp.pm:

# Start the application
__PACKAGE__->setup();
mymethod();
sub mymethod {
    ...
}
But that's probably not what you're looking for...


>
> The idea is to check and create some kind of a data structure that
> will be loaded into memory (__PACKAGE__->{mydata} =3D { ... }) of my
> application and will be then accessible at anytime from anywhere in
> any controller
>

Create a model for it.
$ script/myapp_create.pl model MyData

Then create the data structure in Model/MyData.pm:

package MyApp::Model::MyData;
use Moose;
extends 'Catalyst::Model';
has 'mydata' =3D> ( is=3D>'rw', isa=3D>'HashRef', default=3D>sub{{}} );
no Moose;
sub BUILD {
   my $self=3Dshift;
   my $data =3D MyApp->model('DB::Table')->search(...)->first; # I rather
avoid storing dbic resultsets in memory for a long time.
    $self->mydata({  foo=3D> $data } ); }
}

Then call it from any controller:

sub foo : Local {
      my $c=3Dpop;
      $c->stash->{foome} =3D $c->model('MyData')->mydata->{foo};
}


>
> But how can I access my database within MyApp.pm ?
> I tried (in MyApp.pm):
>

If don't have $c defined, you can use MyApp->model() most of the time.

--rodrigo
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.scsys.co.uk/pipermail/catalyst/attachments/20090515/22ca1=
b4f/attachment.htm


More information about the Catalyst mailing list