On Friday, 26 January 2018 10:12:54 UTC, Uwe Klein wrote:
>
> You can keep state inside the bg_expect handling
> using a fully referenced or global variable, can't you?
>
Yes, that's what I was doing in previous versions of this script, I had code like:
expect_background {
-re \\n(\[^\\r]+)\\r {
set file $expect_out(1,string)
lappend ::sid_files($expect_out(spawn_id)) $file
}
$::PROMPT {run_grep $expect_out(spawn_id)}
}
- where run_grep was another proc to run the next step in the process, which would then read from ::sid_files($spawn_id).
But when you have a sequence of such operations to run on each connection, and the results of earlier steps determine the later steps, this sort of code gets quite hairy. Switching to running a coroutine per connection makes it much cleaner.
The equivalent of the code above becomes:
co_expect $spawn_id {\n([^\r]+)\r}
while {[yield]} {
set file $::expect_out(1,string)
lappend files $file
}
foreach file $files {
...next steps...