"""Offline Perl 5 semantic verification lab for Chapter 27 (packages-modules)."""
import json
VERSION = "CPython 3.12.14 offline Perl 5 semantic verifier (perl absent)"
def case_package_symbol_tables_and_qualified_names():
symtab = {"main": {}, "Foo": {}}
cur_pkg = "main"
symtab[cur_pkg]["$i"] = 1
out = [f"包名 : {cur_pkg} {symtab[cur_pkg]['$i']}"]
cur_pkg = "Foo"
symtab[cur_pkg]["$i"] = 10
out.append(f"包名 : {cur_pkg} {symtab[cur_pkg]['$i']}")
cur_pkg = "main"
symtab[cur_pkg]["$i"] = 100
out.append(f"包名 : {cur_pkg} {symtab[cur_pkg]['$i']}")
out.append(f"包名: {cur_pkg} {symtab['Foo']['$i']}")
expected = {
"lines": [
"包名 : main 1",
"包名 : Foo 10",
"包名 : main 100",
"包名: main 10",
],
"main_i": 100,
"foo_i": 10,
}
actual = {
"lines": out,
"main_i": symtab["main"]["$i"],
"foo_i": symtab["Foo"]["$i"],
}
return {
"name": "package_symbol_tables_and_qualified_names",
"summary": "验证 main 与 Foo 独立符号表、__PACKAGE__ 切换及 $Foo::i 跨包限定访问",
"expected": expected,
"actual": actual,
"passed": actual == expected,
}
def case_begin_end_execution_order():
begin_blocks = ["BEGIN-1", "BEGIN-2"]
main_body = ["MAIN-BODY"]
end_blocks = ["END-1", "END-2"]
# Perl executes BEGIN FIFO during compile, then main body, then END LIFO on exit
timeline = list(begin_blocks) + list(main_body) + list(reversed(end_blocks))
expected = {
"timeline": ["BEGIN-1", "BEGIN-2", "MAIN-BODY", "END-2", "END-1"],
"begin_fifo": True,
"end_lifo": True,
}
actual = {
"timeline": timeline,
"begin_fifo": timeline[:2] == ["BEGIN-1", "BEGIN-2"],
"end_lifo": timeline[-2:] == ["END-2", "END-1"],
}
return {
"name": "begin_end_execution_order",
"summary": "验证 BEGIN 编译期先进先出(FIFO)执行、主体运行及 END 退出期后进先出(LIFO)顺序",
"expected": expected,
"actual": actual,
"passed": actual == expected,
}
def case_require_use_inc_and_exporter():
def module_to_relpath(mod_name: str) -> str:
return mod_name.replace("::", "/") + ".pm"
module_defs = {
"Foo": {"subs": {"bar": "Hello", "blat": "World", "splat": "Not"}, "export": ["bar", "blat"], "returns_true": True},
"BadMod": {"subs": {}, "export": [], "returns_true": False},
}
# require loads module into %INC without importing into caller namespace
inc_loaded = {}
caller_ns = {}
def do_require(mod: str) -> bool:
if not module_defs[mod]["returns_true"]:
return False
inc_loaded[module_to_relpath(mod)] = f"/usr/lib/perl5/{module_to_relpath(mod)}"
return True
def do_use(mod: str) -> bool:
if not do_require(mod):
return False
for sym in module_defs[mod]["export"]:
caller_ns[sym] = f"{mod}::{sym}"
return True
bad_ok = do_require("BadMod")
use_ok = do_use("Foo")
expected = {
"nested_relpath": "MyDirectory/MyModule.pm",
"bad_module_rejected": False,
"use_foo_ok": True,
"imported_symbols": ["bar", "blat"],
"splat_not_exported": True,
"inc_entry": "/usr/lib/perl5/Foo.pm",
}
actual = {
"nested_relpath": module_to_relpath("MyDirectory::MyModule"),
"bad_module_rejected": bad_ok,
"use_foo_ok": use_ok,
"imported_symbols": sorted(caller_ns.keys()),
"splat_not_exported": "splat" not in caller_ns,
"inc_entry": inc_loaded.get("Foo.pm"),
}
return {
"name": "require_use_inc_and_exporter",
"summary": "验证 :: 路径映射、模块末尾 1; 返回真值约束、require/use 区别与 Exporter @EXPORT 符号导出",
"expected": expected,
"actual": actual,
"passed": actual == expected,
}
def case_h2xs_scaffold_and_makefile_install_steps():
scaffold_files = [
"Person/lib/Person.pm",
"Person/Makefile.PL",
"Person/README",
"Person/t/Person.t",
"Person/Changes",
"Person/MANIFEST",
]
install_pipeline = ["perl Makefile.PL", "make", "make test", "make install"]
expected = {
"file_count": 6,
"has_makefile_pl": True,
"has_test_script": True,
"install_steps": ["perl Makefile.PL", "make", "make test", "make install"],
}
actual = {
"file_count": len(scaffold_files),
"has_makefile_pl": "Person/Makefile.PL" in scaffold_files,
"has_test_script": "Person/t/Person.t" in scaffold_files,
"install_steps": install_pipeline,
}
return {
"name": "h2xs_scaffold_and_makefile_install_steps",
"summary": "核对 h2xs -AX -n Person 标准模块骨架目录清单与 MakeMaker 四步构建安装流程",
"expected": expected,
"actual": actual,
"passed": actual == expected,
}
def main():
cases = [
case_package_symbol_tables_and_qualified_names(),
case_begin_end_execution_order(),
case_require_use_inc_and_exporter(),
case_h2xs_scaffold_and_makefile_install_steps(),
]
print(json.dumps({"version": VERSION, "cases": cases}, ensure_ascii=False, indent=2))
if __name__ == "__main__":
main()
本例需在 Windows x64、CPython 3.12.14 离线 Perl 5 语义验证器(本机未安装原生 perl 解释器)。 中运行,请复制源码、保存文件或下载工程包后在本地执行验证。
运行说明与环境独立工程
运行环境:Windows x64、CPython 3.12.14 离线 Perl 5 语义验证器(本机未安装原生 perl 解释器)。
入口文件:projects/perl-packages-modules-lab/explore-packages-modules.py
本地运行步骤快速上手
- 点击上方“保存源码”保存为
explore-packages-modules.py,或下载右侧课程工程包。 - 确认本地已准备 Windows x64、CPython 3.12.14 离线 Perl 5 语义验证器(本机未安装原生 perl 解释器)。。
- 按对应章节命令执行,结合正文核对实际输出。
附件与关联工程包下载
课程关联工程包(登记关联1章) · 含当前章节或实例 · mbskycom202612a2497.zip
课程关联工程包(登记关联29章) · 含当前章节或实例 · mbskycom202612a2480.zip