On Fri, May 15, 2009 at 12:12 PM, Emmanuel Quevillon <[email protected]>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} = { ... }) 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' => ( is=>'rw', isa=>'HashRef', default=>sub{{}} );
no Moose;
sub BUILD {
my $self=shift;
my $data = MyApp->model('DB::Table')->search(...)->first; # I rather
avoid storing dbic resultsets in memory for a long time.
$self->mydata({ foo=> $data } ); }
}
Then call it from any controller:
sub foo : Local {
my $c=pop;
$c->stash->{foome} = $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
_______________________________________________
List: [email protected]
Listinfo: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/catalyst
Searchable archive: http://www.mail-archive.com/[email protected]/
Dev site: http://dev.catalyst.perl.org/