[Dancer-users] init on first run

Naveed Massjouni naveedm9 at gmail.com
Fri May 20 22:01:22 CEST 2011


On Fri, May 20, 2011 at 2:57 PM, Nick Knutov <mail at knutov.com> wrote:
> Hello all,
>
> what is the best way to do some initialization for some data on the only
> first run? Something like 'before sub', but it should run only on the first
> run of the script instance.
>
> Example of use:
>
> our $first;
> before sub {
>        # if this is the first run of instance
>        # $cache->{statuses} = ... # load some static data from DB
>
>        # I can do
>        # _load_cache() unless $first;
>        # $first = 1;
>        # but this is not beautiful
> }

You don't need the $first in your case:

    before sub {
        if (not $cache->{statuses}) {
            $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.

-Naveed Massjouni

> --
> Best Regards,
> Nick Knutov
> http://knutov.com
> ICQ: 272873706
> Voice: +7-904-84-23-130
> _______________________________________________
> Dancer-users mailing list
> Dancer-users at perldancer.org
> http://www.backup-manager.org/cgi-bin/listinfo/dancer-users
>


More information about the Dancer-users mailing list