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.

+3


source to share


2 answers


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.)

+5


source


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

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

      



Hope it helps.

+1


source







All Articles