In a Tcl script, I want to catch the return of a Tcl proc in order to execute some finalization actions. For example my code could be as follows:
proc X10 { a } { return [expr $a * 10] }
proc procF {} {
set a 13
catch {[info body X10]} __result
return $__result
}
procF
The previous code gives me an error: invalid command name " return [expr $a * 10] "
Although that replacing info body X10
with return [expr $a * 10]
works as expected. What I initially thought is that both of them are exchangeable and should give the same output. So, why the first gives an error and what is the difference between both of them ?
X10
with braces around the expression:proc X1 {a} {return [expr {$a * 10}]}
It enables Tcl to bytecode-compile it and won't blow up horribly onX1 {[puts haha;exit]}
… – Donal FellowsprocF
will return the string "__result". You probably want the invocation to bereturn $__result
or, equivalently,set __result
. – Peter Lewerin