Reputation: 1066
I have two procs in the same namespace
proc myNamespace::function {} {
set myVar "Hello"
}
proc myNamespace::otherFunction {} {
global myVar
puts $myVar
}
Now for some reason this does not work. I have tried using upvar #0
to make myVar
a global variable. I have tried doing global myNamespace::myVar
in otherFunction
, nothing seems to work. I just want to be able to have myVar
be a variable in the myNamespace
namespace. How can I accomplish this?
Upvotes: 0
Views: 207
Reputation: 13252
Try
proc myNamespace::function {} {
variable myVar
set myVar "Hello"
}
proc myNamespace::otherFunction {} {
variable myVar
puts $myVar
}
Variables created within a command are always local variables unless they are expressly declared to be namespace variables (with the variable
command) or global variables (with the global
command). Tcl doesn't use lexical scoping, so a command never just picks up variables from the surrounding scope. It is also possible to use namespace or global variables without declarations if one uses namespace-qualified names such as ::foo::bar
(the namespace variable bar
in the foo
namespace which is a child of the ::
namespace) or ::baz
(the global variable baz
).
Documentation: global, proc, set, variable
Upvotes: 3