Capture and $ variables

After a condition matches, message fields can be **captured** as `$variables` and reused in later returns (`$token`, `$id`). Header `# capture` sets defaults; builtins like `$now` are also available.

自动 capture

消息条件匹配成功后,按 # capture 写入 连接级 vars(见 8.4):

  • # capture *:JSON 顶层每个 field → $field
  • # capture id,token:仅列出字段
  • # capture off:不自动 capture

返回中使用 $变量

在 JSON、管道、template 中展开:{"type":"res","id":"$id"} compile 阶段引用未定义变量 → E003;runtime 未定义 → 空串(simulate 与 validate 一致)

内置变量(v1 冻结,见第十八节)

$now、$nowIso、$uuid、$connectionId 等;与 capture 变量共存,同名时 **capture 覆盖内置**。

变量与 scope 分离

  • pop scope 不删除 连接级 vars($token 在 logout 后仍保留,除非新消息 capture 覆盖)
  • scope 仅控制:匹配子树、--@loop timer
  • 若 logout 需清变量:在返回管道中 set 或依赖后续 capture

内置变量与 capture 生命周期

内置变量(读取时求值)

变量 含义
$now Unix 毫秒字符串
$nowIso ISO-8601 UTC
$uuid 随机 UUID v4
$connectionId 本连接 ID

capture 变量

来自最近命中消息条件的 JSON 顶层(及 # capture 列表);写入 vars。 同名:capture 覆盖内置

生命周期

vars 存活于整连接;pop scope 不删 vars。 连接关闭清空 vars。