NAME
namespace - create and manipulate contexts for commands and variables
SYNOPSIS
nnaammeessppaaccee ?option? ?arg ...?DESCRIPTION
The nnaammeessppaaccee command lets you create, access, and destroy separateotxs o cmad ad aibe. e te eto WHAT IS A NAMES-
PPAACCEE?? below for a brief overview of namespaces. The legal option's are
listed below. Note that you can abbreviate the option's.nnaammeessppaaccee cchhiillddrreenn ?namespace? ?pattern?
Returns a list of all child namespaces that belong to the names-
pace namespace. If namespace is not specified, then the chil-
dren are returned for the current namespace. This command
returns fully-qualified names, which start with ::::. If the
optional pattern is given, then this command returns only thenames that match the glob-style pattern. The actual pattern
used is determined as follows: a pattern that starts with :::: isused directly, otherwise the namespace namespace (or the fully-
qualified name of the current namespace) is prepended onto the
the pattern. nnaammeessppaaccee ccooddee scriptCaptures the current namespace context for later execution of
the script script. It returns a new script in which script has been wrapped in a nnaammeessppaaccee iinnssccooppee command. The new script has two important properties. First, it can be evaluated in anynamespace and will cause script to be evaluated in the current
namespace (the one where the nnaammeessppaaccee ccooddee command was
invoked). Second, additional arguments can be appended to the resulting script and they will be passed to script as additionalarguments. For example, suppose the command sseett ssccrriipptt [[nnaammeess-
ppaaccee ccooddee {{ffoooo bbaarr}}]] is invoked in namespace ::::aa::::bb. Then eevvaall
""$$ssccrriipptt xx yy"" can be executed in any namespace (assuming the
value of ssccrriipptt has been passed in properly) and will have the same effect as the command ::::nnaammeessppaaccee eevvaall ::::aa::::bb {{ffoooo bbaarr xx yy}}. This command is needed because extensions like Tk normallyexecute callback scripts in the global namespace. A scoped com-
mand captures a command together with its namespace context in a
way that allows it to be executed properly later. See the sec-
tion SSCCOOPPEEDD VVAALLUUEESS for some examples of how this is used to cre-
ate callback scripts. nnaammeessppaaccee ccuurrrreennttReturns the fully-qualified name for the current namespace. The
actual name of the global namespace is ``'' (i.e., an empty
string), but this command returns :::: for the global namespace as
a convenience to programmers.nnaammeessppaaccee ddeelleettee ?namespace namespace ...?
Each namespace namespace is deleted and all variables, proce-
dures, and child namespaces contained in the namespace are
deleted. If a procedure is currently executing inside thenamespace, the namespace will be kept alive until the procedure
returns; however, the namespace is marked to prevent other code
from looking it up by name. If a namespace doesn't exist, this
command returns an error. If no namespace names are given, this
command does nothing.nnaammeessppaaccee eevvaall namespace arg ?arg ...?
Activates a namespace called namespace and evaluates some code
in that context. If the namespace does not already exist, it is
created. If more than one arg argument is specified, the argu-
ments are concatenated together with a space between each one inthe same fashion as the eevvaall command, and the result is evalu-
ated.If namespace has leading namespace qualifiers and any leading
namespaces do not exist, they are automatically created.
nnaammeessppaaccee eexxiissttss namespace
Returns 11 if namespace is a valid namespace in the current con-
text, returns 00 otherwise.nnaammeessppaaccee eexxppoorrtt ?-cclleeaarr? ?pattern pattern ...?
Specifies which commands are exported from a namespace. The
exported commands are those that can be later imported intoanother namespace using a nnaammeessppaaccee iimmppoorrtt command. Both com-
mands defined in a namespace and commands the namespace has pre-
viously imported can be exported by a namespace. The commands
do not have to be defined at the time the nnaammeessppaaccee eexxppoorrtt com-
mand is executed. Each pattern may contain glob-style special
characters, but it may not include any namespace qualifiers.
That is, the pattern can only specify commands in the current(exporting) namespace. Each pattern is appended onto the names-
pace's list of export patterns. If the -cclleeaarr flag is given,
the namespace's export pattern list is reset to empty before any
pattern arguments are appended. If no patterns are given andthe -cclleeaarr flag isn't given, this command returns the names-
pace's current export list. nnaammeessppaaccee ffoorrggeett ?pattern pattern ...?Removes previously imported commands from a namespace. Each
pattern is a simple or qualified name such as xx, ffoooo::::xx or aa::::bb::::pp**. Qualified names contain ::::s and qualify a name withthe name of one or more namespaces. Each qualified pattern is
qualified with the name of an exporting namespace and may have
glob-style special characters in the command name at the end of
the qualified name. Glob characters may not appear in a names-
pace name. For each simple pattern this command deletes thematching commands of the current namespace that were imported
from a different namespace. For qualified patterns, this com-
mand first finds the matching exported commands. It then checks whether any of those commands were previously imported by thecurrent namespace. If so, this command deletes the correspond-
ing imported commands. In effect, this un-does the action of a
nnaammeessppaaccee iimmppoorrtt command.nnaammeessppaaccee iimmppoorrtt ?-ffoorrccee? ?pattern pattern ...?
Imports commands into a namespace. Each pattern is a qualified
name like ffoooo::::xx or aa::::pp**. That is, it includes the name of anexporting namespace and may have glob-style special characters
in the command name at the end of the qualified name. Globcharacters may not appear in a namespace name. All the commands
that match a pattern string and which are currently exportedfrom their namespace are added to the current namespace. This
is done by creating a new command in the current namespace that
points to the exported command in its original namespace; when
the new imported command is called, it invokes the exported com-
mand. This command normally returns an error if an imported command conflicts with an existing command. However, if the-ffoorrccee option is given, imported commands will silently replace
existing commands. The nnaammeessppaaccee iimmppoorrtt command has snapshot semantics: that is, only requested commands that are currentlydefined in the exporting namespace are imported. In other
words, you can import only the commands that are in a namespace
at the time when the nnaammeessppaaccee iimmppoorrtt command is executed. Ifanother command is defined and exported in this namespace later
on, it will not be imported.nnaammeessppaaccee iinnssccooppee namespace script ?arg ...?
Executes a script in the context of the specified namespace.
This command is not expected to be used directly by programmers; calls to it are generated implicitly when applications use nnaammeessppaaccee ccooddee commands to create callback scripts that theapplications then register with, e.g., Tk widgets. The nnaammeess-
ppaaccee iinnssccooppee command is much like the nnaammeessppaaccee eevvaall commandexcept that the namespace must already exist, and nnaammeessppaaccee
iinnssccooppee appends additional args as proper list elements.nnaammeessppaaccee iinnssccooppee ::::ffoooo $$ssccrriipptt $$xx $$yy $$zz is equivalent to nnaammeess-
ppaaccee eevvaall ::::ffoooo [[ccoonnccaatt $$ssccrriipptt [[lliisstt $$xx $$yy $$zz]]]] thus additional
arguments will not undergo a second round of substitution, as is the case with nnaammeessppaaccee eevvaall. nnaammeessppaaccee oorriiggiinn commandReturns the fully-qualified name of the original command to
which the imported command command refers. When a command isimported into a namespace, a new command is created in that
namespace that points to the actual command in the exporting
namespace. If a command is imported into a sequence of names-
paces a, b,...,n where each successive namespace just imports
the command from the previous namespace, this command returns
the fully-qualified name of the original command in the first
namespace, a. If command does not refer to an imported command,
the command's own fully-qualified name is returned.
nnaammeessppaaccee ppaarreenntt ?namespace?
Returns the fully-qualified name of the parent namespace for
namespace namespace. If namespace is not specified, the fully-
qualified name of the current namespace's parent is returned.
nnaammeessppaaccee qquuaalliiffiieerrss stringReturns any leading namespace qualifiers for string. Qualifiers
are namespace names separated by ::::s. For the string
::::ffoooo::::bbaarr::::xx, this command returns ::::ffoooo::::bbaarr, and for :::: it returns an empty string. This command is the complement of the nnaammeessppaaccee ttaaiill command. Note that it does not check whether thenamespace names are, in fact, the names of currently defined
namespaces.
nnaammeessppaaccee ttaaiill stringReturns the simple name at the end of a qualified string. Qual-
ifiers are namespace names separated by ::::s. For the string
::::ffoooo::::bbaarr::::xx, this command returns xx, and for :::: it returns an empty string. This command is the complement of the nnaammeessppaacceeqquuaalliiffiieerrss command. It does not check whether the namespace
names are, in fact, the names of currently defined namespaces.
nnaammeessppaaccee wwhhiicchh ?-ccoommmmaanndd? ?-vvaarriiaabbllee? name
Looks up name as either a command or variable and returns itsfully-qualified name. For example, if name does not exist in
the current namespace but does exist in the global namespace,
this command returns a fully-qualified name in the global names-
pace. If the command or variable does not exist, this command returns an empty string. If the variable has been created but not defined, such as with the vvaarriiaabbllee command or through attrraaccee on the variable, this command will return the fully-quali-
fied name of the variable. If no flag is given, name is treateds cmad ae Se h scin NAME RESOLUTION eo fr n
explanation of the rules regarding name resolution.WHAT IS A NAMESPACE?
A namespace is a collection of commands and variables. It encapsulates
the commands and variables to ensure that they won't interfere with thecommands and variables of other namespaces. Tcl has always had one
such collection, which we refer to as the global namespace. The global
namespace holds all global variables and commands. The nnaammeessppaaccee eevvaall
command lets you create new namespaces. For example,
nnaammeessppaaccee eevvaall CCoouunntteerr {{ nnaammeessppaaccee eexxppoorrtt bbuummpp vvaarriiaabbllee nnuumm 00 pprroocc bbuummpp {{}} {{ vvaarriiaabbllee nnuumm iinnccrr nnuumm }} }}creates a new namespace containing the variable nnuumm and the procedure
bbuummpp. The commands and variables in this namespace are separate from
other commands and variables in the same program. If there is a com-
mand named bbuummpp in the global namespace, for example, it will be dif-
ferent from the command bbuummpp in the CCoouunntteerr namespace.
Namespace variables resemble global variables in Tcl. They exist out-
side of the procedures in a namespace but can be accessed in a proce-
dure via the vvaarriiaabbllee command, as shown in the example above. Namespaces are dynamic. You can add and delete commands and variablesat any time, so you can build up the contents of a namespace over time
using a series of nnaammeessppaaccee eevvaall commands. For example, the followingseries of commands has the same effect as the namespace definition
shown above: nnaammeessppaaccee eevvaall CCoouunntteerr {{ vvaarriiaabbllee nnuumm 00 pprroocc bbuummpp {{}} {{ vvaarriiaabbllee nnuumm rreettuurrnn [[iinnccrr nnuumm]] }} }} nnaammeessppaaccee eevvaall CCoouunntteerr {{ pprroocc tteesstt {{aarrggss}} {{rreettuurrnn $$aarrggss
}} }} nnaammeessppaaccee eevvaall CCoouunntteerr {{ rreennaammee tteesstt """" }}Note that the tteesstt procedure is added to the CCoouunntteerr namespace, and
later removed via the rreennaammee command.Namespaces can have other namespaces within them, so they nest hierar-
chically. A nested namespace is encapsulated inside its parent names-
pace and can not interfere with other namespaces.
QUALIFIED NAMES
Each namespace has a textual name such as hhiissttoorryy or ::::ssaaffee::::iinntteerrpp.
Since namespaces may nest, qualified names are used to refer to com-
mands, variables, and child namespaces contained inside namespaces.
Qualified names are similar to the hierarchical path names for Unix files or Tk widgets, except that :::: is used as the separator instead of// or ... The topmost or global namespace has the name ``'' (i.e., an
empty string), although :::: is a synonym. As an example, the name::::ssaaffee::::iinntteerrpp::::ccrreeaattee refers to the command ccrreeaattee in the namespace
iinntteerrpp that is a child of of namespace ::::ssaaffee, which in turn is a child
of the global namespace ::::.
If you want to access commands and variables from another namespace,
you must use some extra syntax. Names must be qualified by the names-
pace that contains them. From the global namespace, we might access
the CCoouunntteerr procedures like this: CCoouunntteerr::::bbuummpp 55 CCoouunntteerr::::RReesseett We could access the current count like this:ppuuttss ""ccoouunntt == $$CCoouunntteerr::::nnuumm""
When one namespace contains another, you may need more than one quali-
fier to reach its elements. If we had a namespace FFoooo that contained
the namespace CCoouunntteerr, you could invoke its bbuummpp procedure from the
global namespace like this:
FFoooo::::CCoouunntteerr::::bbuummpp 33 You can also use qualified names when you create and rename commands.For example, you could add a procedure to the FFoooo namespace like this:
pprroocc FFoooo::::TTeesstt {{aarrggss}} {{rreettuurrnn $$aarrggss}}
And you could move the same procedure to another namespace like this:
rreennaammee FFoooo::::TTeesstt BBaarr::::TTeesstt There are a few remaining points about qualified names that we shouldcover. Namespaces have nonempty names except for the global namespace.
:::: is disallowed in simple command, variable, and namespace names
except as a namespace separator. Extra ::s in a qualified name are
ignored; that is, two or more ::s are treated as a namespace separator.
A trailing :::: in a qualified variable or command name refers to the variable or command named {}. However, a trailing :::: in a qualifiednamespace name is ignored.
NAME RESOLUTION
In general, all Tcl commands that take variable and command names sup-
port qualified names. This means you can give qualified names to such commands as sseett, pprroocc, rreennaammee, and iinntteerrpp aalliiaass. If you provide afully-qualified name that starts with a ::::, there is no question about
what command, variable, or namespace you mean. However, if the name
does not start with a :::: (i.e., is relative), Tcl follows a fixed rule for looking it up: Command and variable names are always resolved bylooking first in the current namespace, and then in the global names-
pace. Namespace names, on the other hand, are always resolved by look-
ing in only the current namespace.
In the following example, sseett ttrraacceeLLeevveell 00 nnaammeessppaaccee eevvaall DDeebbuugg {{pprriinnttTTrraaccee $$ttrraacceeLLeevveell
}}Tcl looks for ttrraacceeLLeevveell in the namespace DDeebbuugg and then in the global
namespace. It looks up the command pprriinnttTTrraaccee in the same way. If a
variable or command name is not found in either context, the name is undefined. To make this point absolutely clear, consider the following example: sseett ttrraacceeLLeevveell 00 nnaammeessppaaccee eevvaall FFoooo {{ vvaarriiaabbllee ttrraacceeLLeevveell 33 nnaammeessppaaccee eevvaall DDeebbuugg {{pprriinnttTTrraaccee $$ttrraacceeLLeevveell
}} }}Here Tcl looks for ttrraacceeLLeevveell first in the namespace FFoooo::::DDeebbuugg. Since
it is not found there, Tcl then looks for it in the global namespace.
The variable FFoooo::::ttrraacceeLLeevveell is completely ignored during the name res-
olution process. You can use the nnaammeessppaaccee wwhhiicchh command to clear up any question about name resolution. For example, the command:nnaammeessppaaccee eevvaall FFoooo::::DDeebbuugg {{nnaammeessppaaccee wwhhiicchh -vvaarriiaabbllee ttrraacceeLLeevveell}}
returns ::::ttrraacceeLLeevveell. On the other hand, the command,nnaammeessppaaccee eevvaall FFoooo {{nnaammeessppaaccee wwhhiicchh -vvaarriiaabbllee ttrraacceeLLeevveell}}
returns ::::FFoooo::::ttrraacceeLLeevveell.As mentioned above, namespace names are looked up differently than the
names of variables and commands. Namespace names are always resolvedin the current namespace. This means, for example, that a nnaammeessppaaccee
eevvaall command that creates a new namespace always creates a child of the
current namespace unless the new namespace name begins with a ::::.
Tcl has no access control to limit what variables, commands, or names-
paces you can reference. If you provide a qualified name that resolves to an element by the name resolution rule above, you can access the element.You can access a namespace variable from a procedure in the same names-
pace by using the vvaarriiaabbllee command. Much like the gglloobbaall command, thiscreates a local link to the namespace variable. If necessary, it also
creates the variable in the current namespace and initializes it. Note
that the gglloobbaall command only creates links to variables in the globalnamespace. It is not necessary to use a vvaarriiaabbllee command if you always
refer to the namespace variable using an appropriate qualified name.
IIMMPPOORRTTIINNGG CCOOMMMMAANNDDSSNamespaces are often used to represent libraries. Some library com-
mands are used so frequently that it is a nuisance to type their quali-
fied names. For example, suppose that all of the commands in a packagelike BLT are contained in a namespace called BBlltt. Then you might
access these commands like this:BBlltt::::ggrraapphh ..gg -bbaacckkggrroouunndd rreedd
BBlltt::::ttaabbllee .. ..gg 00,,00 If you use the ggrraapphh and ttaabbllee commands frequently, you may want to access them without the BBlltt:::: prefix. You can do this by importing thecommands into the current namespace, like this:
nnaammeessppaaccee iimmppoorrtt BBlltt::::**This adds all exported commands from the BBlltt namespace into the current
namespace context, so you can write code like this:
ggrraapphh ..gg -bbaacckkggrroouunndd rreedd
ttaabbllee .. ..gg 00,,00The nnaammeessppaaccee iimmppoorrtt command only imports commands from a namespace
that that namespace exported with a nnaammeessppaaccee eexxppoorrtt command.
Importing every command from a namespace is generally a bad idea since
you don't know what you will get. It is better to import just the spe-
cific commands you need. For example, the command nnaammeessppaaccee iimmppoorrtt BBlltt::::ggrraapphh BBlltt::::ttaabbllee imports only the ggrraapphh and ttaabbllee commands into the current context. If you try to import a command that already exists, you will get anerror. This prevents you from importing the same command from two dif-
ferent packages. But from time to time (perhaps when debugging), you may want to get around this restriction. You may want to reissue the nnaammeessppaaccee iimmppoorrtt command to pick up new commands that have appeared ina namespace. In that case, you can use the -ffoorrccee option, and existing
commands will be silently overwritten:nnaammeessppaaccee iimmppoorrtt -ffoorrccee BBlltt::::ggrraapphh BBlltt::::ttaabbllee
If for some reason, you want to stop using the imported commands, you can remove them with an nnaammeessppaaccee ffoorrggeett command, like this: nnaammeessppaaccee ffoorrggeett BBlltt::::**This searches the current namespace for any commands imported from BBlltt.
If it finds any, it removes them. Otherwise, it does nothing. After this, the BBlltt commands must be accessed with the BBlltt:::: prefix.When you delete a command from the exporting namespace like this:
rreennaammee BBlltt::::ggrraapphh """"the command is automatically removed from all namespaces that import
it. EEXXPPOORRTTIINNGG CCOOMMMMAANNDDSSYou can export commands from a namespace like this:
nnaammeessppaaccee eevvaall CCoouunntteerr {{ nnaammeessppaaccee eexxppoorrtt bbuummpp rreesseett vvaarriiaabbllee NNuumm 00 vvaarriiaabbllee MMaaxx 110000 pprroocc bbuummpp {{{{bbyy 11}}}} {{ vvaarriiaabbllee NNuummiinnccrr NNuumm $$bbyy
CChheecckkrreettuurrnn $$NNuumm
}} pprroocc rreesseett {{}} {{ vvaarriiaabbllee NNuumm sseett NNuumm 00 }} pprroocc CChheecckk {{}} {{ vvaarriiaabbllee NNuumm vvaarriiaabbllee MMaaxxiiff {{$$NNuumm >> $$MMaaxx}} {{
eerrrroorr ""ttoooo hhiigghh!!"" }} }} }} The procedures bbuummpp and rreesseett are exported, so they are included whenyou import from the CCoouunntteerr namespace, like this:
nnaammeessppaaccee iimmppoorrtt CCoouunntteerr::::** However, the CChheecckk procedure is not exported, so it is ignored by the import operation. The nnaammeessppaaccee iimmppoorrtt command only imports commands that were declaredas exported by their namespace. The nnaammeessppaaccee eexxppoorrtt command specifies
what commands may be imported by other namespaces. If a nnaammeessppaaccee
iimmppoorrtt command specifies a command that is not exported, the command is not imported.SEE ALSO
variable(n) KKEEYYWWOORRDDSS exported, internal, variableTcl 8.0 namespace(n)