The TCL interpreter manages all commands and global variables in groups. These groups are called namespaces, and commands and variables in one namespace do not affect another namespace. These namespaces are tree-organized, and one namespace command can be introduced into another namespace. The root of a namespace tree is the global namespace, which contains all the commands and variables that are not explicitly created in other namespaces.
Any commands and variables in a namespace that already exists can be created from within or outside the namespace. This can be done by prefixing the name of the command or variable with a namespace prefix, separated by Namespace::var between the prefix and the name. If the name of the global namespace is an empty string, it is usually written only: Var.
The primary use of namespaces is as a mechanism for creating related named packages. Namespaces can help create collection commands, and group public APIs in a command space into a pattern of public commands plus subcommands.
L namespace eval: Get the name of the namespace
If you set a new variable in the namespace, use the following command:
L namespaceDelete: -- Remove namespace
L variable command : Set or access the variables of the namespace
Creates a variable in the current namespace and sets it if the value of the variable is given. If it is processed in one procedure of that namespace, it also makes the variable of the specified name invisible in the process.
Output Result:
Note: always use the variable command to declare variables. In a namespace, if you access a variable that is not explicitly declared in the namespace, Tcl first looks for the variable of that name in the global variable. If such a global variable is found, TCL uses it instead of creating a namespace variable. This behavior of Tcl language seems inconvenient at first, in fact this design is in each namespace, can easily access the predefined global variables argv, env and so on.
The variable command cannot initialize the value of an array, but it can set variables in the namespace that allow the procedure to access them. That is, the array needs to be initialized in a separate step.
Output Result:
L namespace qualifiers command : Gets the namespace portion of the qualified name
L namespace tail command : --Gets the trailing namespace of the qualified name
To make each part name a qualified name, just use the text:: Concatenate them together. Note: If you keep the namespace in a variable, you need to replace the variable in the following form, otherwise the $ variable is replaced by: it causes the parsing of the error.
L Namespace export, namespace import command : -- Import Export command
Output Result:
Note:
The global namespace does not export any commands by default, and this namespace is customarily left to the application script management. The library should not export commands from the global namespace, nor should it import commands to the global command space.
L namespace forget command : --Prevent deletion of namespaces you create
The Namespaceimport command has the meaning of a snapshot: It imports only commands that are in an exportable state when it is called. Also, by default, it does not overwrite commands that already exist, even if they were previously imported. You can use the –force option to force a specified overlay. If you want to remove the previously imported commands from the namespace, and you are afraid to delete the namespaces you created, you should use the namespace forget command.
L namespace current command : --Gets the name of the present namespace
L namespace The parent command: --Gets the current namespace's parents namespace
L namespace childred command : --Gets the sub-namespace of the current namespace
L info commond command : --View commands and variables in the namespace
L namespace which command : - get fully qualified name
L namespace Origin command : --Get the source of the namespace
Note:
Namespaceorigin: You can view the original source of the command, even if it has been renamed.
L Namespace ensemble create command : -- test command
This command is used to test whether a command is a collection command, or True.
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Tcl Learning--The name space