user3466037
user3466037

Reputation: 21

Tcl NameSpaces: How to Keep Global Variables separate from other Namespaces?

Why doesn't this work? (I expect 'puts $top' to print "vega"):

set top vega

namespace eval ::other {set top NULL}

puts $top ;

Output:

NULL

Upvotes: 1

Views: 261

Answers (1)

glenn jackman
glenn jackman

Reputation: 247162

The answer in detail can be found here: http://tcl.tk/man/tcl8.5/TclCmd/namespace.htm#M26

Because you did not declare "top" to be a variable in that namespace, the set command continued to the global namespace to resolve the name top

You probably intended

set top vega
namespace eval ::other {
    variable top
    set top NULL
}
puts $top          ;# outputs "vega"
puts $other::top   ;# outputs "NULL"

But read that link at the top for all the wrinkles

Upvotes: 2

Related Questions