如果您有 Tcl 8.6+ 并且您考虑在 Tcl coroutine 之上重新建模您的脚本,您可以在几行中实现这种延续行为。这假定您从交互式 Tcl shell(dc shell?)运行脚本。
# script.tcl
if {[info procs allSteps] eq ""} {
# We are not re-entering (continuing), so start all over.
proc allSteps {args} {
yield; # do not run when defining the coroutine;
puts 1
puts 2
puts 3
yield; # step out, once first sequence of steps (1-10) has been executed
puts 4
puts 5
puts 6
rename allSteps ""; # self-clean, once the remainder of steps (11-N) have run
}
coroutine nextSteps allSteps
}
nextSteps; # run coroutine
- 将您的脚本打包到 proc 正文中 (
allSteps)。
- 在 proc 正文中:放置
yield 以指示在您的第一步之后(例如,在第 10 步之后)的保持/继续点。
- 基于
allSteps创建协程nextSteps。
- 保护
proc 和coroutine 定义,使其不会导致重新定义(当步骤未决时)
然后,启动您的交互式 shell 并运行 source script.tcl:
% source script.tcl
1
2
3
现在,进行人工审核。然后,在同一个 shell 中继续:
% source script.tcl
4
5
6
请注意,您可以多次运行整个 2 阶段序列(因为协程 proc 的自清理:rename):
% source script.tcl
1
2
3
% source script.tcl
4
5
6
再次重申:所有这些都假设您确实不从 shell 退出,并在执行审查时维护您的 shell。如果您出于某种原因需要退出 shell(或者您无法运行 Tcl 8.6+),那么 Donal 的建议是可行的方法。
更新
如果适用于您的情况,您可以使用anonymous (lambda) proc 改进实施。这简化了生命周期管理(避免重新定义、管理协程和 proc,不需要rename):
# script.tcl
if {[info commands nextSteps] eq ""} {
# We are not re-entering (continuing), so start all over.
coroutine nextSteps apply {args {
yield; # do not run when defining the coroutine;
puts 1
puts 2
puts 3
yield; # step out, once first sequence of steps (1-10) has been executed
puts 4
puts 5
puts 6
}}
}
nextSteps