English 中文(简体)
可变范围
原标题:Variable Scope in Tcl
  • 时间:2012-01-14 01:10:28
  •  标签:
  • tcl
  • scope

I have the following basic code:

proc test {} { 
    set my_var2 3
    foreach iter {1 2 3} {
        set my_var1 4
        set my_var2 5
        puts "Inside: $my_var1 $my_var2
"  
    }
    puts "outside $my_var1, $my_var2
"    ;#WHY IT DOES NOT GIVE ERROR HERE!
}
test  ;#calling the function

该方案的产出是:

Inside: 4 5

Inside: 4 5

Inside: 4 5

outside 4, 5

Now my confusion is since my_var1 is define only in the local scope of foreach loop why its value is available even outside the loop? In other words what determines the scope of a variable in Tcl? Thanks a lot for the help!

最佳回答

http://www.tcl.tk/man/tcl/tutorial/Tcl13.html。 《技术手册》:

Tcl evaluates variables within a scope delineated by procs, namespaces [...], and at the topmost level, the global scope.

因此, for虫不会产生新的范围,其所有变数都由原体来限定。

问题回答

暂无回答




相关问题
Scope with a self-invoking function in Javascript

Take below code iterates over 6 input buttons and attaches an onclick event to every button that alerts the index number of the respective iteration: for (var i = 1; i < 6; ++i) { var but = ...

Check session from a view in CodeIgniter

What is the best way to check session from a view in CodeIgniter, it shows no way in their user guide, otherwise I will have to make two views on everything, which is kinda weird...still a newbie to ...

preventing data from being freed when vector goes out of scope

Is there a way to transfer ownership of the data contained in a std::vector (pointed to by, say T*data) into another construct, preventing having "data" become a dangling pointer after the vector goes ...

定型语言是否具有范围概念?

定型语言是否具有范围概念? 在我看来,如果职能参数被放在职能执行之前的位置上,这些参数就会以不正统的方式出现。

"Global" variable scope in PHP

I have a section of code like the following: ---- file.php ---- require_once("mylib.php"); function($a,$b) { $r = $_GLOBALS[ someGlobal ]; echo $r; } ---- mylib.php ---- $_GLOBALS[ ...

Type reference scope

I m studying databases and am currently working on a object-relational DB project and I ve encountered a small problem with the number of possible constraints in an object table. I m using "Database ...

热门标签