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