Saturday, 15 January 2011

Bad formatted expr returning empty string in Tcl -



Bad formatted expr returning empty string in Tcl -

this may sound silly. bear me. while playing around expr, came across next scenario

proc badexpr { b } { homecoming expr $a+$b } proc goodexpr { b } { homecoming [ expr {$a+$b} ] } puts "bad look result : --->[ badexpr 1 3 ]<-----" puts "good look result : [ goodexpr 1 3 ]"

output:

bad look result : ---><----- look result : 4

as can see, proc badexpr returning empty string. out of curiosity, interested know why returning empty string ?

funnily enough, badexpr this:

proc badexpr { b } { homecoming [expr $a+$b] }

what you've actually got is weirdexpr:

proc badexpr { b } { homecoming expr $a+$b }

what do? sets things in result dictionary. tcl results should considered triple of 3 things: result code (a little integer, in case 0 ok — 1 thrown error, , there few other ones can ignore) result value (what return) , result dictionary. result dictionary used errors, hold things stack trace (used populate errorinfo global variable), line number on error occurred, computer-readable description of error (used populate errorcode global variable), etc., can hold old thing. , you're putting odd (because that's how return works); system-defined keys begin - create them ordinary options (and dictionary called alternative dictionary).

let's show using two-variable form of catch (the sec variable catches dictionary):

% proc weirdexpr { b } { homecoming expr $a+$b } % grab {weirdexpr 1 3} p q 0 % puts $p % puts $q expr 1+3 -code 0 -level 0

yes, there's funny entry in there called expr value 1+3

tcl expr

No comments:

Post a Comment