[Dancer-users] init on first run

David Precious davidp at preshweb.co.uk
Sat May 21 13:49:42 CEST 2011

On Friday 20 May 2011 21:01:22 Naveed Massjouni wrote:
> On Fri, May 20, 2011 at 2:57 PM, Nick Knutov <mail at knutov.com> wrote:

> You don't need the $first in your case:
>     before sub {
>         if (not $cache->{statuses}) {
>             $cache->{statuses} = ...;
>             _load_cache();
>         }
>     }

Or perhaps simpler still:

    before sub {
        $cache->{statuses} ||= _load_cache();

> If you are not running in a CGI environment, then you should be able to
> just do:
>     use Dancer;
>     # put this in the file scope
>     $cache->{statuses} = ...;
>     _load_cache();
>     # now declare routes
> I believe the lines in the file scope will only get run once, running
> in a persistent environment such as Plack servers or FastCGI.

Yep - that's how I'd do it.  That'll run at runtime once as the app is 
starting up, job done.

This is what I did in DancerJukebox to add some code to run at app startup to 
fork a new process to do stuff in the background, and it works a treat.


Dave P

David Precious  ("bigpresh")

   "Programming is like sex. One mistake and you have to support
   it for the rest of your life". (Michael Sinz)

More information about the Dancer-users mailing list