| 1 |
'\" |
| 2 |
'\" Copyright (c) 1993-1997 Bell Labs Innovations for Lucent Technologies |
| 3 |
'\" Copyright (c) 1997 Sun Microsystems, Inc. |
| 4 |
'\" Copyright (c) 2000 Scriptics Corporation. |
| 5 |
'\" Copyright (c) 2004-2005 Donal K. Fellows. |
| 6 |
'\" |
| 7 |
'\" See the file "license.terms" for information on usage and redistribution |
| 8 |
'\" of this file, and for a DISCLAIMER OF ALL WARRANTIES. |
| 9 |
'\" |
| 10 |
.TH namespace n 8.5 Tcl "Tcl Built-In Commands" |
| 11 |
.so man.macros |
| 12 |
.BS |
| 13 |
'\" Note: do not modify the .SH NAME line immediately below! |
| 14 |
.SH NAME |
| 15 |
namespace \- create and manipulate contexts for commands and variables |
| 16 |
.SH SYNOPSIS |
| 17 |
\fBnamespace \fR\fIsubcommand\fR ?\fIarg ...\fR? |
| 18 |
.BE |
| 19 |
.SH DESCRIPTION |
| 20 |
.PP |
| 21 |
The \fBnamespace\fR command lets you create, access, and destroy |
| 22 |
separate contexts for commands and variables. |
| 23 |
See the section \fBWHAT IS A NAMESPACE?\fR below |
| 24 |
for a brief overview of namespaces. |
| 25 |
The legal values of \fIsubcommand\fR are listed below. |
| 26 |
Note that you can abbreviate the \fIsubcommand\fRs. |
| 27 |
.TP |
| 28 |
\fBnamespace children \fR?\fInamespace\fR? ?\fIpattern\fR? |
| 29 |
. |
| 30 |
Returns a list of all child namespaces that belong to the |
| 31 |
namespace \fInamespace\fR. |
| 32 |
If \fInamespace\fR is not specified, |
| 33 |
then the children are returned for the current namespace. |
| 34 |
This command returns fully-qualified names, |
| 35 |
which start with a double colon (\fB::\fR). |
| 36 |
If the optional \fIpattern\fR is given, |
| 37 |
then this command returns only the names that match the glob-style pattern. |
| 38 |
The actual pattern used is determined as follows: |
| 39 |
a pattern that starts with double colon (\fB::\fR) is used directly, |
| 40 |
otherwise the namespace \fInamespace\fR |
| 41 |
(or the fully-qualified name of the current namespace) |
| 42 |
is prepended onto the pattern. |
| 43 |
.TP |
| 44 |
\fBnamespace code \fIscript\fR |
| 45 |
. |
| 46 |
Captures the current namespace context for later execution |
| 47 |
of the script \fIscript\fR. |
| 48 |
It returns a new script in which \fIscript\fR has been wrapped |
| 49 |
in a \fBnamespace inscope\fR command. |
| 50 |
The new script has two important properties. |
| 51 |
First, it can be evaluated in any namespace and will cause |
| 52 |
\fIscript\fR to be evaluated in the current namespace |
| 53 |
(the one where the \fBnamespace code\fR command was invoked). |
| 54 |
Second, additional arguments can be appended to the resulting script |
| 55 |
and they will be passed to \fIscript\fR as additional arguments. |
| 56 |
For example, suppose the command |
| 57 |
\fBset script [namespace code {foo bar}]\fR |
| 58 |
is invoked in namespace \fB::a::b\fR. |
| 59 |
Then \fBeval $script [list x y]\fR |
| 60 |
can be executed in any namespace (assuming the value of |
| 61 |
\fBscript\fR has been passed in properly) |
| 62 |
and will have the same effect as the command |
| 63 |
\fB::namespace eval ::a::b {foo bar x y}\fR. |
| 64 |
This command is needed because |
| 65 |
extensions like Tk normally execute callback scripts |
| 66 |
in the global namespace. |
| 67 |
A scoped command captures a command together with its namespace context |
| 68 |
in a way that allows it to be executed properly later. |
| 69 |
See the section \fBSCOPED SCRIPTS\fR for some examples |
| 70 |
of how this is used to create callback scripts. |
| 71 |
.TP |
| 72 |
\fBnamespace current\fR |
| 73 |
. |
| 74 |
Returns the fully-qualified name for the current namespace. |
| 75 |
The actual name of the global namespace is |
| 76 |
.MT |
| 77 |
(i.e., an empty string), |
| 78 |
but this command returns \fB::\fR for the global namespace |
| 79 |
as a convenience to programmers. |
| 80 |
.TP |
| 81 |
\fBnamespace delete \fR?\fInamespace namespace ...\fR? |
| 82 |
. |
| 83 |
Each namespace \fInamespace\fR is deleted |
| 84 |
and all variables, procedures, and child namespaces |
| 85 |
contained in the namespace are deleted. |
| 86 |
If a procedure is currently executing inside the namespace, |
| 87 |
the namespace will be kept alive until the procedure returns; |
| 88 |
however, the namespace is marked to prevent other code from |
| 89 |
looking it up by name. |
| 90 |
If a namespace does not exist, this command returns an error. |
| 91 |
If no namespace names are given, this command does nothing. |
| 92 |
.TP |
| 93 |
\fBnamespace ensemble\fR \fIsubcommand\fR ?\fIarg ...\fR? |
| 94 |
. |
| 95 |
Creates and manipulates a command that is formed out of an ensemble of |
| 96 |
subcommands. See the section \fBENSEMBLES\fR below for further |
| 97 |
details. |
| 98 |
.TP |
| 99 |
\fBnamespace eval\fR \fInamespace arg\fR ?\fIarg ...\fR? |
| 100 |
. |
| 101 |
Activates a namespace called \fInamespace\fR and evaluates some code |
| 102 |
in that context. |
| 103 |
If the namespace does not already exist, it is created. |
| 104 |
If more than one \fIarg\fR argument is specified, |
| 105 |
the arguments are concatenated together with a space between each one |
| 106 |
in the same fashion as the \fBeval\fR command, |
| 107 |
and the result is evaluated. |
| 108 |
.RS |
| 109 |
.PP |
| 110 |
If \fInamespace\fR has leading namespace qualifiers |
| 111 |
and any leading namespaces do not exist, |
| 112 |
they are automatically created. |
| 113 |
.RE |
| 114 |
.TP |
| 115 |
\fBnamespace exists\fR \fInamespace\fR |
| 116 |
. |
| 117 |
Returns \fB1\fR if \fInamespace\fR is a valid namespace in the current |
| 118 |
context, returns \fB0\fR otherwise. |
| 119 |
.TP |
| 120 |
\fBnamespace export \fR?\fB\-clear\fR? ?\fIpattern pattern ...\fR? |
| 121 |
. |
| 122 |
Specifies which commands are exported from a namespace. |
| 123 |
The exported commands are those that can be later imported |
| 124 |
into another namespace using a \fBnamespace import\fR command. |
| 125 |
Both commands defined in a namespace and |
| 126 |
commands the namespace has previously imported |
| 127 |
can be exported by a namespace. |
| 128 |
The commands do not have to be defined |
| 129 |
at the time the \fBnamespace export\fR command is executed. |
| 130 |
Each \fIpattern\fR may contain glob-style special characters, |
| 131 |
but it may not include any namespace qualifiers. |
| 132 |
That is, the pattern can only specify commands |
| 133 |
in the current (exporting) namespace. |
| 134 |
Each \fIpattern\fR is appended onto the namespace's list of export patterns. |
| 135 |
If the \fB\-clear\fR flag is given, |
| 136 |
the namespace's export pattern list is reset to empty before any |
| 137 |
\fIpattern\fR arguments are appended. |
| 138 |
If no \fIpattern\fRs are given and the \fB\-clear\fR flag is not given, |
| 139 |
this command returns the namespace's current export list. |
| 140 |
.TP |
| 141 |
\fBnamespace forget \fR?\fIpattern pattern ...\fR? |
| 142 |
. |
| 143 |
Removes previously imported commands from a namespace. |
| 144 |
Each \fIpattern\fR is a simple or qualified name such as |
| 145 |
\fBx\fR, \fBfoo::x\fR or \fBa::b::p*\fR. |
| 146 |
Qualified names contain double colons (\fB::\fR) and qualify a name |
| 147 |
with the name of one or more namespaces. |
| 148 |
Each |
| 149 |
.QW "qualified pattern" |
| 150 |
is qualified with the name of an exporting namespace |
| 151 |
and may have glob-style special characters in the command name |
| 152 |
at the end of the qualified name. |
| 153 |
Glob characters may not appear in a namespace name. |
| 154 |
For each |
| 155 |
.QW "simple pattern" |
| 156 |
this command deletes the matching commands of the |
| 157 |
current namespace that were imported from a different namespace. |
| 158 |
For |
| 159 |
.QW "qualified patterns" , |
| 160 |
this command first finds the matching exported commands. |
| 161 |
It then checks whether any of those commands |
| 162 |
were previously imported by the current namespace. |
| 163 |
If so, this command deletes the corresponding imported commands. |
| 164 |
In effect, this undoes the action of a \fBnamespace import\fR command. |
| 165 |
.TP |
| 166 |
\fBnamespace import \fR?\fB\-force\fR? ?\fIpattern\fR \fIpattern ...\fR? |
| 167 |
. |
| 168 |
Imports commands into a namespace, or queries the set of imported |
| 169 |
commands in a namespace. When no arguments are present, |
| 170 |
\fBnamespace import\fR returns the list of commands in |
| 171 |
the current namespace that have been imported from other |
| 172 |
namespaces. The commands in the returned list are in |
| 173 |
the format of simple names, with no namespace qualifiers at all. |
| 174 |
This format is suitable for composition with \fBnamespace forget\fR |
| 175 |
(see \fBEXAMPLES\fR below). |
| 176 |
.RS |
| 177 |
.PP |
| 178 |
When \fIpattern\fR arguments are present, |
| 179 |
each \fIpattern\fR is a qualified name like |
| 180 |
\fBfoo::x\fR or \fBa::p*\fR. |
| 181 |
That is, it includes the name of an exporting namespace |
| 182 |
and may have glob-style special characters in the command name |
| 183 |
at the end of the qualified name. |
| 184 |
Glob characters may not appear in a namespace name. |
| 185 |
When the namespace name is not fully qualified (i.e., does not start |
| 186 |
with a namespace separator) it is resolved as a namespace name in the |
| 187 |
way described in the \fBNAME RESOLUTION\fR section; it is an error if |
| 188 |
no namespace with that name can be found. |
| 189 |
.PP |
| 190 |
All the commands that match a \fIpattern\fR string |
| 191 |
and which are currently exported from their namespace |
| 192 |
are added to the current namespace. |
| 193 |
This is done by creating a new command in the current namespace |
| 194 |
that points to the exported command in its original namespace; |
| 195 |
when the new imported command is called, it invokes the exported command. |
| 196 |
This command normally returns an error |
| 197 |
if an imported command conflicts with an existing command. |
| 198 |
However, if the \fB\-force\fR option is given, |
| 199 |
imported commands will silently replace existing commands. |
| 200 |
The \fBnamespace import\fR command has snapshot semantics: |
| 201 |
that is, only requested commands that are currently defined |
| 202 |
in the exporting namespace are imported. |
| 203 |
In other words, you can import only the commands that are in a namespace |
| 204 |
at the time when the \fBnamespace import\fR command is executed. |
| 205 |
If another command is defined and exported in this namespace later on, |
| 206 |
it will not be imported. |
| 207 |
.RE |
| 208 |
.TP |
| 209 |
\fBnamespace inscope\fR \fInamespace\fR \fIscript\fR ?\fIarg ...\fR? |
| 210 |
. |
| 211 |
Executes a script in the context of the specified \fInamespace\fR. |
| 212 |
This command is not expected to be used directly by programmers; |
| 213 |
calls to it are generated implicitly when applications |
| 214 |
use \fBnamespace code\fR commands to create callback scripts |
| 215 |
that the applications then register with, e.g., Tk widgets. |
| 216 |
The \fBnamespace inscope\fR command is much like the \fBnamespace eval\fR |
| 217 |
command except that the \fInamespace\fR must already exist, |
| 218 |
and \fBnamespace inscope\fR appends additional \fIarg\fRs |
| 219 |
as proper list elements. |
| 220 |
.RS |
| 221 |
.PP |
| 222 |
.CS |
| 223 |
\fBnamespace inscope ::foo $script $x $y $z\fR |
| 224 |
.CE |
| 225 |
.PP |
| 226 |
is equivalent to |
| 227 |
.PP |
| 228 |
.CS |
| 229 |
\fBnamespace eval ::foo [concat $script [list $x $y $z]]\fR |
| 230 |
.CE |
| 231 |
.PP |
| 232 |
thus additional arguments will not undergo a second round of substitution, |
| 233 |
as is the case with \fBnamespace eval\fR. |
| 234 |
.RE |
| 235 |
.TP |
| 236 |
\fBnamespace origin \fIcommand\fR |
| 237 |
. |
| 238 |
Returns the fully-qualified name of the original command |
| 239 |
to which the imported command \fIcommand\fR refers. |
| 240 |
When a command is imported into a namespace, |
| 241 |
a new command is created in that namespace |
| 242 |
that points to the actual command in the exporting namespace. |
| 243 |
If a command is imported into a sequence of namespaces |
| 244 |
\fIa, b,...,n\fR where each successive namespace |
| 245 |
just imports the command from the previous namespace, |
| 246 |
this command returns the fully-qualified name of the original command |
| 247 |
in the first namespace, \fIa\fR. |
| 248 |
If \fIcommand\fR does not refer to an imported command, |
| 249 |
the command's own fully-qualified name is returned. |
| 250 |
.TP |
| 251 |
\fBnamespace parent\fR ?\fInamespace\fR? |
| 252 |
. |
| 253 |
Returns the fully-qualified name of the parent namespace |
| 254 |
for namespace \fInamespace\fR. |
| 255 |
If \fInamespace\fR is not specified, |
| 256 |
the fully-qualified name of the current namespace's parent is returned. |
| 257 |
.TP |
| 258 |
\fBnamespace path\fR ?\fInamespaceList\fR? |
| 259 |
. |
| 260 |
Returns the command resolution path of the current namespace. If |
| 261 |
\fInamespaceList\fR is specified as a list of named namespaces, the |
| 262 |
current namespace's command resolution path is set to those namespaces |
| 263 |
and returns the empty list. The default command resolution path is |
| 264 |
always empty. See the section \fBNAME RESOLUTION\fR below for an |
| 265 |
explanation of the rules regarding name resolution. |
| 266 |
.TP |
| 267 |
\fBnamespace qualifiers\fR \fIstring\fR |
| 268 |
. |
| 269 |
Returns any leading namespace qualifiers for \fIstring\fR. |
| 270 |
Qualifiers are namespace names separated by double colons (\fB::\fR). |
| 271 |
For the \fIstring\fR \fB::foo::bar::x\fR, |
| 272 |
this command returns \fB::foo::bar\fR, |
| 273 |
and for \fB::\fR it returns an empty string. |
| 274 |
This command is the complement of the \fBnamespace tail\fR command. |
| 275 |
Note that it does not check whether the |
| 276 |
namespace names are, in fact, |
| 277 |
the names of currently defined namespaces. |
| 278 |
.TP |
| 279 |
\fBnamespace tail\fR \fIstring\fR |
| 280 |
. |
| 281 |
Returns the simple name at the end of a qualified string. |
| 282 |
Qualifiers are namespace names separated by double colons (\fB::\fR). |
| 283 |
For the \fIstring\fR \fB::foo::bar::x\fR, |
| 284 |
this command returns \fBx\fR, |
| 285 |
and for \fB::\fR it returns an empty string. |
| 286 |
This command is the complement of the \fBnamespace qualifiers\fR command. |
| 287 |
It does not check whether the namespace names are, in fact, |
| 288 |
the names of currently defined namespaces. |
| 289 |
.TP |
| 290 |
\fBnamespace upvar\fR \fInamespace\fR ?\fIotherVar myVar \fR...? |
| 291 |
. |
| 292 |
This command arranges for zero or more local variables in the current |
| 293 |
procedure to refer to variables in \fInamespace\fR. The namespace name is |
| 294 |
resolved as described in section \fBNAME RESOLUTION\fR. |
| 295 |
The command |
| 296 |
\fBnamespace upvar $ns a b\fR has the same behaviour as |
| 297 |
\fBupvar 0 ${ns}::a b\fR, with the sole exception of the resolution rules |
| 298 |
used for qualified namespace or variable names. |
| 299 |
\fBnamespace upvar\fR returns an empty string. |
| 300 |
.TP |
| 301 |
\fBnamespace unknown\fR ?\fIscript\fR? |
| 302 |
. |
| 303 |
Sets or returns the unknown command handler for the current namespace. |
| 304 |
The handler is invoked when a command called from within the namespace |
| 305 |
cannot be found in the current namespace, the namespace's path nor in |
| 306 |
the global namespace. |
| 307 |
The \fIscript\fR argument, if given, should be a well |
| 308 |
formed list representing a command name and optional arguments. When |
| 309 |
the handler is invoked, the full invocation line will be appended to the |
| 310 |
script and the result evaluated in the context of the namespace. The |
| 311 |
default handler for all namespaces is \fB::unknown\fR. If no argument |
| 312 |
is given, it returns the handler for the current namespace. |
| 313 |
.TP |
| 314 |
\fBnamespace which\fR ?\fB\-command\fR? ?\fB\-variable\fR? \fIname\fR |
| 315 |
. |
| 316 |
Looks up \fIname\fR as either a command or variable |
| 317 |
and returns its fully-qualified name. |
| 318 |
For example, if \fIname\fR does not exist in the current namespace |
| 319 |
but does exist in the global namespace, |
| 320 |
this command returns a fully-qualified name in the global namespace. |
| 321 |
If the command or variable does not exist, |
| 322 |
this command returns an empty string. If the variable has been |
| 323 |
created but not defined, such as with the \fBvariable\fR command |
| 324 |
or through a \fBtrace\fR on the variable, this command will return the |
| 325 |
fully-qualified name of the variable. |
| 326 |
If no flag is given, \fIname\fR is treated as a command name. |
| 327 |
See the section \fBNAME RESOLUTION\fR below for an explanation of |
| 328 |
the rules regarding name resolution. |
| 329 |
.SH "WHAT IS A NAMESPACE?" |
| 330 |
.PP |
| 331 |
A namespace is a collection of commands and variables. |
| 332 |
It encapsulates the commands and variables to ensure that they |
| 333 |
will not interfere with the commands and variables of other namespaces. |
| 334 |
Tcl has always had one such collection, |
| 335 |
which we refer to as the \fIglobal namespace\fR. |
| 336 |
The global namespace holds all global variables and commands. |
| 337 |
The \fBnamespace eval\fR command lets you create new namespaces. |
| 338 |
For example, |
| 339 |
.PP |
| 340 |
.CS |
| 341 |
\fBnamespace eval\fR Counter { |
| 342 |
\fBnamespace export\fR bump |
| 343 |
variable num 0 |
| 344 |
|
| 345 |
proc bump {} { |
| 346 |
variable num |
| 347 |
incr num |
| 348 |
} |
| 349 |
} |
| 350 |
.CE |
| 351 |
.PP |
| 352 |
creates a new namespace containing the variable \fBnum\fR and |
| 353 |
the procedure \fBbump\fR. |
| 354 |
The commands and variables in this namespace are separate from |
| 355 |
other commands and variables in the same program. |
| 356 |
If there is a command named \fBbump\fR in the global namespace, |
| 357 |
for example, it will be different from the command \fBbump\fR |
| 358 |
in the \fBCounter\fR namespace. |
| 359 |
.PP |
| 360 |
Namespace variables resemble global variables in Tcl. |
| 361 |
They exist outside of the procedures in a namespace |
| 362 |
but can be accessed in a procedure via the \fBvariable\fR command, |
| 363 |
as shown in the example above. |
| 364 |
.PP |
| 365 |
Namespaces are dynamic. |
| 366 |
You can add and delete commands and variables at any time, |
| 367 |
so you can build up the contents of a |
| 368 |
namespace over time using a series of \fBnamespace eval\fR commands. |
| 369 |
For example, the following series of commands has the same effect |
| 370 |
as the namespace definition shown above: |
| 371 |
.PP |
| 372 |
.CS |
| 373 |
\fBnamespace eval\fR Counter { |
| 374 |
variable num 0 |
| 375 |
proc bump {} { |
| 376 |
variable num |
| 377 |
return [incr num] |
| 378 |
} |
| 379 |
} |
| 380 |
\fBnamespace eval\fR Counter { |
| 381 |
proc test {args} { |
| 382 |
return $args |
| 383 |
} |
| 384 |
} |
| 385 |
\fBnamespace eval\fR Counter { |
| 386 |
rename test "" |
| 387 |
} |
| 388 |
.CE |
| 389 |
.PP |
| 390 |
Note that the \fBtest\fR procedure is added to the \fBCounter\fR namespace, |
| 391 |
and later removed via the \fBrename\fR command. |
| 392 |
.PP |
| 393 |
Namespaces can have other namespaces within them, |
| 394 |
so they nest hierarchically. |
| 395 |
A nested namespace is encapsulated inside its parent namespace |
| 396 |
and can not interfere with other namespaces. |
| 397 |
.SH "QUALIFIED NAMES" |
| 398 |
.PP |
| 399 |
Each namespace has a textual name such as |
| 400 |
\fBhistory\fR or \fB::safe::interp\fR. |
| 401 |
Since namespaces may nest, |
| 402 |
qualified names are used to refer to |
| 403 |
commands, variables, and child namespaces contained inside namespaces. |
| 404 |
Qualified names are similar to the hierarchical path names for |
| 405 |
Unix files or Tk widgets, |
| 406 |
except that \fB::\fR is used as the separator |
| 407 |
instead of \fB/\fR or \fB.\fR. |
| 408 |
The topmost or global namespace has the name |
| 409 |
.MT |
| 410 |
(i.e., an empty string), although \fB::\fR is a synonym. |
| 411 |
As an example, the name \fB::safe::interp::create\fR |
| 412 |
refers to the command \fBcreate\fR in the namespace \fBinterp\fR |
| 413 |
that is a child of namespace \fB::safe\fR, |
| 414 |
which in turn is a child of the global namespace, \fB::\fR. |
| 415 |
.PP |
| 416 |
If you want to access commands and variables from another namespace, |
| 417 |
you must use some extra syntax. |
| 418 |
Names must be qualified by the namespace that contains them. |
| 419 |
From the global namespace, |
| 420 |
we might access the \fBCounter\fR procedures like this: |
| 421 |
.PP |
| 422 |
.CS |
| 423 |
Counter::bump 5 |
| 424 |
Counter::Reset |
| 425 |
.CE |
| 426 |
.PP |
| 427 |
We could access the current count like this: |
| 428 |
.PP |
| 429 |
.CS |
| 430 |
puts "count = $Counter::num" |
| 431 |
.CE |
| 432 |
.PP |
| 433 |
When one namespace contains another, you may need more than one |
| 434 |
qualifier to reach its elements. |
| 435 |
If we had a namespace \fBFoo\fR that contained the namespace \fBCounter\fR, |
| 436 |
you could invoke its \fBbump\fR procedure |
| 437 |
from the global namespace like this: |
| 438 |
.PP |
| 439 |
.CS |
| 440 |
Foo::Counter::bump 3 |
| 441 |
.CE |
| 442 |
.PP |
| 443 |
You can also use qualified names when you create and rename commands. |
| 444 |
For example, you could add a procedure to the \fBFoo\fR |
| 445 |
namespace like this: |
| 446 |
.PP |
| 447 |
.CS |
| 448 |
proc Foo::Test {args} {return $args} |
| 449 |
.CE |
| 450 |
.PP |
| 451 |
And you could move the same procedure to another namespace like this: |
| 452 |
.PP |
| 453 |
.CS |
| 454 |
rename Foo::Test Bar::Test |
| 455 |
.CE |
| 456 |
.PP |
| 457 |
There are a few remaining points about qualified names |
| 458 |
that we should cover. |
| 459 |
Namespaces have nonempty names except for the global namespace. |
| 460 |
\fB::\fR is disallowed in simple command, variable, and namespace names |
| 461 |
except as a namespace separator. |
| 462 |
Extra colons in any separator part of a qualified name are ignored; |
| 463 |
i.e. two or more colons are treated as a namespace separator. |
| 464 |
A trailing \fB::\fR in a qualified variable or command name |
| 465 |
refers to the variable or command named {}. |
| 466 |
However, a trailing \fB::\fR in a qualified namespace name is ignored. |
| 467 |
.SH "NAME RESOLUTION" |
| 468 |
.PP |
| 469 |
In general, all Tcl commands that take variable and command names |
| 470 |
support qualified names. |
| 471 |
This means you can give qualified names to such commands as |
| 472 |
\fBset\fR, \fBproc\fR, \fBrename\fR, and \fBinterp alias\fR. |
| 473 |
If you provide a fully-qualified name that starts with a \fB::\fR, |
| 474 |
there is no question about what command, variable, or namespace |
| 475 |
you mean. |
| 476 |
However, if the name does not start with a \fB::\fR |
| 477 |
(i.e., is \fIrelative\fR), |
| 478 |
Tcl follows basic rules for looking it up: |
| 479 |
.IP \(bu |
| 480 |
\fBVariable names\fR are always resolved by looking first in the current |
| 481 |
namespace, and then in the global namespace. |
| 482 |
.IP \(bu |
| 483 |
\fBCommand names\fR are always resolved by looking in the current namespace |
| 484 |
first. If not found there, they are searched for in every namespace on the |
| 485 |
current namespace's command path (which is empty by default). If not found |
| 486 |
there, command names are looked up in the global namespace (or, failing that, |
| 487 |
are processed by the appropriate \fBnamespace unknown\fR handler.) |
| 488 |
.IP \(bu |
| 489 |
\fBNamespace names\fR are always resolved by looking in only the current |
| 490 |
namespace. |
| 491 |
.PP |
| 492 |
In the following example, |
| 493 |
.PP |
| 494 |
.CS |
| 495 |
set traceLevel 0 |
| 496 |
\fBnamespace eval\fR Debug { |
| 497 |
printTrace $traceLevel |
| 498 |
} |
| 499 |
.CE |
| 500 |
.PP |
| 501 |
Tcl looks for \fBtraceLevel\fR in the namespace \fBDebug\fR |
| 502 |
and then in the global namespace. |
| 503 |
It looks up the command \fBprintTrace\fR in the same way. |
| 504 |
If a variable or command name is not found in either context, |
| 505 |
the name is undefined. |
| 506 |
To make this point absolutely clear, consider the following example: |
| 507 |
.PP |
| 508 |
.CS |
| 509 |
set traceLevel 0 |
| 510 |
\fBnamespace eval\fR Foo { |
| 511 |
variable traceLevel 3 |
| 512 |
|
| 513 |
\fBnamespace eval\fR Debug { |
| 514 |
printTrace $traceLevel |
| 515 |
} |
| 516 |
} |
| 517 |
.CE |
| 518 |
.PP |
| 519 |
Here Tcl looks for \fBtraceLevel\fR first in the namespace \fBFoo::Debug\fR. |
| 520 |
Since it is not found there, Tcl then looks for it |
| 521 |
in the global namespace. |
| 522 |
The variable \fBFoo::traceLevel\fR is completely ignored |
| 523 |
during the name resolution process. |
| 524 |
.PP |
| 525 |
You can use the \fBnamespace which\fR command to clear up any question |
| 526 |
about name resolution. |
| 527 |
For example, the command: |
| 528 |
.PP |
| 529 |
.CS |
| 530 |
\fBnamespace eval\fR Foo::Debug {\fBnamespace which\fR \-variable traceLevel} |
| 531 |
.CE |
| 532 |
.PP |
| 533 |
returns \fB::traceLevel\fR. |
| 534 |
On the other hand, the command, |
| 535 |
.PP |
| 536 |
.CS |
| 537 |
\fBnamespace eval\fR Foo {\fBnamespace which\fR \-variable traceLevel} |
| 538 |
.CE |
| 539 |
.PP |
| 540 |
returns \fB::Foo::traceLevel\fR. |
| 541 |
.PP |
| 542 |
As mentioned above, |
| 543 |
namespace names are looked up differently |
| 544 |
than the names of variables and commands. |
| 545 |
Namespace names are always resolved in the current namespace. |
| 546 |
This means, for example, |
| 547 |
that a \fBnamespace eval\fR command that creates a new namespace |
| 548 |
always creates a child of the current namespace |
| 549 |
unless the new namespace name begins with \fB::\fR. |
| 550 |
.PP |
| 551 |
Tcl has no access control to limit what variables, commands, |
| 552 |
or namespaces you can reference. |
| 553 |
If you provide a qualified name that resolves to an element |
| 554 |
by the name resolution rule above, |
| 555 |
you can access the element. |
| 556 |
.PP |
| 557 |
You can access a namespace variable |
| 558 |
from a procedure in the same namespace |
| 559 |
by using the \fBvariable\fR command. |
| 560 |
Much like the \fBglobal\fR command, |
| 561 |
this creates a local link to the namespace variable. |
| 562 |
If necessary, it also creates the variable in the current namespace |
| 563 |
and initializes it. |
| 564 |
Note that the \fBglobal\fR command only creates links |
| 565 |
to variables in the global namespace. |
| 566 |
It is not necessary to use a \fBvariable\fR command |
| 567 |
if you always refer to the namespace variable using an |
| 568 |
appropriate qualified name. |
| 569 |
.SH "IMPORTING COMMANDS" |
| 570 |
.PP |
| 571 |
Namespaces are often used to represent libraries. |
| 572 |
Some library commands are used so frequently |
| 573 |
that it is a nuisance to type their qualified names. |
| 574 |
For example, suppose that all of the commands in a package |
| 575 |
like BLT are contained in a namespace called \fBBlt\fR. |
| 576 |
Then you might access these commands like this: |
| 577 |
.PP |
| 578 |
.CS |
| 579 |
Blt::graph .g \-background red |
| 580 |
Blt::table . .g 0,0 |
| 581 |
.CE |
| 582 |
.PP |
| 583 |
If you use the \fBgraph\fR and \fBtable\fR commands frequently, |
| 584 |
you may want to access them without the \fBBlt::\fR prefix. |
| 585 |
You can do this by importing the commands into the current namespace, |
| 586 |
like this: |
| 587 |
.PP |
| 588 |
.CS |
| 589 |
\fBnamespace import\fR Blt::* |
| 590 |
.CE |
| 591 |
.PP |
| 592 |
This adds all exported commands from the \fBBlt\fR namespace |
| 593 |
into the current namespace context, so you can write code like this: |
| 594 |
.PP |
| 595 |
.CS |
| 596 |
graph .g \-background red |
| 597 |
table . .g 0,0 |
| 598 |
.CE |
| 599 |
.PP |
| 600 |
The \fBnamespace import\fR command only imports commands |
| 601 |
from a namespace that that namespace exported |
| 602 |
with a \fBnamespace export\fR command. |
| 603 |
.PP |
| 604 |
Importing \fIevery\fR command from a namespace is generally |
| 605 |
a bad idea since you do not know what you will get. |
| 606 |
It is better to import just the specific commands you need. |
| 607 |
For example, the command |
| 608 |
.PP |
| 609 |
.CS |
| 610 |
\fBnamespace import\fR Blt::graph Blt::table |
| 611 |
.CE |
| 612 |
.PP |
| 613 |
imports only the \fBgraph\fR and \fBtable\fR commands into the |
| 614 |
current context. |
| 615 |
.PP |
| 616 |
If you try to import a command that already exists, you will get an |
| 617 |
error. This prevents you from importing the same command from two |
| 618 |
different packages. But from time to time (perhaps when debugging), |
| 619 |
you may want to get around this restriction. You may want to |
| 620 |
reissue the \fBnamespace import\fR command to pick up new commands |
| 621 |
that have appeared in a namespace. In that case, you can use the |
| 622 |
\fB\-force\fR option, and existing commands will be silently overwritten: |
| 623 |
.PP |
| 624 |
.CS |
| 625 |
\fBnamespace import\fR \-force Blt::graph Blt::table |
| 626 |
.CE |
| 627 |
.PP |
| 628 |
If for some reason, you want to stop using the imported commands, |
| 629 |
you can remove them with a \fBnamespace forget\fR command, like this: |
| 630 |
.PP |
| 631 |
.CS |
| 632 |
\fBnamespace forget\fR Blt::* |
| 633 |
.CE |
| 634 |
.PP |
| 635 |
This searches the current namespace for any commands imported from \fBBlt\fR. |
| 636 |
If it finds any, it removes them. Otherwise, it does nothing. |
| 637 |
After this, the \fBBlt\fR commands must be accessed with the \fBBlt::\fR |
| 638 |
prefix. |
| 639 |
.PP |
| 640 |
When you delete a command from the exporting namespace like this: |
| 641 |
.PP |
| 642 |
.CS |
| 643 |
rename Blt::graph "" |
| 644 |
.CE |
| 645 |
.PP |
| 646 |
the command is automatically removed from all namespaces that import it. |
| 647 |
.SH "EXPORTING COMMANDS" |
| 648 |
You can export commands from a namespace like this: |
| 649 |
.PP |
| 650 |
.CS |
| 651 |
\fBnamespace eval\fR Counter { |
| 652 |
\fBnamespace export\fR bump reset |
| 653 |
variable Num 0 |
| 654 |
variable Max 100 |
| 655 |
|
| 656 |
proc bump {{by 1}} { |
| 657 |
variable Num |
| 658 |
incr Num $by |
| 659 |
Check |
| 660 |
return $Num |
| 661 |
} |
| 662 |
proc reset {} { |
| 663 |
variable Num |
| 664 |
set Num 0 |
| 665 |
} |
| 666 |
proc Check {} { |
| 667 |
variable Num |
| 668 |
variable Max |
| 669 |
if {$Num > $Max} { |
| 670 |
error "too high!" |
| 671 |
} |
| 672 |
} |
| 673 |
} |
| 674 |
.CE |
| 675 |
.PP |
| 676 |
The procedures \fBbump\fR and \fBreset\fR are exported, |
| 677 |
so they are included when you import from the \fBCounter\fR namespace, |
| 678 |
like this: |
| 679 |
.PP |
| 680 |
.CS |
| 681 |
\fBnamespace import\fR Counter::* |
| 682 |
.CE |
| 683 |
.PP |
| 684 |
However, the \fBCheck\fR procedure is not exported, |
| 685 |
so it is ignored by the import operation. |
| 686 |
.PP |
| 687 |
The \fBnamespace import\fR command only imports commands |
| 688 |
that were declared as exported by their namespace. |
| 689 |
The \fBnamespace export\fR command specifies what commands |
| 690 |
may be imported by other namespaces. |
| 691 |
If a \fBnamespace import\fR command specifies a command |
| 692 |
that is not exported, the command is not imported. |
| 693 |
.SH "SCOPED SCRIPTS" |
| 694 |
.PP |
| 695 |
The \fBnamespace code\fR command is the means by which a script may be |
| 696 |
packaged for evaluation in a namespace other than the one in which it |
| 697 |
was created. It is used most often to create event handlers, Tk bindings, |
| 698 |
and traces for evaluation in the global context. For instance, the following |
| 699 |
code indicates how to direct a variable \fBtrace\fR callback into the current |
| 700 |
namespace: |
| 701 |
.PP |
| 702 |
.CS |
| 703 |
\fBnamespace eval\fR a { |
| 704 |
variable b |
| 705 |
proc theTraceCallback { n1 n2 op } { |
| 706 |
upvar 1 $n1 var |
| 707 |
puts "the value of $n1 has changed to $var" |
| 708 |
return |
| 709 |
} |
| 710 |
trace add variable b write [\fBnamespace code\fR theTraceCallback] |
| 711 |
} |
| 712 |
set a::b c |
| 713 |
.CE |
| 714 |
.PP |
| 715 |
When executed, it prints the message: |
| 716 |
.PP |
| 717 |
.CS |
| 718 |
the value of a::b has changed to c |
| 719 |
.CE |
| 720 |
.SH ENSEMBLES |
| 721 |
.PP |
| 722 |
The \fBnamespace ensemble\fR is used to create and manipulate ensemble |
| 723 |
commands, which are commands formed by grouping subcommands together. |
| 724 |
The commands typically come from the current namespace when the |
| 725 |
ensemble was created, though this is configurable. Note that there |
| 726 |
may be any number of ensembles associated with any namespace |
| 727 |
(including none, which is true of all namespaces by default), though |
| 728 |
all the ensembles associated with a namespace are deleted when that |
| 729 |
namespace is deleted. The link between an ensemble command and its |
| 730 |
namespace is maintained however the ensemble is renamed. |
| 731 |
.PP |
| 732 |
Three subcommands of the \fBnamespace ensemble\fR command are defined: |
| 733 |
.TP |
| 734 |
\fBnamespace ensemble create\fR ?\fIoption value ...\fR? |
| 735 |
. |
| 736 |
Creates a new ensemble command linked to the current namespace, |
| 737 |
returning the fully qualified name of the command created. The |
| 738 |
arguments to \fBnamespace ensemble create\fR allow the configuration |
| 739 |
of the command as if with the \fBnamespace ensemble configure\fR |
| 740 |
command. If not overridden with the \fB\-command\fR option, this |
| 741 |
command creates an ensemble with exactly the same name as the linked |
| 742 |
namespace. See the section \fBENSEMBLE OPTIONS\fR below for a full |
| 743 |
list of options supported and their effects. |
| 744 |
.TP |
| 745 |
\fBnamespace ensemble configure \fIcommand\fR ?\fIoption\fR? ?\fIvalue ...\fR? |
| 746 |
. |
| 747 |
Retrieves the value of an option associated with the ensemble command |
| 748 |
named \fIcommand\fR, or updates some options associated with that |
| 749 |
ensemble command. See the section \fBENSEMBLE OPTIONS\fR below for a |
| 750 |
full list of options supported and their effects. |
| 751 |
.TP |
| 752 |
\fBnamespace ensemble exists\fR \fIcommand\fR |
| 753 |
. |
| 754 |
Returns a boolean value that describes whether the command |
| 755 |
\fIcommand\fR exists and is an ensemble command. This command only |
| 756 |
ever returns an error if the number of arguments to the command is |
| 757 |
wrong. |
| 758 |
.PP |
| 759 |
When called, an ensemble command takes its first argument and looks it |
| 760 |
up (according to the rules described below) to discover a list of |
| 761 |
words to replace the ensemble command and subcommand with. The |
| 762 |
resulting list of words is then evaluated (with no further |
| 763 |
substitutions) as if that was what was typed originally (i.e. by |
| 764 |
passing the list of words through \fBTcl_EvalObjv\fR) and returning |
| 765 |
the result of the command. Note that it is legal to make the target |
| 766 |
of an ensemble rewrite be another (or even the same) ensemble |
| 767 |
command. The ensemble command will not be visible through the use of |
| 768 |
the \fBuplevel\fR or \fBinfo level\fR commands. |
| 769 |
.SS "ENSEMBLE OPTIONS" |
| 770 |
.PP |
| 771 |
The following options, supported by the \fBnamespace ensemble |
| 772 |
create\fR and \fBnamespace ensemble configure\fR commands, control how |
| 773 |
an ensemble command behaves: |
| 774 |
.TP |
| 775 |
\fB\-map\fR |
| 776 |
. |
| 777 |
When non-empty, this option supplies a dictionary that provides a |
| 778 |
mapping from subcommand names to a list of prefix words to substitute |
| 779 |
in place of the ensemble command and subcommand words (in a manner |
| 780 |
similar to an alias created with \fBinterp alias\fR; the words are not |
| 781 |
reparsed after substitution); if the first word of any target is not |
| 782 |
fully qualified when set, it is assumed to be relative to the |
| 783 |
\fIcurrent\fR namespace and changed to be exactly that (that is, it is |
| 784 |
always fully qualified when read). When this option is empty, the mapping |
| 785 |
will be from the local name of the subcommand to its fully-qualified |
| 786 |
name. Note that when this option is non-empty and the |
| 787 |
\fB\-subcommands\fR option is empty, the ensemble subcommand names |
| 788 |
will be exactly those words that have mappings in the dictionary. |
| 789 |
.TP |
| 790 |
\fB\-parameters\fR |
| 791 |
.VS 8.6 |
| 792 |
This option gives a list of named arguments (the names being used during |
| 793 |
generation of error messages) that are passed by the caller of the ensemble |
| 794 |
between the name of the ensemble and the subcommand argument. By default, it |
| 795 |
is the empty list. |
| 796 |
.VE 8.6 |
| 797 |
.TP |
| 798 |
\fB\-prefixes\fR |
| 799 |
. |
| 800 |
This option (which is enabled by default) controls whether the |
| 801 |
ensemble command recognizes unambiguous prefixes of its subcommands. |
| 802 |
When turned off, the ensemble command requires exact matching of |
| 803 |
subcommand names. |
| 804 |
.TP |
| 805 |
\fB\-subcommands\fR |
| 806 |
. |
| 807 |
When non-empty, this option lists exactly what subcommands are in the |
| 808 |
ensemble. The mapping for each of those commands will be either whatever |
| 809 |
is defined in the \fB\-map\fR option, or to the command with the same |
| 810 |
name in the namespace linked to the ensemble. If this option is |
| 811 |
empty, the subcommands of the namespace will either be the keys of the |
| 812 |
dictionary listed in the \fB\-map\fR option or the exported commands |
| 813 |
of the linked namespace at the time of the invocation of the ensemble |
| 814 |
command. |
| 815 |
.TP |
| 816 |
\fB\-unknown\fR |
| 817 |
. |
| 818 |
When non-empty, this option provides a partial command (to which all |
| 819 |
the words that are arguments to the ensemble command, including the |
| 820 |
fully-qualified name of the ensemble, are appended) to handle the case |
| 821 |
where an ensemble subcommand is not recognized and would otherwise |
| 822 |
generate an error. When empty (the default) an error (in the style of |
| 823 |
\fBTcl_GetIndexFromObj\fR) is generated whenever the ensemble is |
| 824 |
unable to determine how to implement a particular subcommand. See |
| 825 |
\fBUNKNOWN HANDLER BEHAVIOUR\fR for more details. |
| 826 |
.PP |
| 827 |
The following extra option is allowed by \fBnamespace ensemble |
| 828 |
create\fR: |
| 829 |
.TP |
| 830 |
\fB\-command\fR |
| 831 |
. |
| 832 |
This write-only option allows the name of the ensemble created by |
| 833 |
\fBnamespace ensemble create\fR to be anything in any existing |
| 834 |
namespace. The default value for this option is the fully-qualified |
| 835 |
name of the namespace in which the \fBnamespace ensemble create\fR |
| 836 |
command is invoked. |
| 837 |
.PP |
| 838 |
The following extra option is allowed by \fBnamespace ensemble |
| 839 |
configure\fR: |
| 840 |
.TP |
| 841 |
\fB\-namespace\fR |
| 842 |
. |
| 843 |
This read-only option allows the retrieval of the fully-qualified name |
| 844 |
of the namespace which the ensemble was created within. |
| 845 |
.SS "UNKNOWN HANDLER BEHAVIOUR" |
| 846 |
.PP |
| 847 |
If an unknown handler is specified for an ensemble, that handler is |
| 848 |
called when the ensemble command would otherwise return an error due |
| 849 |
to it being unable to decide which subcommand to invoke. The exact |
| 850 |
conditions under which that occurs are controlled by the |
| 851 |
\fB\-subcommands\fR, \fB\-map\fR and \fB\-prefixes\fR options as |
| 852 |
described above. |
| 853 |
.PP |
| 854 |
To execute the unknown handler, the ensemble mechanism takes the |
| 855 |
specified \fB\-unknown\fR option and appends each argument of the |
| 856 |
attempted ensemble command invocation (including the ensemble command |
| 857 |
itself, expressed as a fully qualified name). It invokes the resulting |
| 858 |
command in the scope of the attempted call. If the execution of the |
| 859 |
unknown handler terminates normally, the ensemble engine reparses the |
| 860 |
subcommand (as described below) and tries to dispatch it again, which |
| 861 |
is ideal for when the ensemble's configuration has been updated by the |
| 862 |
unknown subcommand handler. Any other kind of termination of the |
| 863 |
unknown handler is treated as an error. |
| 864 |
.PP |
| 865 |
The result of the unknown handler is expected to be a list (it is an |
| 866 |
error if it is not). If the list is an empty list, the ensemble |
| 867 |
command attempts to look up the original subcommand again and, if it |
| 868 |
is not found this time, an error will be generated just as if the |
| 869 |
\fB\-unknown\fR handler was not there (i.e. for any particular |
| 870 |
invocation of an ensemble, its unknown handler will be called at most |
| 871 |
once.) This makes it easy for the unknown handler to update the |
| 872 |
ensemble or its backing namespace so as to provide an implementation |
| 873 |
of the desired subcommand and reparse. |
| 874 |
.PP |
| 875 |
When the result is a non-empty list, the words of that list are used |
| 876 |
to replace the ensemble command and subcommand, just as if they had |
| 877 |
been looked up in the \fB\-map\fR. It is up to the unknown handler to |
| 878 |
supply all namespace qualifiers if the implementing subcommand is not |
| 879 |
in the namespace of the caller of the ensemble command. Also note that |
| 880 |
when ensemble commands are chained (e.g. if you make one of the |
| 881 |
commands that implement an ensemble subcommand into an ensemble, in a |
| 882 |
manner similar to the \fBtext\fR widget's tag and mark subcommands) then the |
| 883 |
rewrite happens in the context of the caller of the outermost |
| 884 |
ensemble. That is to say that ensembles do not in themselves place any |
| 885 |
namespace contexts on the Tcl call stack. |
| 886 |
.PP |
| 887 |
Where an empty \fB\-unknown\fR handler is given (the default), the |
| 888 |
ensemble command will generate an error message based on the list of |
| 889 |
commands that the ensemble has defined (formatted similarly to the |
| 890 |
error message from \fBTcl_GetIndexFromObj\fR). This is the error that |
| 891 |
will be thrown when the subcommand is still not recognized during |
| 892 |
reparsing. It is also an error for an \fB\-unknown\fR handler to |
| 893 |
delete its namespace. |
| 894 |
.SH EXAMPLES |
| 895 |
Create a namespace containing a variable and an exported command: |
| 896 |
.PP |
| 897 |
.CS |
| 898 |
\fBnamespace eval\fR foo { |
| 899 |
variable bar 0 |
| 900 |
proc grill {} { |
| 901 |
variable bar |
| 902 |
puts "called [incr bar] times" |
| 903 |
} |
| 904 |
\fBnamespace export\fR grill |
| 905 |
} |
| 906 |
.CE |
| 907 |
.PP |
| 908 |
Call the command defined in the previous example in various ways. |
| 909 |
.PP |
| 910 |
.CS |
| 911 |
# Direct call |
| 912 |
::foo::grill |
| 913 |
|
| 914 |
# Use the command resolution path to find the name |
| 915 |
\fBnamespace eval\fR boo { |
| 916 |
\fBnamespace path\fR ::foo |
| 917 |
grill |
| 918 |
} |
| 919 |
|
| 920 |
# Import into current namespace, then call local alias |
| 921 |
\fBnamespace import\fR foo::grill |
| 922 |
grill |
| 923 |
|
| 924 |
# Create two ensembles, one with the default name and one with a |
| 925 |
# specified name. Then call through the ensembles. |
| 926 |
\fBnamespace eval\fR foo { |
| 927 |
\fBnamespace ensemble\fR create |
| 928 |
\fBnamespace ensemble\fR create -command ::foobar |
| 929 |
} |
| 930 |
foo grill |
| 931 |
foobar grill |
| 932 |
.CE |
| 933 |
.PP |
| 934 |
Look up where the command imported in the previous example came from: |
| 935 |
.PP |
| 936 |
.CS |
| 937 |
puts "grill came from [\fBnamespace origin\fR grill]" |
| 938 |
.CE |
| 939 |
.PP |
| 940 |
Remove all imported commands from the current namespace: |
| 941 |
.PP |
| 942 |
.CS |
| 943 |
namespace forget {*}[namespace import] |
| 944 |
.CE |
| 945 |
.PP |
| 946 |
.VS 8.6 |
| 947 |
Create an ensemble for simple working with numbers, using the |
| 948 |
\fB\-parameters\fR option to allow the operator to be put between the first |
| 949 |
and second arguments. |
| 950 |
.PP |
| 951 |
.CS |
| 952 |
\fBnamespace eval\fR do { |
| 953 |
\fBnamespace export\fR * |
| 954 |
\fBnamespace ensemble\fR create -parameters x |
| 955 |
proc plus {x y} {expr { $x + $y }} |
| 956 |
proc minus {x y} {expr { $x - $y }} |
| 957 |
} |
| 958 |
|
| 959 |
# In use, the ensemble works like this: |
| 960 |
puts [do 1 plus [do 9 minus 7]] |
| 961 |
.CE |
| 962 |
.VE 8.6 |
| 963 |
.SH "SEE ALSO" |
| 964 |
interp(n), upvar(n), variable(n) |
| 965 |
.SH KEYWORDS |
| 966 |
command, ensemble, exported, internal, variable |
| 967 |
'\" Local Variables: |
| 968 |
'\" mode: nroff |
| 969 |
'\" End: |