[Catalyst] Making a hash available across the application

J. Shirley jshirley at gmail.com
Wed Mar 17 18:04:35 GMT 2010

On Wed, Mar 17, 2010 at 9:43 AM, Dermot <paikkos at googlemail.com> wrote:
> Hi,
> I'm sure there is a way but it's not jumping out at me.
> I have a list of countries and their associated database ID's that I
> need to stash in the header of (almost) every page on a site. I have
> no problems creating a controller that can do
> $c->model(DB::Countries)->search({foo=>1}) and stick that into a
> $c->stash but I don't want to make repeated DB calls. I'd like to
> create a hash at startup and make it available to
> root/lib/site/header. Is it possible to create a hash within
> lib/MyApp.pm that will be available throughout and how would my
> templates access it?
> TIA,
> Dp.

You can quite easily create accessors off the schema, and use lazy_build.

Something like this:

package MyApp::Schema;

use Moose;
extends 'DBIx::Class::Schema';

has something => (
    is => ro,
    lazy_build => 1

sub _build_something {
    my ( $self ) = @_;
    [ $self->resultset('Countries')->search({ foo => 1 })->all ];

no Moose;

Then after that if you wish, you can do clear_something and it will be


(Code untested, typed out quickly)

More information about the Catalyst mailing list