Monday 2 January 2017

TCL NAMESPACE

Introduction:

A namespace is a collection of commands and global variables that is kept apart from the usual global scope. Elements are created within a namespace using the namespace command. For example, the following namespace implements a simple counter facility:

    namespace counter {
        variable num 0

        proc next {{by 1}} {
            global num
            incr num $by
            return $num
        }
    }


The namespace contains a global variable called "num" and a procedure called "next".
Having these elements in a separate namespace prevents unwanted interactions with other elements in a program. For example, suppose we have another namespace defined like this:

    namespace symbol {
        variable num 0

        proc next {name} {
            global num
            return "$name[incr num]"
        }
    }

This namespace also contains a global variable called "num" and a procedure called "next". However, it performs a different function and is completely separate from the counter namespace defined above.
Within a namespace, we can use simple names like "num" and "next" to reference local elements. Outside of a namespace, however, we must be more specific. Command names and variable names can be qualified by the namespace that contains them. The "::" string is used as a separator between namespace names and command/variable names. For example, if we want to access the counter, we invoke:

    set val [counter::next 2]

and if we want to generate a symbol, we invoke:

    set sym [symbol::next "foo"]

A namespace can also have child namespaces within it, so one library can contain its own private copy of many other libraries. For example, the symbol namespace could contain its own private counter facility like this:

    namespace symbol {
        namespace counter {
            variable num 0

            proc next {{by 1}} {
                global num
                incr num $by
                return $num
            }
        }

        proc next {name} {
            return "$name[counter::next]"
        }
    }

The counter contained in the symbol namespace operates independently of the counter defined previously. Strictly speaking, its name is "symbol::counter", although within the context of the symbol namespace, it can be referred to simply as "counter".
The global namespace is named "::", and it is the root of all namespaces in an interpreter. To avoid confusion, any name can be fully qualified from the global namespace. Fully qualified names start with "::" and list all namespaces in the path leading to the element. For example, the fully qualified name for the num variable in the symbol::counter namespace is:

    ::symbol::counter::num

If a name does not have a leading "::", it is treated relative to the current namespace context. Namespace names are a lot like file names in the Unix file system, except that a "::" separator is used instead of "/".
Namespace definitions are additive. Another procedure could be added to the counter namespace like this:

    namespace counter {
        proc reset {} {
            global num
            set num 0
        }
    }

or like this:

    proc counter::reset {} {
        global num
        set num 0
    }

Existing procedures like counter::next can be redefined again and again in a similar manner.
A namespace can be deleted using the "delete namespace" command. This deletes all commands and variables in the namespace, and deletes all child namespaces as well.

What is namespace?
    A namespace is an encapsulated collection of commands and variables to ensure that they won’t
    Interfere with the commands and variables of other namespaces. A namespace is similar to a
    Local variable within a proc.Tcl has always had one such collection, which we refer as the
    Global namespace.

    Tcl supports many object-oriented programming constructs, including namespaces. A namespace is
    a collection of commands and variables. Namespaces are very useful for avoiding name collisions.
    In addition, most Tcl packages create their own namespace to store their procedures and variables.

 Name space example.
 namespace eval test {
 proc sum {a b} {
 set num [expr $a + $b]
 puts “The sum is: $num”
 }
 }
You can easily call a procedure within a namespace from another namespace by using the double colons (::)
test::sum 2 3

namespace eval sample {
proc sum {m n} {
  set res [expr $m + $n]
  return $res
}
}
puts [sample::sum 2 5]


For More details refer to the below URL


No comments:

Post a Comment

Note: only a member of this blog may post a comment.