这是5个特殊的代码块。要理解这几个块,关键在于几个时间点:ui
在BEGIN期间能够作一些程序执行以前的操做,例如事先给某个比较特殊的变量赋值,检查文件是否存在,检查操做系统是否知足要求等等。操作系统
package Foo; use strict; use warnings; BEGIN { print "This is the first BEGIN block\n"; } print "The program is running\n"; BEGIN { print "This is the second BEGIN block\n"; }
因为BEGIN代码块在编译期间执行,程序普通行的print是在执行期间执行,因此上面的代码结果为:code
This is the first BEGIN block This is the second BEGIN block The program is running
下面程序出现语法错误,但BEGIN也会执行:it
BEGIN { print "This is the first BEGIN block\n"; } print "The program is running\n"; BEGIN { print "This is the second BEGIN block\n"; } my $x =;
执行结果:io
syntax error at some_program.pl line 8, near "=;" Execution of some_program.pl aborted due to compilation errors. This is the first BEGIN block This is the second BEGIN block
不过上面的error信息不必定会最早输出,由于stdout和stderr是两个独立的文件句柄,没法保证它们之间的顺序。编译
实际上,use导入模块时若是导入的是空列表,它等价于在BEGIN中使用require语句:ast
use File::Find (); # 等价于 BEGIN { require File::Find; }
END块是在程序执行结束,但退出前执行的,也就是上面的步骤(3)所在期间。require
END { print "This is the first END block\n"; } END { print "This is the second END block\n"; }
输出结果:注意,先输出second END,再输出first END变量
This is the second END block This is the first END block
INIT、CHECK 和 UNITCHECK 块生效于程序编译结束以后、执行以前。因此若是语法错误,它们不会被触发。perl
INIT { print "This is the first INIT block\n"; } CHECK { print "This is the first CHECK block\n"; } INIT { print "This is the second INIT block\n"; } CHECK { print "This is the second CHECK block\n"; }
输出结果:
This is the second CHECK block This is the first CHECK block This is the first INIT block This is the second INIT block