On 29/01/2014 06:03, gagan kandra wrote:
> Please tell me what is difference and what is wrong with namespace in my code.
>
The difference between [variable foo $bar] and [set foo $bar], when
called from inside a [namespace eval], depends on whether the variable
called 'foo' exists in the current namespace beforehand. If the variable
exists, they behave the same. If the variable *doesn't*, things get more
complex.
With [variable], the named variable is created in the current namespace.
This occurs even if you don't supply a value (though in that case the
variable is created in the unset state.)
With [set], the variable is looked up in the global namespace (or
wherever else the current variable resolver dictates, but the default is
simple) and if that exists, that variable is used instead. Otherwise,
the variable is created in the current namespace. The [set] command is
not special in this way; [array set], [lappend], and in fact nearly
anything that works with variables is the same; only [variable],
[upvar], [global] and [namespace upvar] are different.
Because you really do not want the craziness, use [variable] to declare
the variables in your namespaces before you use them. (This doesn't
apply to the global namespace or to local variables, which is why most
people don't notice, or — I think — to TclOO-driven namespaces which use
a custom resolver that isn't so crazy.)
Donal.
--
Donal Fellows — Tcl user, Tcl maintainer, TIP editor.