"""Offline Perl 5 semantic verification lab for Chapter 29 (embedded-documentation)."""
import json
import re
VERSION = "CPython 3.12.14 offline Perl 5 POD semantic verifier (perl/pod2html absent)"
def split_perl_and_pod(source: str) -> tuple[list[str], list[str]]:
"""Separate active Perl code lines from embedded POD blocks (=cmd .. =cut)."""
code_lines = []
pod_lines = []
in_pod = False
for line in source.splitlines():
if not in_pod:
if re.match(r"^=[a-zA-Z]", line):
in_pod = True
pod_lines.append(line)
else:
code_lines.append(line)
else:
pod_lines.append(line)
if line.strip() == "=cut":
in_pod = False
return code_lines, pod_lines
def case_pod_parser_cut_and_perl_ignore():
src = (
'#!/usr/bin/perl\n\n'
'print "Hello, World\\n";\n\n'
'=head1 Hello, World 实例\n\n'
'这是一个 Perl 的简单实例。\n\n'
'=cut\n\n'
'print "Hello, BlueSky\\n";\n'
)
code_lines, pod_lines = split_perl_and_pod(src)
prints = [m.group(1) for ln in code_lines for m in [re.search(r'print\s+"([^"]+)\\n";', ln)] if m]
expected = {
"executed_outputs": ["Hello, World", "Hello, BlueSky"],
"pod_ignored_by_perl": True,
"pod_starts_head1": True,
"pod_ends_cut": True,
}
actual = {
"executed_outputs": prints,
"pod_ignored_by_perl": all("简单实例" not in ln for ln in code_lines),
"pod_starts_head1": pod_lines[0].startswith("=head1 "),
"pod_ends_cut": pod_lines[-1].strip() == "=cut",
}
return {
"name": "pod_parser_cut_and_perl_ignore",
"summary": "验证 Perl 词法器忽略 =head1 至 =cut 间 POD 文档并正常执行前后代码",
"expected": expected,
"actual": actual,
"passed": actual == expected,
}
def case_end_and_data_filehandle_semantics():
src = (
'print "Hello, World\\n";\n'
'while(<DATA>) { print $_; }\n'
'__END__\n'
'\n'
'=head1 Hello, World 实例\n'
'这是一个 Perl 的简单实例。\n'
'print "Hello, BlueSky\\n";\n'
)
code_part, data_part = src.split("__END__\n", 1)
# Without reading <DATA>, only pre-__END__ print runs
without_data_out = ["Hello, World"]
# With while(<DATA>), lines after __END__ are echoed verbatim as data
with_data_lines = ["Hello, World"] + data_part.splitlines()
expected = {
"without_data_count": 1,
"with_data_first": "Hello, World",
"with_data_contains_pod_line": True,
"trailing_print_not_executed": True,
}
actual = {
"without_data_count": len(without_data_out),
"with_data_first": with_data_lines[0],
"with_data_contains_pod_line": "=head1 Hello, World 实例" in with_data_lines,
"trailing_print_not_executed": 'print "Hello, BlueSky\\n";' not in code_part,
}
return {
"name": "end_and_data_filehandle_semantics",
"summary": "验证 __END__/__DATA__ 终止编译并决定通过 <DATA> 句柄读取或忽略后续文本的行为",
"expected": expected,
"actual": actual,
"passed": actual == expected,
}
def case_pod_three_paragraph_types_and_commands():
pod_doc = (
"=encoding utf-8\n\n"
"=pod\n\n"
"=head1 概述\n\n"
"使用 B<粗体> 与 C<代码> 格式化普通段落。\n\n"
" my $x = 42;\n"
" print $x;\n\n"
"=over 4\n\n"
"=item * 第一项\n\n"
"=back\n\n"
"=cut\n"
)
paras = [p for p in re.split(r"\n\s*\n", pod_doc.strip()) if p.strip()]
classified = []
for p in paras:
if p.startswith("="):
classified.append("command")
elif p.startswith((" ", "\t")):
classified.append("verbatim")
else:
classified.append("ordinary")
known_cmds = [
"=pod", "=head1", "=head2", "=head3", "=head4",
"=over", "=item", "=back", "=begin", "=end", "=for", "=encoding", "=cut",
]
expected = {
"paragraph_types": ["command", "command", "command", "ordinary", "verbatim", "command", "command", "command", "command"],
"has_all_three_types": True,
"known_command_count": 13,
}
actual = {
"paragraph_types": classified,
"has_all_three_types": set(classified) == {"command", "ordinary", "verbatim"},
"known_command_count": len(known_cmds),
}
return {
"name": "pod_three_paragraph_types_and_commands",
"summary": "验证 POD 普通段落、原文缩进段落与命令段落三类语法识别及 13 条核心命令清单",
"expected": expected,
"actual": actual,
"passed": actual == expected,
}
def case_pod2html_and_begin_end_html_block():
pod_html = (
"=encoding utf-8\n\n"
"=head1 蓝色天空\n\n"
"=begin html\n\n"
"<h1>蓝色天空</h1>\n"
"<p>https://mbsky.example.com</p>\n\n"
"=end html\n\n"
"=cut\n"
)
# Minimal offline pod2html renderer for =head1 and =begin html .. =end html
toc = []
body = []
in_raw_html = False
unclosed_begin = False
for para in re.split(r"\n\s*\n", pod_html.strip()):
if para.startswith("=head1 "):
title = para[7:].strip()
toc.append(f'<li><a href="#{title}">{title}</a></li>')
body.append(f'<h1 id="{title}">{title}</h1>')
elif para.strip() == "=begin html":
in_raw_html = True
elif para.strip() == "=end html":
in_raw_html = False
elif in_raw_html:
body.append(para)
unclosed_begin = in_raw_html
# Also test invalid snippet where =begin html is followed by =cut without =end html
bad_pod = "=begin html\n\n=head1 Title\n\n=cut\n"
bad_unclosed = ("=begin html" in bad_pod) and ("=end html" not in bad_pod)
expected = {
"toc_link_generated": True,
"raw_html_preserved": True,
"valid_begin_closed": False,
"detects_missing_end_html": True,
}
actual = {
"toc_link_generated": '<li><a href="#蓝色天空">蓝色天空</a></li>' in toc,
"raw_html_preserved": any("<h1>蓝色天空</h1>" in b for b in body),
"valid_begin_closed": unclosed_begin,
"detects_missing_end_html": bad_unclosed,
}
return {
"name": "pod2html_and_begin_end_html_block",
"summary": "验证离线 pod2html 索引与标题生成、=begin html/=end html 原文直通及缺配对 =end 检出",
"expected": expected,
"actual": actual,
"passed": actual == expected,
}
def main():
cases = [
case_pod_parser_cut_and_perl_ignore(),
case_end_and_data_filehandle_semantics(),
case_pod_three_paragraph_types_and_commands(),
case_pod2html_and_begin_end_html_block(),
]
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-embedded-documentation-lab/explore-embedded-documentation.py
本地运行步骤快速上手
- 点击上方“保存源码”保存为
explore-embedded-documentation.py,或下载右侧课程工程包。 - 确认本地已准备 Windows x64、CPython 3.12.14 离线 Perl 5 语义验证器(本机未安装原生 perl 解释器)。。
- 按对应章节命令执行,结合正文核对实际输出。
附件与关联工程包下载
课程关联工程包(登记关联29章) · 含当前章节或实例 · mbskycom202612a2480.zip
课程关联工程包(登记关联1章) · 含当前章节或实例 · mbskycom202612a2487.zip