tcl - Cannot access variable within expect_background -


i have code starts process, expects startup output, , logs rest file:

proc foo { } {     set log_fp [open "somefile" a]      exec cp $prog "$prog.elf"      spawn someprog     set someprog_spawn_id $spawn_id      # things here that wait output someprog     expect {         -i $someprog_spawn_id         -re "some output indicating successful startup"     }      # send process background     expect_background {         -i $someprog_spawn_id         full_buffer { }         eof {             wait -i $someprog_spawn_id             close $log_fp          }         -re {^.*\n} {             puts $log_fp $expect_out(buffer)         }     } } 

unfortunately, errors message:

can't read "log_fp": no such variable 

how can access variable within scope?

the expect_background callback scripts evaluated in global scope (because procedure may have finished @ point when fire) have put variable in scope well…

proc foo { } {     global log_fp     set log_fp [open "somefile" a]     # ... 

alternatively, 8.5 can tricks using apply make binding

expect_background "     -i \$someprog_spawn_id     full_buffer { }     [list eof [list apply {log_fp {         wait -i $someprog_spawn_id         close $log_fp      }} $log_fp]]     [list -re {^.*\n} [list apply {log_fp {         puts $log_fp $expect_out(buffer)     }} $log_fp]] " 

really ugly though. using global variable lot easier.


Comments

Popular posts from this blog

javascript - Jquery show_hide, what to add in order to make the page scroll to the bottom of the hidden field once button is clicked -

javascript - Highcharts multi-color line -

javascript - Enter key does not work in search box -