Perl指令碼包含多個BEGIN及END塊

2021-06-07 01:20:53 字數 1112 閱讀 4696

眾所周知,perl中的begin塊在其它**執行前執行,而end塊在其它**執行後執行。

可是,如果perl指令碼中同時包含多個begin及end塊呢,其執**況如何?

看下面測試**:

#!/usr/bin/perl -w

use strict;

endprint "the other [" . __line__ . "] logical\n";

begin

print "the other [" . __line__ . "] logical\n";

endprint "the other [" . __line__ . "] logical\n";

begin

print "the other [" . __line__ . "] logical\n";

endprint "the other [" . __line__ . "] logical\n";

begin

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塊按照在指令碼中出現的順序,自下而上執行。

上述結論為實驗結果,暫無官方資訊佐證。

關於Perl語言中BEGIN和END的使用

關於perl語言中begin和end的使用 perl語言學習者必須關注 很強實用性。有些時候我們需要在perl業務 開始執行之前做一些初始化工作,或者在結束的時候做一些收尾的工作,perl語言提供了這樣的塊begin和end幫助perl程式設計師實現這些操作。下面我先看乙個簡單的例子做為begin和...

perl 測試指令碼

修改日期 2010年01月09日 1.系統環境 系統版本 fedora6 x86 64 db2版本 db2 v9.5.0.3 perl版本 v5.8.8 系統自帶 perl dbi版本 perl dbi 1.52 1.fc6 系統自帶 dbd db2版本 dbd db2 1.76 2.安裝dbd d...

perl指令碼練習

題目 12 34 56 78 24 65 87 90 76 11 67 87 100 89 78 99 求出文字中數字的最大 小值 我看很多的朋友都用shell的awk實現了,但本人對awk不是很了解,試著用perl簡單的寫了一下,也能實現 code usr bin perl w use stric...