02.03.2014 Views

Tornado

Tornado

Tornado

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

<strong>Tornado</strong> 2.0<br />

User’s Guide<br />

For example, in <strong>Tornado</strong> tools, events are represented in Tcl as structured strings.<br />

The first element of the string is the name of the event. Tcl scripts that handle<br />

events can search for the appropriate procedure to handle a particular event by<br />

mapping the event name to a procedure name, and calling that procedure if it<br />

exists. The following Tcl script demonstrates this approach:<br />

proc shEventDispatch {event} {<br />

set handlerProc "[lindex $event 0]_Handler"<br />

}<br />

if {[info procs $handlerProc] != ""} {<br />

$handlerProc $event<br />

} {<br />

#event has no handler--do nothing.<br />

}<br />

B.2.8 Control Structures<br />

Tcl provides all the popular control structures: conditionals (if), loops (while, for,<br />

and foreach), case statements (switch), and explicit variable-scope control (global,<br />

upvar, and uplevel variable declarations). By using these facilities, you can even<br />

define your own control structures. While there is nothing mysterious about these<br />

facilities, more detailed descriptions are beyond the scope of this summary. For<br />

detailed information, see the books cited in the introduction to B.2 A Taste of Tcl,<br />

p.318.<br />

B.2.9 Tcl Error Handling<br />

Every Tcl procedure, whether built-in or script, normally returns a string. Tcl<br />

procedures may signal an error instead: in a defined procedure, this is done with<br />

the error command. This starts a process called unwinding. When a procedure<br />

signals an error, it passes to its caller a string containing information about the<br />

error. Control is passed to the calling procedure. If that procedure did not provide<br />

for this possibility by using the Tcl catch command, control is passed to its caller in<br />

turn. This recursive unwinding continues until the top level, the Tcl interpreter, is<br />

reached.<br />

As control is passed along, any procedure can catch the error and take one of two<br />

actions: signal another error and provide error information, or work around the<br />

error and return as usual, ending the unwinding process.<br />

At each unwinding step, the Tcl interpreter adds a description of the current<br />

execution context to the Tcl variable errorInfo. After unwinding ends, you can<br />

324

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!