Initial version of donated sources by Avertec, 3.4p5.
[tas-yagle.git] / distrib / share / tcl / help / tcl / variables / variable
1 NAME
2 variable - create and initialize a namespace variable
3
4 SYNOPSIS
5 variable ?name value...? name ?value?
6
7
8 DESCRIPTION
9 This command is normally used within a namespace eval command to create
10 one or more variables within a namespace. Each variable name is ini-
11 tialized with value. The value for the last variable is optional.
12
13 If a variable name does not exist, it is created. In this case, if
14 value is specified, it is assigned to the newly created variable. If
15 no value is specified, the new variable is left undefined. If the
16 variable already exists, it is set to value if value is specified or
17 left unchanged if no value is given. Normally, name is unqualified
18 (does not include the names of any containing namespaces), and the
19 variable is created in the current namespace. If name includes any
20 namespace qualifiers, the variable is created in the specified names-
21 pace. If the variable is not defined, it will be visible to the names-
22 pace which command, but not to the info exists command.
23
24 If the variable command is executed inside a Tcl procedure, it creates
25 local variables linked to the corresponding namespace variables (and
26 therefore these variables are listed by info vars.) In this way the
27 variable command resembles the global command, although the global com-
28 mand only links to variables in the global namespace. If any values
29 are given, they are used to modify the values of the associated names-
30 pace variables. If a namespace variable does not exist, it is created
31 and optionally initialized.
32
33 A name argument cannot reference an element within an array. Instead,
34 name should reference the entire array, and the initialization value
35 should be left off. After the variable has been declared, elements
36 within the array can be set using ordinary set or array commands.
37
38 EXAMPLES
39 Create a variable in a namespace:
40 namespace eval foo {
41 variable bar 12345
42 }
43
44 Create an array in a namespace:
45 namespace eval someNS {
46 variable someAry
47 array set someAry {
48 someName someValue
49 otherName otherValue
50 }
51 }
52
53 Access variables in namespaces from a procedure:
54 namespace eval foo {
55 proc spong {} {
56 # Variable in this namespace
57 variable bar
58 puts "bar is $bar"
59
60 # Variable in another namespace
61 variable ::someNS::someAry
62 parray someAry
63 }
64 }
65
66
67 SEE ALSO
68 global(n), namespace(n), upvar(n)
69
70
71 KEYWORDS