J
J Krugman
In Programming Perl, Third Edition, page 484, it says:
INIT blocks are really just like BEGIN blocks, except they let
the programmer distinguish construction that must happen at
compile phase from construction that must happen at run phase.
When you're running a script directly, that's not terribly
important because the compiler gets invoked every time anyway;
but when compilation is separate from execution, the distinction
can be crucial. The compiler may only be invoked once, and the
resulting executable may be invoked many times.
The last sentence suggests that if initialization code is stuck in
a BEGIN block for a program that is compiled once but run many
times, then the initialization will not be properly done except
for the very first execution. The only common scenario I could
think of where this separation of compilation and execution would
happen is something like mod_perl, but then I found this in Practical
mod_perl by Bekman and Cholet:
Perl calls [INIT and CHECK] blocks only during perl_parse( ),
which mod_perl calls once at startup time. Therefore, ...INIT
blocks don't work in mod_perl, for the same reason [this doesn't]:
panic% perl -e 'eval qq(INIT { print "ok\n" })'
So now I'm even more confused. If Perl indeed calls INIT only
during perl_parse, then I don't see how the excerpt from PP can be
correct, since it seems farfetched that parsing of the program
could occur multiple times for a single compilation.
My two questions are: 1) notwithstanding the excerpt from PP, in
the case of a script intended to be run under mod_perl, would a
BEGIN block be the right way to take care of initialization code,
like this
{
my $private; BEGIN { $private = 1 } sub foo {
# use $private } }
? And 2) what's a reasonably common, realistic scenario in which
compilation would happen once, execution many times, and the
distinction between BEGIN and INIT was meaningful in the sense
described by the PP excerpt above?
TIA!
jill
P.S. Is this the right forum for this question?
INIT blocks are really just like BEGIN blocks, except they let
the programmer distinguish construction that must happen at
compile phase from construction that must happen at run phase.
When you're running a script directly, that's not terribly
important because the compiler gets invoked every time anyway;
but when compilation is separate from execution, the distinction
can be crucial. The compiler may only be invoked once, and the
resulting executable may be invoked many times.
The last sentence suggests that if initialization code is stuck in
a BEGIN block for a program that is compiled once but run many
times, then the initialization will not be properly done except
for the very first execution. The only common scenario I could
think of where this separation of compilation and execution would
happen is something like mod_perl, but then I found this in Practical
mod_perl by Bekman and Cholet:
Perl calls [INIT and CHECK] blocks only during perl_parse( ),
which mod_perl calls once at startup time. Therefore, ...INIT
blocks don't work in mod_perl, for the same reason [this doesn't]:
panic% perl -e 'eval qq(INIT { print "ok\n" })'
So now I'm even more confused. If Perl indeed calls INIT only
during perl_parse, then I don't see how the excerpt from PP can be
correct, since it seems farfetched that parsing of the program
could occur multiple times for a single compilation.
My two questions are: 1) notwithstanding the excerpt from PP, in
the case of a script intended to be run under mod_perl, would a
BEGIN block be the right way to take care of initialization code,
like this
{
my $private; BEGIN { $private = 1 } sub foo {
# use $private } }
? And 2) what's a reasonably common, realistic scenario in which
compilation would happen once, execution many times, and the
distinction between BEGIN and INIT was meaningful in the sense
described by the PP excerpt above?
TIA!
jill
P.S. Is this the right forum for this question?