Always means alway, well ;)
from perldoc perlmod
Package Constructors and Destructors
Four special subroutines act as package constructors and destructors.
These are the BEGIN, CHECK, INIT, and END routines. The
sub is optional for these routines.
A BEGIN subroutine is executed as soon as possible, that is, the moment
it is completely defined, even before the rest of the containing file
is parsed. You may have multiple BEGIN blocks within a file--they
will execute in order of definition. Because a BEGIN block executes
immediately, it can pull in definitions of subroutines and such from other
files in time to be visible to the rest of the file. Once a BEGIN
has run, it is immediately undefined and any code it used is returned to
Perl's memory pool. This means you can't ever explicitly call a BEGIN.
An END subroutine is executed as late as possible, that is, after
perl has finished running the program and just before the interpreter
is being exited, even if it is exiting as a result of a die() function.
(But not if it's polymorphing into another program via exec, or
being blown out of the water by a signal--you have to trap that yourself
(if you can).) You may have multiple END blocks within a file--they
will execute in reverse order of definition; that is: last in, first
out (LIFO). END blocks are not executed when you run perl with the
-c switch, or if compilation fails.
Inside an END subroutine, $? contains the value that the program is
going to pass to exit(). You can modify $? to change the exit
value of the program. Beware of changing $? by accident (e.g. by
running something via system).
Similar to BEGIN blocks, INIT blocks are run just before the
Perl runtime begins execution, in ``first in, first out'' (FIFO) order.
For example, the code generators documented in perlcc make use of
INIT blocks to initialize and resolve pointers to XSUBs.
Similar to END blocks, CHECK blocks are run just after the
Perl compile phase ends and before the run time begins, in
LIFO order. CHECK blocks are again useful in the Perl compiler
suite to save the compiled state of the program.
When you use the -n and -p switches to Perl, BEGIN and
END work just as they do in awk, as a degenerate case.
Both BEGIN and CHECK blocks are run when you use the -c
switch for a compile-only syntax check, although your main code
is not.
MJD says you
can't just make shit up and expect the computer to know what you mean, retardo!
** The Third rule of perl club is a statement of fact: pod is sexy.
|
| [reply] [d/l] [select] |