How a Perl5 program works

运行一个Perl5 程序分为两个阶段:Complication time & Run time 。

Complication time

如下图所示,由Source code最终解析成特定的数据结构 optree 。中间可以利用 BEGIN block 触发Complication time 过程中的 Run time 即中断编译,执行代码(其实是数据结构中的内容)

这里写图片描述

Run time

按照特定顺序运行 optree (optree 是一个由C定义的 tree 类型数据结构, 源于叫OP的数据结构 )中的数据。
OP:optree is a tree of C data structures all deriving from a structure called OP. Each op has a name, some flags, and zero or more children. Ops correspond to Perl 5 operations (called ppcodes) or Perl 5 data structures (scalars, arrays, hashes, et cetera).

BEGIN, CHECK, INIT 何时运行

BEGIN block: 于Complication time 运行, block 运行完回到上一编译退出点。
CHECK block:于Complication time 之后, Run time 开始前运行,LIFO 顺序执行。
INIT block:于 Run time 开始前运行。
Note: require, do, eval 文件 or block 中存在 INIT block or CHECK block, 将不被执行。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章