Handle a signal: Difference between revisions

Content added Content deleted
No edit summary
(→‎{{header|Tcl}}: Illustrated more of 8.6's try/trap syntax)
Line 275: Line 275:


=={{header|Tcl}}==
=={{header|Tcl}}==
Core Tcl does not have signal handling. However the Expect and TclX extensions do:
Core Tcl does not have signal handling. However the [http://expect.nist.gov/ Expect] and [http://tclx.sourceforge.net/ TclX] extensions do:


Using Expect:
Using Expect:
Line 333: Line 333:
puts "infinite loop interrupted, but not on SIGINT: $::errorInfo"
puts "infinite loop interrupted, but not on SIGINT: $::errorInfo"
}
}
}</lang>
With Tcl 8.6, that would be written as:
<lang tcl>package require Tclx

signal error sigint

set start_time [clock seconds]
proc infinite_loop {} {
while 1 {
puts [incr n]
after 500
}
}
try {
infinite_loop
} trap {POSIX SIG SIGINT} {} {
puts "elapsed time: [expr {[clock seconds] - $start_time}] seconds"
}</lang>
}</lang>