错误:无法读取“n”:tcl中没有这样的变量

时间:2022-08-07 23:07:09
proc rep {name} {
    upvar $name n 
    puts "nm is $n"
}

In the above procedure, 'name' is a parameter which is passed to a procedure named 'rep'. When I run this program I got "error : Can't read "n" : no such variable". Could any one tell me what could be the possible cause for this error.

在上面的过程中,“name”是传递给名为“rep”的过程的参数。当我运行这个程序时,我得到"error: Can't read "n: no such variable"。谁能告诉我这个错误的可能原因是什么?

2 个解决方案

#1


5  

That error message would be produced if the variable whose name you passed to rep did not exist in the calling scope. For example, check this interactive session with tclsh…

如果传递给rep的变量的名称在调用范围中不存在,则将生成该错误消息。例如,使用tclsh检查这个交互式会话……

% proc rep {name} {
    upvar $name n 
    puts "nm is $n"
}
% rep foo
can't read "n": no such variable
% set foo x
x
% rep foo
nm is x

Going deeper…

The variable foo is in a funny state after the upvar if it is unset; it's actually existing (it's referenced in the global namespace's hash table of variables) but has no contents, so tests of whether it exists fail. (A variable is said to exist when it has an entry somewhere — that is, some storage to put its contents in — and it has a value set in that storage; an unset variable can be one that has a NULL at the C level in that storage. The Tcl language itself does not support NULL values at all for this reason; they correspond to non-existence.)

变量foo在upvar之后处于一个有趣的状态,如果它是未设置的;它实际上是存在的(它在全局命名空间的变量哈希表中引用),但是没有内容,所以测试它是否存在失败。(当一个变量在某个地方有一个条目时——也就是说,有一个存储来放置它的内容——它在那个存储中有一个值集;未设置变量可以是在该存储中C级具有NULL的变量。由于这个原因,Tcl语言本身根本不支持空值;它们对应于不存在。)

#2


1  

I ran into this too. I realized I was sending $foo instead of foo (note, no dollar sign).

我也遇到过这种情况。我意识到我是用foo而不是foo(注意,没有美元符号)。

% set foo 1
%
% rep $foo
can't read "foo": no such variable
%
% rep foo
nm is 1

Hope it helps.

希望它可以帮助。

#1


5  

That error message would be produced if the variable whose name you passed to rep did not exist in the calling scope. For example, check this interactive session with tclsh…

如果传递给rep的变量的名称在调用范围中不存在,则将生成该错误消息。例如,使用tclsh检查这个交互式会话……

% proc rep {name} {
    upvar $name n 
    puts "nm is $n"
}
% rep foo
can't read "n": no such variable
% set foo x
x
% rep foo
nm is x

Going deeper…

The variable foo is in a funny state after the upvar if it is unset; it's actually existing (it's referenced in the global namespace's hash table of variables) but has no contents, so tests of whether it exists fail. (A variable is said to exist when it has an entry somewhere — that is, some storage to put its contents in — and it has a value set in that storage; an unset variable can be one that has a NULL at the C level in that storage. The Tcl language itself does not support NULL values at all for this reason; they correspond to non-existence.)

变量foo在upvar之后处于一个有趣的状态,如果它是未设置的;它实际上是存在的(它在全局命名空间的变量哈希表中引用),但是没有内容,所以测试它是否存在失败。(当一个变量在某个地方有一个条目时——也就是说,有一个存储来放置它的内容——它在那个存储中有一个值集;未设置变量可以是在该存储中C级具有NULL的变量。由于这个原因,Tcl语言本身根本不支持空值;它们对应于不存在。)

#2


1  

I ran into this too. I realized I was sending $foo instead of foo (note, no dollar sign).

我也遇到过这种情况。我意识到我是用foo而不是foo(注意,没有美元符号)。

% set foo 1
%
% rep $foo
can't read "foo": no such variable
%
% rep foo
nm is 1

Hope it helps.

希望它可以帮助。