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.
That error message would be produced if the variable whose name you passed to
repdid 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 xGoing deeper…
The variable
foois in a funny state after theupvarif 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 aNULLat the C level in that storage. The Tcl language itself does not supportNULLvalues at all for this reason; they correspond to non-existence.)