"""Offline Perl 5 semantic verification lab for Chapter 03 (environment)."""
import json
import re
import shutil
VERSION = "CPython 3.12.14 offline Perl 5 semantic verifier (perl absent)"
def case_perl_version_probe_and_parse():
sample_output = (
"This is perl 5, version 18, subversion 2 (v5.18.2) "
"built for darwin-thread-multi-2level\n"
"Copyright 1987-2013, Larry Wall"
)
m = re.search(r"perl\s+(\d+),\s+version\s+(\d+),\s+subversion\s+(\d+)\s+\((v[\d.]+)\)", sample_output)
expected = {
"local_perl_found": False,
"parsed_version": "v5.18.2",
"major_minor_patch": [5, 18, 2],
}
actual = {
"local_perl_found": shutil.which("perl") is not None,
"parsed_version": m.group(4),
"major_minor_patch": [int(m.group(1)), int(m.group(2)), int(m.group(3))],
}
return {
"name": "perl_version_probe_and_parse",
"summary": "探测本机 perl 缺席状态并解析标准 perl -v 输出版本号结构",
"expected": expected,
"actual": actual,
"passed": actual == expected,
}
def case_source_build_pipeline_paths():
steps = [
"tar -xzf perl-5.x.y.tar.gz",
"cd perl-5.x.y",
"./Configure -de",
"make",
"make test",
"make install",
]
install_bin = "/usr/local/bin"
install_lib = "/usr/local/lib/perl5"
expected = {
"step_count": 6,
"has_configure_de": True,
"has_make_test": True,
"bin_dir": "/usr/local/bin",
"lib_prefix": "/usr/local/lib/perl",
}
actual = {
"step_count": len(steps),
"has_configure_de": "./Configure -de" in steps,
"has_make_test": "make test" in steps,
"bin_dir": install_bin,
"lib_prefix": install_lib[:-1],
}
return {
"name": "source_build_pipeline_paths",
"summary": "校验 Unix/Linux 与 macOS 源码编译安装六步流程及默认安装目录",
"expected": expected,
"actual": actual,
"passed": actual == expected,
}
def case_cli_flags_and_inc_model():
cli_table = {
"-d": "debugger",
"-I": "prepend_INC",
"-T": "taint_fatal",
"-t": "taint_warn",
"-U": "unsafe_ops",
"-w": "useful_warnings",
"-W": "all_warnings",
"-X": "disable_warnings",
"-e": "inline_program",
"file": "script_file",
}
inc_paths = ["/usr/local/lib/perl5", "/usr/lib/perl5"]
custom_dir = "/opt/my_perl_lib"
effective_inc = [custom_dir] + inc_paths
expected = {
"flag_count": 10,
"taint_strict_vs_warn": ("taint_fatal", "taint_warn"),
"inc_head": "/opt/my_perl_lib",
"warning_levels": ["useful_warnings", "all_warnings", "disable_warnings"],
}
actual = {
"flag_count": len(cli_table),
"taint_strict_vs_warn": (cli_table["-T"], cli_table["-t"]),
"inc_head": effective_inc[0],
"warning_levels": [cli_table["-w"], cli_table["-W"], cli_table["-X"]],
}
return {
"name": "cli_flags_and_inc_model",
"summary": "验证 perl 命令行 10 项核心选项语义(含 -I 扩充 @INC、-T/-t 污染检查与 -w/-W/-X 警告控制)",
"expected": expected,
"actual": actual,
"passed": actual == expected,
}
def case_windows_distro_and_execution_modes():
distros = {
"Strawberry Perl": {"bundled_cpan_and_toolchain": True, "arch": ["32bit", "64bit"]},
"ActiveState Perl": {"bundled_cpan_and_toolchain": False, "arch": ["32bit", "64bit"]},
}
modes = {
"one_liner_unix": "perl -e 'print \"Hello\\n\"'",
"one_liner_windows": 'perl -e "print qq{Hello\\n}"',
"script_file": "perl script.pl",
"ide_options": ["Padre", "EPIC (Eclipse plugin)"],
}
expected = {
"strawberry_bundles_cpan": True,
"both_support_64bit": True,
"has_inline_and_file_modes": True,
"ide_count": 2,
}
actual = {
"strawberry_bundles_cpan": distros["Strawberry Perl"]["bundled_cpan_and_toolchain"],
"both_support_64bit": all("64bit" in d["arch"] for d in distros.values()),
"has_inline_and_file_modes": "-e" in modes["one_liner_unix"] and modes["script_file"].endswith(".pl"),
"ide_count": len(modes["ide_options"]),
}
return {
"name": "windows_distro_and_execution_modes",
"summary": "核对 Windows 发行版差异、命令行交互/脚本两种运行方式与 GUI IDE 选项",
"expected": expected,
"actual": actual,
"passed": actual == expected,
}
def main():
cases = [
case_perl_version_probe_and_parse(),
case_source_build_pipeline_paths(),
case_cli_flags_and_inc_model(),
case_windows_distro_and_execution_modes(),
]
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-environment-lab/explore-environment.py
本地运行步骤快速上手
- 点击上方“保存源码”保存为
explore-environment.py,或下载右侧课程工程包。 - 确认本地已准备 Windows x64、CPython 3.12.14 离线 Perl 5 语义验证器(本机未安装原生 perl 解释器)。。
- 按对应章节命令执行,结合正文核对实际输出。
附件与关联工程包下载
课程关联工程包(登记关联29章) · 含当前章节或实例 · mbskycom202612a2480.zip
课程关联工程包(登记关联1章) · 含当前章节或实例 · mbskycom202612a2488.zip