proc中的Expect_after/expect_background的行为与全局调用时的行为是否有所不同? [英] Is behavior of expect_after/expect_background in proc different than when called globally?
问题描述
我正在自动完成一些期望工作,并且有类似以下内容的东西:
I'm automating some work with expect, and have something like the following:
# procedure to set background and after patterns
proc foo {} {
expect_after {
-re $regex1 {puts "Matched regex1"; send $command_to_run; exp_continue}
timeout {exp_continue}
}
expect_background {
-re $regex2 {do other things; exp_continue}
-re $regex3 {and more different things; exp_continue}
timeout {exp_continue}
}
}
spawn $thing
foo
expect_user {
-ex "stahp" {exit}
}
在匹配expect_after
模式(并运行关联的主体)之后,该控件将无限期挂起.但是,如果将expect_after
和expect_background
模式从过程中移出,那么它将按预期运行.
This hangs indefinitely after expect_after
pattern is matched (and the associated body is run). However, if I move the expect_after
and expect_background
patterns out of the procedure, then it runs as I, well, expected.
为什么放在程序中时行为会有所不同?
Why does it behave differently when put in a procedure?
推荐答案
非常感谢glenn jackman!似乎在过程中调用expect_after
,expect_background
和可能的expect_before
时,不仅要查找处于全局范围内的spawn_id,还需要指定它.
Thanks to glenn jackman for the idea! It seems that when called in a procedure, expect_after
, expect_background
, and probably expect_before
not only look for the spawn_id which is in the global scope, but need it specified.
这有效:
proc foo {} {
namespace eval global {
expect_after {
-i $spawn_id -re $regex1 {do things}
}
expect_background {
-i $spawn_id -re $regex2 {do more different things}
-i $spawn_id ...
}
}
}
如果任何人都可以解释为什么它需要-i $spawn_id
的原因,那将是很好的,但这是针对遇到相同问题的任何人的解决方案.添加global spawn_id
也应该可以,但是我最终使用了它,因为我有大约5-6个变量,其中一半是在foo
中修改的.
If anyone can explain why it needs -i $spawn_id
that would be great, but here's a fix for anyone running into the same problem. Adding a global spawn_id
should also work, but I ended up using this as I have about 5-6 variables, half of which I modify in foo
.
这篇关于proc中的Expect_after/expect_background的行为与全局调用时的行为是否有所不同?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!