ucatch

Richard Suchenwirth 2006-02-08 - Debugging a Tcl script which deals with Unicodes can be difficult if you see error messages on stdout, but all characters not representable in your encoding system is converted to "?". In such a situation, I wrote the following catch deunicodifier:

 proc ucatch {body {_var ""}} {
     if {$_var ne ""} {upvar 1 $_var var}
     set code [catch {uplevel 1 $body} var]
     if {$code == 1} {set var [u2x $var]}
     return $code
 }
 proc u2x str {
     set res ""
     foreach c [split $str ""] {
         scan $c %c int
         append res [expr {$int>127? [format \\u%04X $int]: $c}]
     }
     set res
 }

#------------ Test: first with a regular catch

 % set a(\u1234) hello
 % if [catch {puts $a(\u1235)} res] {puts $res}
 can't read "a(?)": no such element in array

Now comes the ucatch, admittedly more helpful:

 % if [ucatch {puts $a(\u1235)} res] {puts $res}
 can't read "a(\u1235)": no such element in array

Very clever, no? I beamed. But in came William of Occam, razor in hand. "Why three things, where two do the same job?", he asked. "How about

 % if [catch {puts $a(\u1235)} res] {puts [u2x $res]}

?"

I had to agree. Simplicity is sometimes more difficult than one expects...


Category Debugging | Arts and crafts of Tcl-Tk programming