I am puzzled at TCL's variable scope rule, here is an experiment:
if {2 > 1} {
set x 100
}
proc p1 {} {
set y 200
}
puts $x
p1
puts $y
Running the script gives me:
100
can't read "y": no such variable
while executing
"puts $y"
This puzzled me: the error about $y makes sense because y is defined inside proc, so it is not accessible outside the proc. The problem is x, why is it accessible? It is defined in a nesting script of "if" command.
From my habitual thinking from C++ point of view, it does not make sense. Does TCL have special treatment of command "proc" so that the variables declared in its nesting script are dealt differently from others, such as "if", "for", etc.?
BTW I know how to make it work, just want to understand the TCL rules on variable scope.
uplevel, it will also behave the same way as theifcommand. e.g.proc p1 {} { uplevel { ... } }.