I saw some code in TCL like this:
namespace eval ::info {
set count 0;
set id 1;
set role admin;
namespace export *
}
proc ::info::setcount {
set ::info::count 0;
}
proc ::info::setId {
set ::info::id 1;
}
proc ::info::setRole {
set ::info::role user;
}
There are three variables defined in namespace ::info, but that three procs(setcount setId setRole) are not declared in the namespace, seems like they are defined outside of ::info, is this allowed? how does this work?
The procedures are defined in the
::infonamespace. They’re just not inside the scope of thenamespace eval, a command that just creates the namespace if necessary and then executes the given script within that context. Being executed within the context of a namespace changes howprocplaces commands that it creates when the names of those commands are not fully-qualified. The namespace exists independently of thenamespace evalcall.The variables have to be declared in the namespace though; that avoids some really nasty trouble with resolution of variables that can catch people out.
Personally, I prefer to write code like this:
But I know that other people disagree with me. Write what you prefer, as Tcl works just fine with either scheme.