1/09/2022

Namespace In TCL


 

In this article we will discuss about namespace in TCL.

Namespace :

  • A namespace allows you to place a bunch of code under the hood of a name.
  • Thus any naming conflicts with Classes, Functions and Constants etc are avoided between two namespaces.
  • It allows your code to live in that namespace.
  • Hence encapsulation of data & functions are done in various programming languages.
  • Namespace Hierarchy Separator is Generally “::” 

Namespace Hierarchy Visualization :

We will understand the namespace concept using the below info-graphics !


The outer most space , as shown in above picture, is called the Built-In-Namespace. When you start writing any code this is the namespace that you writing the code in. This is true for any programming language , not only TCL. Then one level down , there comes the Global Namespace which is specified by you explicitly. If you go a further level down , you reach up to the Local Namespace. Now, you may go any further level down . Then the levels will be Local Namespace -1 , Local Namespace-2 ......... and so on. You may nominate the nomenclature of these local namespaces as per your own convention.

Namespace Creation in TCL :

To create any namespace of your own , you have to follow the syntax :

namespace eval <Your-Name-space-name>

Here is an example of an namespace with purpose as an up-counter :

namespace eval Counter {

   variable num 1

   proc increase { } {

      variable num

      return [incr num]

   }

}


As you can see we can define variable and procedures exclusively for that namespace ! Here in the above example, the  procedure increase does increases the counter upwards.

Now , at some later part of the code if we decide to include a procedure in the namespace, we do not have to go back and edit it ! Instead we can do it by writing the  code snippet  below :

namespace eval Counter {

   variable clock 5

   proc hello { } {

      puts "Hello World"

   }

   }

You can see this block is similar to the previous block , only difference is that this block appends more information to the namespace we have defined earlier. 

Important to remember that this block does not re-define or replace the existing namespace declared above !

Once these namespace are defined, we can use the procedures declared inside them as :

Counter::increase and Counter::hello

The entire code is combined in the below snippet :

While executed , it gives the below output !


Various puts statements were inserted to identify the various code-blocks of namespace both in and out ! 

Namespace :: Hierarchy :: Code Example

We have the hierarchy visualization in previous section . Now we will take a code example to realise the hierarchy.

Here we create the topmost global namespace as : namespace eval top {}

Within which we define our local namespace as : namespace eval within {}

The two names top and within are self-explanatory w.r.to the purpose of this section.

Here goes our entire code snippet for better understanding :

Which gives the output as :


So , the change of the hierarchical data is done by : set ::top::test 25

And the hierarchical access of procedure is done by : ::top::within::hello

In the above code example.

Namespace :: Export/Import :: Code Example 

If we want to export any data from withing any namespace , the data transferred to the namespace immediately outside as shown by the first info graphics in this article. 

So this export or import operation is very relative and not absolute !

To export everything from current scope to the namespace immediately outside , we can use : namespace export * 

To import from an existing namespace we can use : namespace import <name-space-name>::* 

To delete everything that we have just imported , we can use : namespace forget space::*

We can combine all these commads in the below example :

Which , upon execution , gives the below output :



The entire article is well narrated in the below video :



Courtesy : Image by www.pngegg.com