Why do Tcler suggest to brace your `expr`essions?

后端 未结 3 1409
我寻月下人不归
我寻月下人不归 2020-11-28 12:03

We can evaluate the two expression in two possible ways:

   set a 1
   set b 1
   puts [expr $a + $b ]
   puts [expr {$a + $b } ]

But why h

3条回答
  •  有刺的猬
    2020-11-28 12:52

    • Without the braces, the parameters of expr are converted first to string, and then back again to numbers.
    • Without braces, they are prone to injection attacks very similar to SQL injection attacks.
    • You can get rounding errors you wouldn't want if you don't use braces.
    • With the braces, the expressions can be compiled.

    I based this on Johannes Kuhn's answer which was posted a while back, and you can find out in numbers, how the braced functions are more efficient on the wiki, along with other interesting stuff about the differences and where you can omit the braces to actually get the results you want.

提交回复
热议问题