[Catalyst] Store something in the stash at startup

Charlie Garrison garrison at zeta.org.au
Mon Aug 16 09:47:25 GMT 2010

Good evening,

On 16/08/10 at 11:05 AM +0300, Octavian Rasnita 
<orasnita at gmail.com> wrote:

>I have a module that creates an object (a menu) and I want to 
>store it in the stash when the application starts.
>Where is the recommended place to create this object and store 
>it in the stash if I want that object created just once at app startup?
>Should I override a certain method in MyApp.pm and create the object there?

Does it need to be in stash? How about a method in MyApp.pm that 
supplies the data? Eg.

sub menu_data {
     my $self = shift; # same as $c
     return {
         item1 => 'some data',

Or just use some Moose goodness instead:

has "menu_data" => (
     is => "ro",
     isa => "HashRef",
     default => sub { shift->config->{menu_data} }, #pull data 
from config

And then later in your template:

[% c.menu_data.item_1 %]

If you really do want the data in the stash, then create the 
object at app startup and then assign it to stash on each request:

after 'prepare_action' => sub {
     my $c = shift;
     $c->stash( menu_data => $c->menu_data_object );


    Ꮚ Charlie Garrison ♊ <garrison at zeta.org.au>

O< ascii ribbon campaign - stop html mail - www.asciiribbon.org
〠  http://www.ietf.org/rfc/rfc1855.txt

More information about the Catalyst mailing list