Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: downloads/tcl8.5.2/doc/variable.n @ 25

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

added tcl to libs

File size: 3.1 KB
Line 
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
15variable \- 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
22This command is normally used within a
23\fBnamespace eval\fR command to create one or more variables
24within a namespace.
25Each variable \fIname\fR is initialized with \fIvalue\fR.
26The \fIvalue\fR for the last variable is optional.
27.PP
28If a variable \fIname\fR does not exist, it is created.
29In this case, if \fIvalue\fR is specified,
30it is assigned to the newly created variable.
31If no \fIvalue\fR is specified, the new variable is left undefined.
32If the variable already exists,
33it is set to \fIvalue\fR if \fIvalue\fR is specified
34or left unchanged if no \fIvalue\fR is given.
35Normally, \fIname\fR is unqualified
36(does not include the names of any containing namespaces),
37and the variable is created in the current namespace.
38If \fIname\fR includes any namespace qualifiers,
39the variable is created in the specified namespace.  If the variable
40is not defined, it will be visible to the \fBnamespace which\fR
41command, but not to the \fBinfo exists\fR command.
42.PP
43If the \fBvariable\fR command is executed inside a Tcl procedure,
44it creates local variables
45linked to the corresponding namespace variables (and therefore these
46variables are listed by \fBinfo vars\fR.)
47In this way the \fBvariable\fR command resembles the \fBglobal\fR command,
48although the \fBglobal\fR command
49only links to variables in the global namespace.
50If any \fIvalue\fRs are given,
51they are used to modify the values of the associated namespace variables.
52If a namespace variable does not exist,
53it is created and optionally initialized.
54.PP
55A \fIname\fR argument cannot reference an element within an array.
56Instead, \fIname\fR should reference the entire array,
57and the initialization \fIvalue\fR should be left off.
58After the variable has been declared,
59elements within the array can be set using ordinary
60\fBset\fR or \fBarray\fR commands.
61.SH EXAMPLES
62Create a variable in a namespace:
63.CS
64namespace eval foo {
65    \fBvariable\fR bar 12345
66}
67.CE
68.PP
69Create an array in a namespace:
70.CS
71namespace eval someNS {
72    \fBvariable\fR someAry
73    array set someAry {
74        someName  someValue
75        otherName otherValue
76    }
77}
78.CE
79.PP
80Access variables in namespaces from a procedure:
81.CS
82namespace 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"
96global(n), namespace(n), upvar(n)
97
98.SH KEYWORDS
99global, namespace, procedure, variable
Note: See TracBrowser for help on using the repository browser.