1 |
578 |
markom |
'\"
|
2 |
|
|
'\" Copyright (c) 1993-1997 Bell Labs Innovations for Lucent Technologies
|
3 |
|
|
'\" Copyright (c) 1997 Sun Microsystems, Inc.
|
4 |
|
|
'\"
|
5 |
|
|
'\" See the file "license.terms" for information on usage and redistribution
|
6 |
|
|
'\" of this file, and for a DISCLAIMER OF ALL WARRANTIES.
|
7 |
|
|
'\"
|
8 |
|
|
'\" RCS: @(#) $Id: namespace.n,v 1.1.1.1 2002-01-16 10:25:24 markom Exp $
|
9 |
|
|
'\"
|
10 |
|
|
.so man.macros
|
11 |
|
|
.TH namespace n 8.0 Tcl "Tcl Built-In Commands"
|
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?\fIoption\fR? ?\fIarg ...\fR?
|
18 |
|
|
.BE
|
19 |
|
|
|
20 |
|
|
.SH DESCRIPTION
|
21 |
|
|
.PP
|
22 |
|
|
The \fBnamespace\fR command lets you create, access, and destroy
|
23 |
|
|
separate contexts for commands and variables.
|
24 |
|
|
See the section \fBWHAT IS A NAMESPACE?\fR below
|
25 |
|
|
for a brief overview of namespaces.
|
26 |
|
|
The legal \fIoption\fR's are listed below.
|
27 |
|
|
Note that you can abbreviate the \fIoption\fR's.
|
28 |
|
|
.TP
|
29 |
|
|
\fBnamespace children \fR?\fInamespace\fR? ?\fIpattern\fR?
|
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 \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 \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 the pattern.
|
43 |
|
|
.TP
|
44 |
|
|
\fBnamespace code \fIscript\fR
|
45 |
|
|
Captures the current namespace context for later execution
|
46 |
|
|
of the script \fIscript\fR.
|
47 |
|
|
It returns a new script in which \fIscript\fR has been wrapped
|
48 |
|
|
in a \fBnamespace code\fR command.
|
49 |
|
|
The new script has two important properties.
|
50 |
|
|
First, it can be evaluated in any namespace and will cause
|
51 |
|
|
\fIscript\fR to be evaluated in the current namespace
|
52 |
|
|
(the one where the \fBnamespace code\fR command was invoked).
|
53 |
|
|
Second, additional arguments can be appended to the resulting script
|
54 |
|
|
and they will be passed to \fIscript\fR as additional arguments.
|
55 |
|
|
For example, suppose the command
|
56 |
|
|
\fBset script [namespace code {foo bar}]\fR
|
57 |
|
|
is invoked in namespace \fB::a::b\fR.
|
58 |
|
|
Then \fBeval "$script x y"\fR
|
59 |
|
|
can be executed in any namespace (assuming the value of
|
60 |
|
|
\fBscript\fR has been passed in properly)
|
61 |
|
|
and will have the same effect as the command
|
62 |
|
|
\fBnamespace eval ::a::b {foo bar x y}\fR.
|
63 |
|
|
This command is needed because
|
64 |
|
|
extensions like Tk normally execute callback scripts
|
65 |
|
|
in the global namespace.
|
66 |
|
|
A scoped command captures a command together with its namespace context
|
67 |
|
|
in a way that allows it to be executed properly later.
|
68 |
|
|
See the section \fBSCOPED VALUES\fR for some examples
|
69 |
|
|
of how this is used to create callback scripts.
|
70 |
|
|
.TP
|
71 |
|
|
\fBnamespace current\fR
|
72 |
|
|
Returns the fully-qualified name for the current namespace.
|
73 |
|
|
The actual name of the global namespace is ``''
|
74 |
|
|
(i.e., an empty string),
|
75 |
|
|
but this command returns \fB::\fR for the global namespace
|
76 |
|
|
as a convenience to programmers.
|
77 |
|
|
.TP
|
78 |
|
|
\fBnamespace delete \fR?\fInamespace namespace ...\fR?
|
79 |
|
|
Each namespace \fInamespace\fR is deleted
|
80 |
|
|
and all variables, procedures, and child namespaces
|
81 |
|
|
contained in the namespace are deleted.
|
82 |
|
|
If a procedure is currently executing inside the namespace,
|
83 |
|
|
the namespace will be kept alive until the procedure returns;
|
84 |
|
|
however, the namespace is marked to prevent other code from
|
85 |
|
|
looking it up by name.
|
86 |
|
|
If a namespace doesn't exist, this command returns an error.
|
87 |
|
|
If no namespace names are given, this command does nothing.
|
88 |
|
|
.TP
|
89 |
|
|
\fBnamespace eval\fR \fInamespace arg\fR ?\fIarg ...\fR?
|
90 |
|
|
Activates a namespace called \fInamespace\fR and evaluates some code
|
91 |
|
|
in that context.
|
92 |
|
|
If the namespace does not already exist, it is created.
|
93 |
|
|
If more than one \fIarg\fR argument is specified,
|
94 |
|
|
the arguments are concatenated together with a space between each one
|
95 |
|
|
in the same fashion as the \fBeval\fR command,
|
96 |
|
|
and the result is evaluated.
|
97 |
|
|
.br
|
98 |
|
|
.sp
|
99 |
|
|
If \fInamespace\fR has leading namespace qualifiers
|
100 |
|
|
and any leading namespaces do not exist,
|
101 |
|
|
they are automatically created.
|
102 |
|
|
.TP
|
103 |
|
|
\fBnamespace export \fR?\-\fBclear\fR? ?\fIpattern pattern ...\fR?
|
104 |
|
|
Specifies which commands are exported from a namespace.
|
105 |
|
|
The exported commands are those that can be later imported
|
106 |
|
|
into another namespace using a \fBnamespace import\fR command.
|
107 |
|
|
Both commands defined in a namespace and
|
108 |
|
|
commands the namespace has previously imported
|
109 |
|
|
can be exported by a namespace.
|
110 |
|
|
The commands do not have to be defined
|
111 |
|
|
at the time the \fBnamespace export\fR command is executed.
|
112 |
|
|
Each \fIpattern\fR may contain glob-style special characters,
|
113 |
|
|
but it may not include any namespace qualifiers.
|
114 |
|
|
That is, the pattern can only specify commands
|
115 |
|
|
in the current (exporting) namespace.
|
116 |
|
|
Each \fIpattern\fR is appended onto the namespace's list of export patterns.
|
117 |
|
|
If the \-\fBclear\fR flag is given,
|
118 |
|
|
the namespace's export pattern list is reset to empty before any
|
119 |
|
|
\fIpattern\fR arguments are appended.
|
120 |
|
|
If no \fIpattern\fRs are given and the \-\fBclear\fR flag isn't given,
|
121 |
|
|
this command returns the namespace's current export list.
|
122 |
|
|
.TP
|
123 |
|
|
\fBnamespace forget \fR?\fIpattern pattern ...\fR?
|
124 |
|
|
Removes previously imported commands from a namespace.
|
125 |
|
|
Each \fIpattern\fR is a qualified name such as
|
126 |
|
|
\fBfoo::x\fR or \fBa::b::p*\fR.
|
127 |
|
|
Qualified names contain \fB::\fRs and qualify a name
|
128 |
|
|
with the name of one or more namespaces.
|
129 |
|
|
Each \fIpattern\fR is qualified with the name of an exporting namespace
|
130 |
|
|
and may have glob-style special characters in the command name
|
131 |
|
|
at the end of the qualified name.
|
132 |
|
|
Glob characters may not appear in a namespace name.
|
133 |
|
|
This command first finds the matching exported commands.
|
134 |
|
|
It then checks whether any of those those commands
|
135 |
|
|
were previously imported by the current namespace.
|
136 |
|
|
If so, this command deletes the corresponding imported commands.
|
137 |
|
|
In effect, this un-does the action of a \fBnamespace import\fR command.
|
138 |
|
|
.TP
|
139 |
|
|
\fBnamespace import \fR?\fB\-force\fR? ?\fIpattern\fR \fIpattern ...\fR?
|
140 |
|
|
Imports commands into a namespace.
|
141 |
|
|
Each \fIpattern\fR is a qualified name like
|
142 |
|
|
\fBfoo::x\fR or \fBa::p*\fR.
|
143 |
|
|
That is, it includes the name of an exporting namespace
|
144 |
|
|
and may have glob-style special characters in the command name
|
145 |
|
|
at the end of the qualified name.
|
146 |
|
|
Glob characters may not appear in a namespace name.
|
147 |
|
|
All the commands that match a \fIpattern\fR string
|
148 |
|
|
and which are currently exported from their namespace
|
149 |
|
|
are added to the current namespace.
|
150 |
|
|
This is done by creating a new command in the current namespace
|
151 |
|
|
that points to the exported command in its original namespace;
|
152 |
|
|
when the new imported command is called, it invokes the exported command.
|
153 |
|
|
This command normally returns an error
|
154 |
|
|
if an imported command conflicts with an existing command.
|
155 |
|
|
However, if the \-\fBforce\fR option is given,
|
156 |
|
|
imported commands will silently replace existing commands.
|
157 |
|
|
The \fBnamespace import\fR command has snapshot semantics:
|
158 |
|
|
that is, only requested commands that are currently defined
|
159 |
|
|
in the exporting namespace are imported.
|
160 |
|
|
In other words, you can import only the commands that are in a namespace
|
161 |
|
|
at the time when the \fBnamespace import\fR command is executed.
|
162 |
|
|
If another command is defined and exported in this namespace later on,
|
163 |
|
|
it will not be imported.
|
164 |
|
|
.TP
|
165 |
|
|
\fBnamespace inscope\fR \fInamespace arg\fR ?\fIarg ...\fR?
|
166 |
|
|
Executes a script in the context of a particular namespace.
|
167 |
|
|
This command is not expected to be used directly by programmers;
|
168 |
|
|
calls to it are generated implicitly when applications
|
169 |
|
|
use \fBnamespace code\fR commands to create callback scripts
|
170 |
|
|
that the applications then register with, e.g., Tk widgets.
|
171 |
|
|
The \fBnamespace inscope\fR command is much like the \fBnamespace eval\fR
|
172 |
|
|
command except that it has \fBlappend\fR semantics
|
173 |
|
|
and the namespace must already exist.
|
174 |
|
|
It treats the first argument as a list,
|
175 |
|
|
and appends any arguments after the first
|
176 |
|
|
onto the end as proper list elements.
|
177 |
|
|
\fBnamespace inscope ::foo a x y z\fR
|
178 |
|
|
is equivalent to
|
179 |
|
|
\fBnamespace eval ::foo [concat a [list x y z]]\fR
|
180 |
|
|
This \fBlappend\fR semantics is important because many callback scripts
|
181 |
|
|
are actually prefixes.
|
182 |
|
|
.TP
|
183 |
|
|
\fBnamespace origin \fIcommand\fR
|
184 |
|
|
Returns the fully-qualified name of the original command
|
185 |
|
|
to which the imported command \fIcommand\fR refers.
|
186 |
|
|
When a command is imported into a namespace,
|
187 |
|
|
a new command is created in that namespace
|
188 |
|
|
that points to the actual command in the exporting namespace.
|
189 |
|
|
If a command is imported into a sequence of namespaces
|
190 |
|
|
\fIa, b,...,n\fR where each successive namespace
|
191 |
|
|
just imports the command from the previous namespace,
|
192 |
|
|
this command returns the fully-qualified name of the original command
|
193 |
|
|
in the first namespace, \fIa\fR.
|
194 |
|
|
If \fIcommand\fR does not refer to an imported command,
|
195 |
|
|
the command's own fully-qualified name is returned.
|
196 |
|
|
.TP
|
197 |
|
|
\fBnamespace parent\fR ?\fInamespace\fR?
|
198 |
|
|
Returns the fully-qualified name of the parent namespace
|
199 |
|
|
for namespace \fInamespace\fR.
|
200 |
|
|
If \fInamespace\fR is not specified,
|
201 |
|
|
the fully-qualified name of the current namespace's parent is returned.
|
202 |
|
|
.TP
|
203 |
|
|
\fBnamespace qualifiers\fR \fIstring\fR
|
204 |
|
|
Returns any leading namespace qualifiers for \fIstring\fR.
|
205 |
|
|
Qualifiers are namespace names separated by \fB::\fRs.
|
206 |
|
|
For the \fIstring\fR \fB::foo::bar::x\fR,
|
207 |
|
|
this command returns \fB::foo::bar\fR,
|
208 |
|
|
and for \fB::\fR it returns \fB``''\fR (an empty string).
|
209 |
|
|
This command is the complement of the \fBnamespace tail\fR command.
|
210 |
|
|
Note that it does not check whether the
|
211 |
|
|
namespace names are, in fact,
|
212 |
|
|
the names of currently defined namespaces.
|
213 |
|
|
.TP
|
214 |
|
|
\fBnamespace tail\fR \fIstring\fR
|
215 |
|
|
Returns the simple name at the end of a qualified string.
|
216 |
|
|
Qualifiers are namespace names separated by \fB::\fRs.
|
217 |
|
|
For the \fIstring\fR \fB::foo::bar::x\fR,
|
218 |
|
|
this command returns \fBx\fR,
|
219 |
|
|
and for \fB::\fR it returns \fB``''\fR (an empty string).
|
220 |
|
|
This command is the complement of the \fBnamespace qualifiers\fR command.
|
221 |
|
|
It does not check whether the namespace names are, in fact,
|
222 |
|
|
the names of currently defined namespaces.
|
223 |
|
|
.TP
|
224 |
|
|
\fBnamespace which\fR ?\-\fBcommand\fR? ?\-\fBvariable\fR? \fIname\fR
|
225 |
|
|
Looks up \fIname\fR as either a command or variable
|
226 |
|
|
and returns its fully-qualified name.
|
227 |
|
|
For example, if \fIname\fR does not exist in the current namespace
|
228 |
|
|
but does exist in the global namespace,
|
229 |
|
|
this command returns a fully-qualified name in the global namespace.
|
230 |
|
|
If the command or variable does not exist,
|
231 |
|
|
this command returns an empty string.
|
232 |
|
|
If no flag is given, \fIname\fR is treated as a command name.
|
233 |
|
|
See the section \fBNAME RESOLUTION\fR below for an explanation of
|
234 |
|
|
the rules regarding name resolution.
|
235 |
|
|
|
236 |
|
|
.SH "WHAT IS A NAMESPACE?"
|
237 |
|
|
.PP
|
238 |
|
|
A namespace is a collection of commands and variables.
|
239 |
|
|
It encapsulates the commands and variables to ensure that they
|
240 |
|
|
won't interfere with the commands and variables of other namespaces.
|
241 |
|
|
Tcl has always had one such collection,
|
242 |
|
|
which we refer to as the \fIglobal namespace\fR.
|
243 |
|
|
The global namespace holds all global variables and commands.
|
244 |
|
|
The \fBnamespace eval\fR command lets you create new namespaces.
|
245 |
|
|
For example,
|
246 |
|
|
.CS
|
247 |
|
|
\fBnamespace eval Counter {
|
248 |
|
|
namespace export Bump
|
249 |
|
|
variable num 0
|
250 |
|
|
|
251 |
|
|
proc Bump {} {
|
252 |
|
|
variable num
|
253 |
|
|
incr num
|
254 |
|
|
}
|
255 |
|
|
}\fR
|
256 |
|
|
.CE
|
257 |
|
|
creates a new namespace containing the variable \fBnum\fR and
|
258 |
|
|
the procedure \fBBump\fR.
|
259 |
|
|
The commands and variables in this namespace are separate from
|
260 |
|
|
other commands and variables in the same program.
|
261 |
|
|
If there is a command named \fBBump\fR in the global namespace,
|
262 |
|
|
for example, it will be different from the command \fBBump\fR
|
263 |
|
|
in the \fBCounter\fR namespace.
|
264 |
|
|
.PP
|
265 |
|
|
Namespace variables resemble global variables in Tcl.
|
266 |
|
|
They exist outside of the procedures in a namespace
|
267 |
|
|
but can be accessed in a procedure via the \fBvariable\fR command,
|
268 |
|
|
as shown in the example above.
|
269 |
|
|
.PP
|
270 |
|
|
Namespaces are dynamic.
|
271 |
|
|
You can add and delete commands and variables at any time,
|
272 |
|
|
so you can build up the contents of a
|
273 |
|
|
namespace over time using a series of \fBnamespace eval\fR commands.
|
274 |
|
|
For example, the following series of commands has the same effect
|
275 |
|
|
as the namespace definition shown above:
|
276 |
|
|
.CS
|
277 |
|
|
\fBnamespace eval Counter {
|
278 |
|
|
variable num 0
|
279 |
|
|
proc Bump {} {
|
280 |
|
|
variable num
|
281 |
|
|
return [incr num]
|
282 |
|
|
}
|
283 |
|
|
}
|
284 |
|
|
namespace eval Counter {
|
285 |
|
|
proc test {args} {
|
286 |
|
|
return $args
|
287 |
|
|
}
|
288 |
|
|
}
|
289 |
|
|
namespace eval Counter {
|
290 |
|
|
rename test ""
|
291 |
|
|
}\fR
|
292 |
|
|
.CE
|
293 |
|
|
Note that the \fBtest\fR procedure is added to the \fBCounter\fR namespace,
|
294 |
|
|
and later removed via the \fBrename\fR command.
|
295 |
|
|
.PP
|
296 |
|
|
Namespaces can have other namespaces within them,
|
297 |
|
|
so they nest hierarchically.
|
298 |
|
|
A nested namespace is encapsulated inside its parent namespace
|
299 |
|
|
and can not interfere with other namespaces.
|
300 |
|
|
|
301 |
|
|
.SH "QUALIFIED NAMES"
|
302 |
|
|
.PP
|
303 |
|
|
Each namespace has a textual name such as
|
304 |
|
|
\fBhistory\fR or \fB::safe::interp\fR.
|
305 |
|
|
Since namespaces may nest,
|
306 |
|
|
qualified names are used to refer to
|
307 |
|
|
commands, variables, and child namespaces contained inside namespaces.
|
308 |
|
|
Qualified names are similar to the hierarchical path names for
|
309 |
|
|
Unix files or Tk widgets,
|
310 |
|
|
except that \fB::\fR is used as the separator
|
311 |
|
|
instead of \fB/\fR or \fB.\fR.
|
312 |
|
|
The topmost or global namespace has the name ``'' (i.e., an empty string),
|
313 |
|
|
although \fB::\fR is a synonym.
|
314 |
|
|
As an example, the name \fB::safe::interp::create\fR
|
315 |
|
|
refers to the command \fBcreate\fR in the namespace \fBinterp\fR
|
316 |
|
|
that is a child of of namespace \fB::safe\fR,
|
317 |
|
|
which in turn is a child of the global namespace \fB::\fR.
|
318 |
|
|
.PP
|
319 |
|
|
If you want to access commands and variables from another namespace,
|
320 |
|
|
you must use some extra syntax.
|
321 |
|
|
Names must be qualified by the namespace that contains them.
|
322 |
|
|
From the global namespace,
|
323 |
|
|
we might access the \fBCounter\fR procedures like this:
|
324 |
|
|
.CS
|
325 |
|
|
\fBCounter::Bump 5
|
326 |
|
|
Counter::Reset\fR
|
327 |
|
|
.CE
|
328 |
|
|
We could access the current count like this:
|
329 |
|
|
.CS
|
330 |
|
|
\fBputs "count = $Counter::num"\fR
|
331 |
|
|
.CE
|
332 |
|
|
When one namespace contains another, you may need more than one
|
333 |
|
|
qualifier to reach its elements.
|
334 |
|
|
If we had a namespace \fBFoo\fR that contained the namespace \fBCounter\fR,
|
335 |
|
|
you could invoke its \fBBump\fR procedure
|
336 |
|
|
from the global namespace like this:
|
337 |
|
|
.CS
|
338 |
|
|
\fBFoo::Counter::Bump 3\fR
|
339 |
|
|
.CE
|
340 |
|
|
.PP
|
341 |
|
|
You can also use qualified names when you create and rename commands.
|
342 |
|
|
For example, you could add a procedure to the \fBFoo\fR
|
343 |
|
|
namespace like this:
|
344 |
|
|
.CS
|
345 |
|
|
\fBproc Foo::Test {args} {return $args}\fR
|
346 |
|
|
.CE
|
347 |
|
|
And you could move the same procedure to another namespace like this:
|
348 |
|
|
.CS
|
349 |
|
|
\fBrename Foo::Test Bar::Test\fR
|
350 |
|
|
.CE
|
351 |
|
|
.PP
|
352 |
|
|
There are a few remaining points about qualified names
|
353 |
|
|
that we should cover.
|
354 |
|
|
Namespaces have nonempty names except for the global namespace.
|
355 |
|
|
\fB::\fR is disallowed in simple command, variable, and namespace names
|
356 |
|
|
except as a namespace separator.
|
357 |
|
|
Extra \fB:\fRs in a qualified name are ignored;
|
358 |
|
|
that is, two or more \fB:\fRs are treated as a namespace separator.
|
359 |
|
|
A trailing \fB::\fR in a qualified variable or command name
|
360 |
|
|
refers to the variable or command named {}.
|
361 |
|
|
However, a trailing \fB::\fR in a qualified namespace name is ignored.
|
362 |
|
|
|
363 |
|
|
.SH "NAME RESOLUTION"
|
364 |
|
|
.PP
|
365 |
|
|
In general, all Tcl commands that take variable and command names
|
366 |
|
|
support qualified names.
|
367 |
|
|
This means you can give qualified names to such commands as
|
368 |
|
|
\fBset\fR, \fBproc\fR, \fBrename\fR, and \fBinterp alias\fR.
|
369 |
|
|
If you provide a fully-qualified name that starts with a \fB::\fR,
|
370 |
|
|
there is no question about what command, variable, or namespace
|
371 |
|
|
you mean.
|
372 |
|
|
However, if the name does not start with a \fB::\fR
|
373 |
|
|
(i.e., is \fIrelative\fR),
|
374 |
|
|
Tcl follows a fixed rule for looking it up:
|
375 |
|
|
Command and variable names are always resolved
|
376 |
|
|
by looking first in the current namespace,
|
377 |
|
|
and then in the global namespace.
|
378 |
|
|
Namespace names, on the other hand, are always resolved
|
379 |
|
|
by looking in only the current namespace.
|
380 |
|
|
.PP
|
381 |
|
|
In the following example,
|
382 |
|
|
.CS
|
383 |
|
|
\fBset traceLevel 0
|
384 |
|
|
namespace eval Debug {
|
385 |
|
|
printTrace $traceLevel
|
386 |
|
|
}\fR
|
387 |
|
|
.CE
|
388 |
|
|
Tcl looks for \fBtraceLevel\fR in the namespace \fBDebug\fR
|
389 |
|
|
and then in the global namespace.
|
390 |
|
|
It looks up the command \fBprintTrace\fR in the same way.
|
391 |
|
|
If a variable or command name is not found in either context,
|
392 |
|
|
the name is undefined.
|
393 |
|
|
To make this point absolutely clear, consider the following example:
|
394 |
|
|
.CS
|
395 |
|
|
\fBset traceLevel 0
|
396 |
|
|
namespace eval Foo {
|
397 |
|
|
variable traceLevel 3
|
398 |
|
|
|
399 |
|
|
namespace eval Debug {
|
400 |
|
|
printTrace $traceLevel
|
401 |
|
|
}
|
402 |
|
|
}\fR
|
403 |
|
|
.CE
|
404 |
|
|
Here Tcl looks for \fBtraceLevel\fR first in the namespace \fBFoo::Debug\fR.
|
405 |
|
|
Since it is not found there, Tcl then looks for it
|
406 |
|
|
in the global namespace.
|
407 |
|
|
The variable \fBFoo::traceLevel\fR is completely ignored
|
408 |
|
|
during the name resolution process.
|
409 |
|
|
.PP
|
410 |
|
|
You can use the \fBnamespace which\fR command to clear up any question
|
411 |
|
|
about name resolution.
|
412 |
|
|
For example, the command:
|
413 |
|
|
.CS
|
414 |
|
|
\fBnamespace eval Foo::Debug {namespace which \-variable traceLevel}\fR
|
415 |
|
|
.CE
|
416 |
|
|
returns \fB::traceLevel\fR.
|
417 |
|
|
On the other hand, the command,
|
418 |
|
|
.CS
|
419 |
|
|
\fBnamespace eval Foo {namespace which \-variable traceLevel}\fR
|
420 |
|
|
.CE
|
421 |
|
|
returns \fB::Foo::traceLevel\fR.
|
422 |
|
|
.PP
|
423 |
|
|
As mentioned above,
|
424 |
|
|
namespace names are looked up differently
|
425 |
|
|
than the names of variables and commands.
|
426 |
|
|
Namespace names are always resolved in the current namespace.
|
427 |
|
|
This means, for example,
|
428 |
|
|
that a \fBnamespace eval\fR command that creates a new namespace
|
429 |
|
|
always creates a child of the current namespace
|
430 |
|
|
unless the new namespace name begins with a \fB::\fR.
|
431 |
|
|
.PP
|
432 |
|
|
Tcl has no access control to limit what variables, commands,
|
433 |
|
|
or namespaces you can reference.
|
434 |
|
|
If you provide a qualified name that resolves to an element
|
435 |
|
|
by the name resolution rule above,
|
436 |
|
|
you can access the element.
|
437 |
|
|
.PP
|
438 |
|
|
You can access a namespace variable
|
439 |
|
|
from a procedure in the same namespace
|
440 |
|
|
by using the \fBvariable\fR command.
|
441 |
|
|
Much like the \fBglobal\fR command,
|
442 |
|
|
this creates a local link to the namespace variable.
|
443 |
|
|
If necessary, it also creates the variable in the current namespace
|
444 |
|
|
and initializes it.
|
445 |
|
|
Note that the \fBglobal\fR command only creates links
|
446 |
|
|
to variables in the global namespace.
|
447 |
|
|
It is not necessary to use a \fBvariable\fR command
|
448 |
|
|
if you always refer to the namespace variable using an
|
449 |
|
|
appropriate qualified name.
|
450 |
|
|
|
451 |
|
|
.SH "IMPORTING COMMANDS"
|
452 |
|
|
.PP
|
453 |
|
|
Namespaces are often used to represent libraries.
|
454 |
|
|
Some library commands are used so frequently
|
455 |
|
|
that it is a nuisance to type their qualified names.
|
456 |
|
|
For example, suppose that all of the commands in a package
|
457 |
|
|
like BLT are contained in a namespace called \fBBlt\fR.
|
458 |
|
|
Then you might access these commands like this:
|
459 |
|
|
.CS
|
460 |
|
|
\fBBlt::graph .g \-background red
|
461 |
|
|
Blt::table . .g 0,0\fR
|
462 |
|
|
.CE
|
463 |
|
|
If you use the \fBgraph\fR and \fBtable\fR commands frequently,
|
464 |
|
|
you may want to access them without the \fBBlt::\fR prefix.
|
465 |
|
|
You can do this by importing the commands into the current namespace,
|
466 |
|
|
like this:
|
467 |
|
|
.CS
|
468 |
|
|
\fBnamespace import Blt::*\fR
|
469 |
|
|
.CE
|
470 |
|
|
This adds all exported commands from the \fBBlt\fR namespace
|
471 |
|
|
into the current namespace context, so you can write code like this:
|
472 |
|
|
.CS
|
473 |
|
|
\fBgraph .g \-background red
|
474 |
|
|
table . .g 0,0\fR
|
475 |
|
|
.CE
|
476 |
|
|
The \fBnamespace import\fR command only imports commands
|
477 |
|
|
from a namespace that that namespace exported
|
478 |
|
|
with a \fBnamespace export\fR command.
|
479 |
|
|
.PP
|
480 |
|
|
Importing \fIevery\fR command from a namespace is generally
|
481 |
|
|
a bad idea since you don't know what you will get.
|
482 |
|
|
It is better to import just the specific commands you need.
|
483 |
|
|
For example, the command
|
484 |
|
|
.CS
|
485 |
|
|
\fBnamespace import Blt::graph Blt::table\fR
|
486 |
|
|
.CE
|
487 |
|
|
imports only the \fBgraph\fR and \fBtable\fR commands into the
|
488 |
|
|
current context.
|
489 |
|
|
.PP
|
490 |
|
|
If you try to import a command that already exists, you will get an
|
491 |
|
|
error. This prevents you from importing the same command from two
|
492 |
|
|
different packages. But from time to time (perhaps when debugging),
|
493 |
|
|
you may want to get around this restriction. You may want to
|
494 |
|
|
reissue the \fBnamespace import\fR command to pick up new commands
|
495 |
|
|
that have appeared in a namespace. In that case, you can use the
|
496 |
|
|
\fB\-force\fR option, and existing commands will be silently overwritten:
|
497 |
|
|
.CS
|
498 |
|
|
\fBnamespace import \-force Blt::graph Blt::table\fR
|
499 |
|
|
.CE
|
500 |
|
|
If for some reason, you want to stop using the imported commands,
|
501 |
|
|
you can remove them with an \fBnamespace forget\fR command, like this:
|
502 |
|
|
.CS
|
503 |
|
|
\fBnamespace forget Blt::*\fR
|
504 |
|
|
.CE
|
505 |
|
|
This searches the current namespace for any commands imported from \fBBlt\fR.
|
506 |
|
|
If it finds any, it removes them. Otherwise, it does nothing.
|
507 |
|
|
After this, the \fBBlt\fR commands must be accessed with the \fBBlt::\fR
|
508 |
|
|
prefix.
|
509 |
|
|
.PP
|
510 |
|
|
When you delete a command from the exporting namespace like this:
|
511 |
|
|
.CS
|
512 |
|
|
\fBrename Blt::graph ""\fR
|
513 |
|
|
.CE
|
514 |
|
|
the command is automatically removed from all namespaces that import it.
|
515 |
|
|
|
516 |
|
|
.SH "EXPORTING COMMANDS"
|
517 |
|
|
You can export commands from a namespace like this:
|
518 |
|
|
.CS
|
519 |
|
|
\fBnamespace eval Counter {
|
520 |
|
|
namespace export Bump Reset
|
521 |
|
|
variable num 0
|
522 |
|
|
variable max 100
|
523 |
|
|
|
524 |
|
|
proc Bump {{by 1}} {
|
525 |
|
|
variable num
|
526 |
|
|
incr num $by
|
527 |
|
|
check
|
528 |
|
|
return $num
|
529 |
|
|
}
|
530 |
|
|
proc Reset {} {
|
531 |
|
|
variable num
|
532 |
|
|
set num 0
|
533 |
|
|
}
|
534 |
|
|
proc check {} {
|
535 |
|
|
variable num
|
536 |
|
|
variable max
|
537 |
|
|
if {$num > $max} {
|
538 |
|
|
error "too high!"
|
539 |
|
|
}
|
540 |
|
|
}
|
541 |
|
|
}\fR
|
542 |
|
|
.CE
|
543 |
|
|
The procedures \fBBump\fR and \fBReset\fR are exported,
|
544 |
|
|
so they are included when you import from the \fBCounter\fR namespace,
|
545 |
|
|
like this:
|
546 |
|
|
.CS
|
547 |
|
|
\fBnamespace import Counter::*\fR
|
548 |
|
|
.CE
|
549 |
|
|
However, the \fBcheck\fR procedure is not exported,
|
550 |
|
|
so it is ignored by the import operation.
|
551 |
|
|
.PP
|
552 |
|
|
The \fBnamespace import\fR command only imports commands
|
553 |
|
|
that were declared as exported by their namespace.
|
554 |
|
|
The \fBnamespace export\fR command specifies what commands
|
555 |
|
|
may be imported by other namespaces.
|
556 |
|
|
If a \fBnamespace import\fR command specifies a command
|
557 |
|
|
that is not exported, the command is not imported.
|
558 |
|
|
|
559 |
|
|
.SH "SEE ALSO"
|
560 |
|
|
variable(n)
|
561 |
|
|
|
562 |
|
|
.SH KEYWORDS
|
563 |
|
|
exported, internal, variable
|