Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: downloads/tcl8.5.2/doc/namespace.n @ 42

Last change on this file since 42 was 25, checked in by landauf, 17 years ago

added tcl to libs

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