Namespace resolution of Variables & Procedures

The following code example illustrates how different types of namespace path addressing effects what is being accessed (Variables) or executed (Procedures).

Tom Krehbiel


 set Variable "::Variable"

 proc TestProc {} {
     puts "::proc"
 }

 namespace eval Namespace {
     namespace eval Namespace {
         variable Variable "::Namespace::Namespace Variable"
         proc TestProc {} {
             puts "::Namespace::Namespace proc"
         }
     }
     variable Variable "::Namespace Variable"
     proc TestProc {} {
         puts "::Namespace proc"
     }
     proc test0 {} {
         puts "$::Variable"
     }
     proc test1 {} {
         puts "$::Namespace::Variable"
     }
     proc test2 {} {
         puts "$Namespace::Variable"
     }
     proc test3 {} {
         ::TestProc
     }
     proc test4 {} {
         TestProc
     }
     proc test5 {} {
         Namespace::TestProc
     }
 }

 % Namespace::test0
 ::Variable

 % Namespace::test1
 ::Namespace Variable

 % Namespace::test2
 ::Namespace::Namespace Variable

 % Namespace::test3
 ::proc

 % Namespace::test4
 ::Namespace proc

 % Namespace::test5
 ::Namespace::Namespace proc