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