| 1 |
'\" |
| 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 |
.TH variable n 8.0 Tcl "Tcl Built-In Commands" |
| 9 |
.so man.macros |
| 10 |
.BS |
| 11 |
'\" Note: do not modify the .SH NAME line immediately below! |
| 12 |
.SH NAME |
| 13 |
variable \- create and initialize a namespace variable |
| 14 |
.SH SYNOPSIS |
| 15 |
\fBvariable \fR\fIname\fR |
| 16 |
.sp |
| 17 |
\fBvariable \fR?\fIname value...\fR? |
| 18 |
.BE |
| 19 |
.SH DESCRIPTION |
| 20 |
.PP |
| 21 |
This command is normally used within a |
| 22 |
\fBnamespace eval\fR command to create one or more variables |
| 23 |
within a namespace. |
| 24 |
Each variable \fIname\fR is initialized with \fIvalue\fR. |
| 25 |
The \fIvalue\fR for the last variable is optional. |
| 26 |
.PP |
| 27 |
If a variable \fIname\fR does not exist, it is created. |
| 28 |
In this case, if \fIvalue\fR is specified, |
| 29 |
it is assigned to the newly created variable. |
| 30 |
If no \fIvalue\fR is specified, the new variable is left undefined. |
| 31 |
If the variable already exists, |
| 32 |
it is set to \fIvalue\fR if \fIvalue\fR is specified |
| 33 |
or left unchanged if no \fIvalue\fR is given. |
| 34 |
Normally, \fIname\fR is unqualified |
| 35 |
(does not include the names of any containing namespaces), |
| 36 |
and the variable is created in the current namespace. |
| 37 |
If \fIname\fR includes any namespace qualifiers, |
| 38 |
the variable is created in the specified namespace. If the variable |
| 39 |
is not defined, it will be visible to the \fBnamespace which\fR |
| 40 |
command, but not to the \fBinfo exists\fR command. |
| 41 |
.PP |
| 42 |
If the \fBvariable\fR command is executed inside a Tcl procedure, |
| 43 |
it creates local variables |
| 44 |
linked to the corresponding namespace variables (and therefore these |
| 45 |
variables are listed by \fBinfo vars\fR.) |
| 46 |
In this way the \fBvariable\fR command resembles the \fBglobal\fR command, |
| 47 |
although the \fBglobal\fR command |
| 48 |
resolves variable names with respect to the global namespace instead |
| 49 |
of the current namespace of the procedure. |
| 50 |
If any \fIvalue\fRs are given, |
| 51 |
they are used to modify the values of the associated namespace variables. |
| 52 |
If a namespace variable does not exist, |
| 53 |
it is created and optionally initialized. |
| 54 |
.PP |
| 55 |
A \fIname\fR argument cannot reference an element within an array. |
| 56 |
Instead, \fIname\fR should reference the entire array, |
| 57 |
and the initialization \fIvalue\fR should be left off. |
| 58 |
After the variable has been declared, |
| 59 |
elements within the array can be set using ordinary |
| 60 |
\fBset\fR or \fBarray\fR commands. |
| 61 |
.SH EXAMPLES |
| 62 |
.PP |
| 63 |
Create a variable in a namespace: |
| 64 |
.PP |
| 65 |
.CS |
| 66 |
namespace eval foo { |
| 67 |
\fBvariable\fR bar 12345 |
| 68 |
} |
| 69 |
.CE |
| 70 |
.PP |
| 71 |
Create an array in a namespace: |
| 72 |
.PP |
| 73 |
.CS |
| 74 |
namespace eval someNS { |
| 75 |
\fBvariable\fR someAry |
| 76 |
array set someAry { |
| 77 |
someName someValue |
| 78 |
otherName otherValue |
| 79 |
} |
| 80 |
} |
| 81 |
.CE |
| 82 |
.PP |
| 83 |
Access variables in namespaces from a procedure: |
| 84 |
.PP |
| 85 |
.CS |
| 86 |
namespace eval foo { |
| 87 |
proc spong {} { |
| 88 |
# Variable in this namespace |
| 89 |
\fBvariable\fR bar |
| 90 |
puts "bar is $bar" |
| 91 |
|
| 92 |
# Variable in another namespace |
| 93 |
\fBvariable\fR ::someNS::someAry |
| 94 |
parray someAry |
| 95 |
} |
| 96 |
} |
| 97 |
.CE |
| 98 |
.SH "SEE ALSO" |
| 99 |
global(n), namespace(n), upvar(n) |
| 100 |
.SH KEYWORDS |
| 101 |
global, namespace, procedure, variable |
| 102 |
.\" Local variables: |
| 103 |
.\" mode: nroff |
| 104 |
.\" fill-column: 78 |
| 105 |
.\" End: |