Where to put initialization code (connecting to databases, generating stuff, loading data files, etc)?
Here are some existing options:
(+) Good encapsulation.
(-) Everything is tied to databases, configuration, specific file locations etc. Hard to isolate unit tests, even harder to run code snippets to find bugs, as in perl -MFoo -d -we 'Foo->new'
(-) Code away from where it's used, easy to forget something.
(-) Initializing all-or-nothing.
(-) still easy to forget to run it.
bash$ plackup -e 'use warnings; INIT{ warn "foo"; }; sub { warn "here +"; return [200, [], []] };' Too late to run INIT block at (eval 7) line 1. HTTP::Server::PSGI: Accepting connections at http://0:5000/ here at (eval 7) line 1. 127.0.0.1 - - [20/Nov/2014:14:18:08 +0200] "GET / HTTP/1.1" 200 - "-" + "Mozilla/5.0 (X11; Ubuntu; Linux i686; rv:33.0) Gecko/20100101 Firef +ox/33.0"
Note the Too late to call INIT warning and no sign of "foo" in output.
Now I'd like to have a module which is used as follows:
In project's module:
use Init::Queue sub { get_dbh(); load_file(); build_cache(); }; # postpone till explicitly called
In production/initialization code:
Init::Queue->startup(); # this executes all startup blocks, # in order of appearance
Is there such a module? If not, is it needed? Or is there a simpler approach which I've overlooked?
Crosspost: stackoverflow
In reply to Module for executing postponed initialization code by Dallaylaen
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |