Error: Unable to read "n": no such variable in tcl
proc rep {name} {
upvar $name n
puts "nm is $n"
}
In the above procedure, ' name
' is the parameter that is passed to the procedure named " rep
". When I run this program, I got " error : Can't read "n" : no such variable
". Can anyone tell me what might be the possible cause of this error.
source to share
This error message will be generated if the variable whose name you passed in rep
was not in the scope of the call. For example check this interactive session with 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 ...
A variable foo
is in a funny state after upvar
if it is not set; it actually exists (it is referenced in the global hash table of the global namespace variables) but contains no content, so no error tests are performed. (A variable is said to have a place when it has an entry somewhere - that is, some kind of storage to host its content - and it has a value set in that store; an unset variable could be one that has NULL
in C in Tcl itself does not support values NULL
for this reason, they correspond to non-being.)
source to share