"""Offline Perl 5 semantic verification lab for Chapter 07 (scalars)."""
import json
VERSION = "CPython 3.12.14 offline Perl 5 semantic verifier (perl absent)"
def case_numeric_scalar_literals():
integer = 200
negative = -300
floating = 200.340
bigfloat = -1.2e-23
octal = int("0377", 8)
hexa = int("0xff", 16)
expected = {
"integer": 200,
"negative": -300,
"floating_fmt": "200.34",
"bigfloat_fmt": "-1.2e-23",
"octal": 255,
"hexa": 255,
}
actual = {
"integer": integer,
"negative": negative,
"floating_fmt": f"{floating:g}",
"bigfloat_fmt": f"{bigfloat:g}",
"octal": octal,
"hexa": hexa,
}
return {
"name": "numeric_scalar_literals",
"summary": "校验十进制整数、负数、浮点数、科学计数法及八进制 0377 / 十六进制 0xff 标量表示",
"expected": expected,
"actual": actual,
"passed": actual == expected,
}
def case_string_and_scalar_ops():
var = "字符串标量 - 蓝色天空!"
quote_single = "我在单引号内 - $var"
quote_double = f"我在双引号内 - {var}"
escape_tab = "转义字符使用 -\tHello, World!"
s = "hello" + "world"
num = 5 + 10
mul = 4 * 5
mix = s + str(num)
expected = {
"single_no_interp": "我在单引号内 - $var",
"double_interp": "我在双引号内 - 字符串标量 - 蓝色天空!",
"has_tab": True,
"concat_str": "helloworld",
"num_add": 15,
"num_mul": 20,
"mix_concat": "helloworld15",
}
actual = {
"single_no_interp": quote_single,
"double_interp": quote_double,
"has_tab": "\t" in escape_tab,
"concat_str": s,
"num_add": num,
"num_mul": mul,
"mix_concat": mix,
}
return {
"name": "string_and_scalar_ops",
"summary": "验证字符串单双引号插值差异、点号(.)拼接数字与字符串以及算术运算语义",
"expected": expected,
"actual": actual,
"passed": actual == expected,
}
def case_multiline_and_special_tokens():
multiline_single = "\n蓝色天空\n —— 专注编程与实践!\n"
heredoc = "蓝色天空\n —— 专注编程与实践!\n"
token_values = {
"__FILE__": "demo.pl",
"__LINE__": 4,
"__PACKAGE__": "main",
}
inside_quotes = "__FILE__ __LINE__ __PACKAGE__"
expected = {
"single_stripped_equals_heredoc": True,
"file_token": "demo.pl",
"line_token": 4,
"package_token": "main",
"not_interpolated_in_string": "__FILE__ __LINE__ __PACKAGE__",
}
actual = {
"single_stripped_equals_heredoc": multiline_single.lstrip("\n") == heredoc,
"file_token": token_values["__FILE__"],
"line_token": token_values["__LINE__"],
"package_token": token_values["__PACKAGE__"],
"not_interpolated_in_string": inside_quotes,
}
return {
"name": "multiline_and_special_tokens",
"summary": "验证单引号与 Here-Doc 多行文本以及 __FILE__/__LINE__/__PACKAGE__ 独立编译期标记规则",
"expected": expected,
"actual": actual,
"passed": actual == expected,
}
def case_vstring_codepoints():
def build_vstring(*codepoints):
return "".join(chr(cp) for cp in codepoints)
smile = build_vstring(9786)
foo = build_vstring(102, 111, 111)
martin = build_vstring(77, 97, 114, 116, 105, 110)
needs_wide_warning = any(ord(ch) > 255 for ch in smile)
expected = {
"smile_codepoint": "U+263A",
"smile_wide_warning": True,
"foo": "foo",
"martin": "Martin",
}
actual = {
"smile_codepoint": f"U+{ord(smile):04X}",
"smile_wide_warning": needs_wide_warning,
"foo": foo,
"martin": martin,
}
return {
"name": "vstring_codepoints",
"summary": "验证 v9786、v102.111.111 与 v77.97.114.116.105.110 码点构造及宽字符输出警告条件",
"expected": expected,
"actual": actual,
"passed": actual == expected,
}
def main():
cases = [
case_numeric_scalar_literals(),
case_string_and_scalar_ops(),
case_multiline_and_special_tokens(),
case_vstring_codepoints(),
]
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-scalars-lab/explore-scalars.py
本地运行步骤快速上手
- 点击上方“保存源码”保存为
explore-scalars.py,或下载右侧课程工程包。 - 确认本地已准备 Windows x64、CPython 3.12.14 离线 Perl 5 语义验证器(本机未安装原生 perl 解释器)。。
- 按对应章节命令执行,结合正文核对实际输出。
附件与关联工程包下载
课程关联工程包(登记关联29章) · 含当前章节或实例 · mbskycom202612a2480.zip
课程关联工程包(登记关联1章) · 含当前章节或实例 · mbskycom202612a2501.zip