Perl脚本包含多个BEGIN及END块

 众所周知,Perl中的BEGIN块在其它代码执行前执行,而END块在其它代码执行后执行。
可是,如果Perl脚本中同时包含多个BEGIN及END块呢,其执行情况如何?
看下面测试代码:

#!/usr/bin/perl -w
use strict;
END{ print "the [" . __LINE__ . "] end block\n"; }
print "The other [" . __LINE__ . "] logical\n";
BEGIN{ print "the [" . __LINE__ . "] begin block\n"; }
print "The other [" . __LINE__ . "] logical\n";
END{ print "the [" . __LINE__ . "] end block\n"; }
print "The other [" . __LINE__ . "] logical\n";
BEGIN{ print "the [" . __LINE__ . "] begin block\n"; }
print "The other [" . __LINE__ . "] logical\n";
END{ print "the [" . __LINE__ . "] end block\n"; }
print "The other [" . __LINE__ . "] logical\n";
BEGIN{ print "the [" . __LINE__ . "] begin block\n"; }
print "The other [" . __LINE__ . "] logical\n";


执行结果如下:

$./p.pl
the [5] begin block
the [9] begin block
the [13] begin block
The other [4] logical
The other [6] logical
The other [8] logical
The other [10] logical
The other [12] logical
The other [14] logical
the [11] end block
the [7] end block
the [3] end block


根据多次执行情况来看,其执行结果是唯一的,即可以认为不存在随机顺序的问题。

那么根据上面执行结果:
1、多个BEGIN块按照在脚本中出现的顺序,自上向下执行。
2、多个END块按照在脚本中出现的顺序,自下而上执行。

上述结论为实验结果,暂无官方信息佐证。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值