Question

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.

No correct solution

OTHER TIPS

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…

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

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

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

Hope it helps.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top