Posted to tcl by aspect at Sat Feb 28 01:08:04 GMT 2015view raw

  1.  
  2. set fd [open "|ping csiro.au" r]
  3. chan configure $fd -blocking 0
  4. chan event $fd readable [list pong $fd]
  5.  
  6. proc pong {fd} {
  7. set ln [gets $fd]
  8. if {$ln eq ""} {
  9. if {[eof $fd]} {
  10. puts "Ping died!"
  11. close $fd
  12. after idle finished
  13. return
  14. }
  15. }
  16. puts "Pong! $ln"
  17. }
  18.  
  19. after 5000 [list finished $fd]
  20.  
  21. proc finished {fd} {
  22. puts "Killing ping"
  23. catch {close $fd}
  24. exit
  25. }
  26.  
  27. vwait forever
  28.