Modules - Constructors and Destructors
There are two special subroutine definitions that function as package constructors and destructors. These are the BEGIN 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. An END subroutine is executed as late as possible, that is, when the interpreter is being exited, even if it is exiting as a result of a die function. (But not if it's is 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).