The man page of variable is in some things not very explicit.
I have some questions.
Question 1:
What is the difference between:
namespace eval foo {variable bar 12345}
and
namespace eval foo {set bar 12345}
Is "variable" about allocating space for the variable if there is
no value in the declaration?
Question 2:
I read "If the variable command is executed inside a Tcl procedure,
it creates local variables linked to the corresponding namespace
variables"
Does it also create the namespace variable? Or I need also to
execute it outside the procedure?
Question 3
I want to source a script, perhaps inside a name space, perhaps not,
and I want instead of global, variables only visible in the name space.
Is the solution to change "global var" with "namespace var"?
The docs answer that question for you. Note last sentence in the quote
below from the namespace manpage.:
You can access a namespace variable from a procedure in the same
namespace by using the variable command. Much like the global
command, this creates a local link to the namespace variable. If
necessary, it also creates the variable in the current namespace and
initializes it.
Thanks a lot, Rich!
On Sun, 22 Feb 2026, Rich wrote:
The docs answer that question for you. Note last sentence in the quote
below from the namespace manpage.:
You can access a namespace variable from a procedure in the same
namespace by using the variable command. Much like the global
command, this creates a local link to the namespace variable. If
necessary, it also creates the variable in the current namespace and
initializes it.
Very interesting!
Also the two sentences that follow:
"Note that the global command only creates links to variables in
the global namespace. It is not necessary to use a variable command
if you always refer to the namespace variable using an appropriate
qualified name."
I suppose the phrase "links to variables in the global namespace"
in the first sentence means "links to variables relative to
the global namespace".
If I have a variable v in the namespace name, then
"global ::name::v" and "variable v" inside a procedure in
"namespace eval name" will do the same.
It seems the command "variable" is necessary because "global" is
unable to deal with paths relative to the namespace.
What is the difference between:
namespace eval foo {variable bar 12345}
and
namespace eval foo {set bar 12345}
Do not create both namespace variable?
If I have a variable v in the namespace name, then
"global ::name::v" and "variable v" inside a procedure in
"namespace eval name" will do the same.
The point of "global" is to make a "local proc variable" link to a global
of the same name. ::name::v is not a "local proc variable". Trying to
do that won't make the link.
On 22/02/2026 23:57, Roderick wrote:
What is the difference between:
namespace eval foo {variable bar 12345}
and
namespace eval foo {set bar 12345}
Do not create both namespace variable?
On Tcl 8 it depends. If there is already a global variable "bar", your second
command will change the value of the global variable, rather than create a namespace variable:
% set bar 42
42
% namespace eval foo {set bar 12345}
12345
% set bar
12345
% info exists ::foo::bar
0
See tip #278: https://core.tcl-lang.org/tips/doc/trunk/tip/278.md
Schelte.
On Mon, 23 Feb 2026, Rich wrote:
If I have a variable v in the namespace name, then
"global ::name::v" and "variable v" inside a procedure in
"namespace eval name" will do the same.
The point of "global" is to make a "local proc variable" link to a global
of the same name. ::name::v is not a "local proc variable". Trying to
do that won't make the link.
I meant this:
% namespace eval nn {proc pp {} {global ::nn::v; set v 1}}
% nn::pp
1
% set nn::v
1
Continue with a test of a non-existant namespace:
% namespace eval nn2 {proc pp2 {} {global ::nn3::v; set v 1}}
% set nn3::v
can't read "nn3::v": no such variable
% nn2::pp2
can't access "::nn3::v": parent namespace doesn't exist
On Mon, 23 Feb 2026, Rich wrote:
Continue with a test of a non-existant namespace:
% namespace eval nn2 {proc pp2 {} {global ::nn3::v; set v 1}}
% set nn3::v
can't read "nn3::v": no such variable
% nn2::pp2
can't access "::nn3::v": parent namespace doesn't exist
# tclsh
% global ::nn3::v
% % set ::nn3::v 1
invalid command name "%"
% variable ::nn3::v
can't define "::nn3::v": parent namespace doesn't exis
I think, that is a difference between global and variable.
The last do allocate memory, the first only signalizes that
a local var in procedure is bound to other var that perhaps
does not exist yet.
| Sysop: | Amessyroom |
|---|---|
| Location: | Fayetteville, NC |
| Users: | 59 |
| Nodes: | 6 (0 / 6) |
| Uptime: | 00:02:05 |
| Calls: | 812 |
| Calls today: | 2 |
| Files: | 1,287 |
| D/L today: |
20 files (23,248K bytes) |
| Messages: | 210,076 |