Now in Baidu WISE team
全部博文(150)
分类: Python/Ruby
2012-02-12 18:49:26
1. BEGIN() - It will be called before compile. And because all code has not been load, you cannot use any variable defined in the code. Only some default variables about the package is avaliable.
2. CHECK() - Called at the end of the compilation process.
3. END() - Called at the end of the execution process.
Whatever the complation process success or fail, the 3 subroutines will be executed. And all varibales and resources defined in code are not available. Only perl own variables are availale, such as "__PACKAGE__"
4. INIT() - Called at the beginning of the execution process.
In INIT, you can use variables defined in code.
See the example:
There are only 2 sentences in main. I defined a scalar $f and output a sentence.
If there are syntx error in the 2 sentences, you will find that BEGIN, CHECK, END are executed, and 'Use of uninitialized value $f in concatenation (.) or string' is reported. The execution order is BEGIN->CHECK->END.
If there is not errors, 4 subroutines will be excuted. INIT is executed between CHECK and END. Also, $f is uninitialized in BEGIN, CHECK and END. But $f is available in INIT. The execution order is BEGIN->CHECK->INIT->END