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 | '\" RCS: @(#) $Id: variable.n,v 1.8 2005/05/10 18:34:04 kennykb Exp $ |
---|
9 | '\" |
---|
10 | .so man.macros |
---|
11 | .TH variable 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 | variable \- create and initialize a namespace variable |
---|
16 | .SH SYNOPSIS |
---|
17 | \fBvariable \fR?\fIname value...\fR? \fIname \fR?\fIvalue\fR? |
---|
18 | .BE |
---|
19 | |
---|
20 | .SH DESCRIPTION |
---|
21 | .PP |
---|
22 | This command is normally used within a |
---|
23 | \fBnamespace eval\fR command to create one or more variables |
---|
24 | within a namespace. |
---|
25 | Each variable \fIname\fR is initialized with \fIvalue\fR. |
---|
26 | The \fIvalue\fR for the last variable is optional. |
---|
27 | .PP |
---|
28 | If a variable \fIname\fR does not exist, it is created. |
---|
29 | In this case, if \fIvalue\fR is specified, |
---|
30 | it is assigned to the newly created variable. |
---|
31 | If no \fIvalue\fR is specified, the new variable is left undefined. |
---|
32 | If the variable already exists, |
---|
33 | it is set to \fIvalue\fR if \fIvalue\fR is specified |
---|
34 | or left unchanged if no \fIvalue\fR is given. |
---|
35 | Normally, \fIname\fR is unqualified |
---|
36 | (does not include the names of any containing namespaces), |
---|
37 | and the variable is created in the current namespace. |
---|
38 | If \fIname\fR includes any namespace qualifiers, |
---|
39 | the variable is created in the specified namespace. If the variable |
---|
40 | is not defined, it will be visible to the \fBnamespace which\fR |
---|
41 | command, but not to the \fBinfo exists\fR command. |
---|
42 | .PP |
---|
43 | If the \fBvariable\fR command is executed inside a Tcl procedure, |
---|
44 | it creates local variables |
---|
45 | linked to the corresponding namespace variables (and therefore these |
---|
46 | variables are listed by \fBinfo vars\fR.) |
---|
47 | In this way the \fBvariable\fR command resembles the \fBglobal\fR command, |
---|
48 | although the \fBglobal\fR command |
---|
49 | only links to variables in the global namespace. |
---|
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 | Create a variable in a namespace: |
---|
63 | .CS |
---|
64 | namespace eval foo { |
---|
65 | \fBvariable\fR bar 12345 |
---|
66 | } |
---|
67 | .CE |
---|
68 | .PP |
---|
69 | Create an array in a namespace: |
---|
70 | .CS |
---|
71 | namespace eval someNS { |
---|
72 | \fBvariable\fR someAry |
---|
73 | array set someAry { |
---|
74 | someName someValue |
---|
75 | otherName otherValue |
---|
76 | } |
---|
77 | } |
---|
78 | .CE |
---|
79 | .PP |
---|
80 | Access variables in namespaces from a procedure: |
---|
81 | .CS |
---|
82 | namespace eval foo { |
---|
83 | proc spong {} { |
---|
84 | # Variable in this namespace |
---|
85 | \fBvariable\fR bar |
---|
86 | puts "bar is $bar" |
---|
87 | |
---|
88 | # Variable in another namespace |
---|
89 | \fBvariable\fR ::someNS::someAry |
---|
90 | parray someAry |
---|
91 | } |
---|
92 | } |
---|
93 | .CE |
---|
94 | |
---|
95 | .SH "SEE ALSO" |
---|
96 | global(n), namespace(n), upvar(n) |
---|
97 | |
---|
98 | .SH KEYWORDS |
---|
99 | global, namespace, procedure, variable |
---|