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 } -- Best Regards, Nick Knutov http://knutov.com ICQ: 272873706 Voice: +7-904-84-23-130
On Fri, May 20, 2011 at 2:57 PM, Nick Knutov <mail@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@perldancer.org http://www.backup-manager.org/cgi-bin/listinfo/dancer-users
On Friday 20 May 2011 21:01:22 Naveed Massjouni wrote:
On Fri, May 20, 2011 at 2:57 PM, Nick Knutov <mail@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. Cheers Dave P -- David Precious ("bigpresh") http://www.preshweb.co.uk/ "Programming is like sex. One mistake and you have to support it for the rest of your life". (Michael Sinz)
Hi, you can use a state variable : before sub { state $cache_loaded ||= _load_cache(); } the only requirements are that you use perl 5.10 or greater, and that _load_cache() does all the init you want, and return true on success. On 20 May 2011 20:57, Nick Knutov <mail@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 }
-- Best Regards, Nick Knutov http://knutov.com ICQ: 272873706 Voice: +7-904-84-23-130 _______________________________________________ Dancer-users mailing list Dancer-users@perldancer.org http://www.backup-manager.org/cgi-bin/listinfo/dancer-users
participants (4)
-
damien krotkine -
David Precious -
Naveed Massjouni -
Nick Knutov