Reputation: 3103
It involves TCL 8.6, and is derived from answer I got on issue Nitpicking: Tcl storing in a variable or not, for speed, in procedures So, consider this code:
proc me1 { in1 in2 } {
if { $in1 > $in2 } {
return [ k $in1 ]
} else {
return [ k2 $in2 $in1 ]
}
}
proc me { in1 in2 } {
expr { $in1 > $in2 ? [ k $in1 ] : [ k2 $in2 $in1 ] }
}
proc k {in1 } {
puts "returnal should be \"0${in1}\""
return "0${in1}"
}
proc k2 { in1 in2 } {
return "01${in1}00 ${in2}"
}
It would appear that procs me and me1 should return the same value (they actually do, if similar code was written in Ruby or PERL). This is decidedly not the case in TCL. Consider now the output of me and m1 (remember that if k is called, the "expected return" is printed out):
puts [ me 23 0 ]
>returnal should be "023"
>19
puts [ me1 23 0 ]
>returnal should be "023"
>023
So, expr and/or the ternary are doing something "under the hood", by processing the "returnal". The question are:
1. What does it do?
2. How can it be disabled?
Upvotes: 1
Views: 261
Reputation: 137577
So, expr and/or the ternary are doing something "under the hood", by processing the "returnal". The question are:
- What does it do?
- How can it be disabled?
This isn't the ternary, but rather the general semantics of expr
. That command is documented and defined to return a normalised numeric value if at all possible, and the normalisation does things like converting numeric values into decimal. For example, it would convert 023
(which looks like an octal value) into 19
(= 2×81+3×80).
You can't change expr
in this area; it's a defined part of the semantics (which might be removed in a future version, but who wants to just wait around for that?). If it is a problem, you need to use the if
command version you already have as that doesn't have the convert-to-numeric-if-possible semantics.
Upvotes: 2