1variable(n)                  Tcl Built-In Commands                 variable(n)
2
3
4
5______________________________________________________________________________
6

NAME

8       variable - create and initialize a namespace variable
9

SYNOPSIS

11       variable name
12
13       variable ?name value...?
14______________________________________________________________________________
15

DESCRIPTION

17       This command is normally used within a namespace eval command to create
18       one or more variables within a namespace.  Each variable name  is  ini‐
19       tialized with value.  The value for the last variable is optional.
20
21       If  a  variable  name  does not exist, it is created.  In this case, if
22       value is specified, it is assigned to the newly created  variable.   If
23       no  value  is  specified,  the  new variable is left undefined.  If the
24       variable already exists, it is set to value if value  is  specified  or
25       left  unchanged  if  no  value is given.  Normally, name is unqualified
26       (does not include the names of  any  containing  namespaces),  and  the
27       variable  is  created  in  the current namespace.  If name includes any
28       namespace qualifiers, the variable is created in the  specified  names‐
29       pace.  If the variable is not defined, it will be visible to the names‐
30       pace which command, but not to the info exists command.
31
32       If the variable command is executed inside a Tcl procedure, it  creates
33       local  variables  linked  to the corresponding namespace variables (and
34       therefore these variables are listed by info vars.)  In  this  way  the
35       variable command resembles the global command, although the global com‐
36       mand resolves variable names  with  respect  to  the  global  namespace
37       instead  of  the current namespace of the procedure.  If any values are
38       given, they are used to modify the values of the  associated  namespace
39       variables.   If  a namespace variable does not exist, it is created and
40       optionally initialized.
41
42       A name argument cannot reference an element within an array.   Instead,
43       name  should  reference  the entire array, and the initialization value
44       should be left off.  After the variable  has  been  declared,  elements
45       within the array can be set using ordinary set or array commands.
46

EXAMPLES

48       Create a variable in a namespace:
49
50              namespace eval foo {
51                  variable bar 12345
52              }
53
54       Create an array in a namespace:
55
56              namespace eval someNS {
57                  variable someAry
58                  array set someAry {
59                      someName  someValue
60                      otherName otherValue
61                  }
62              }
63
64       Access variables in namespaces from a procedure:
65
66              namespace eval foo {
67                  proc spong {} {
68                      # Variable in this namespace
69                      variable bar
70                      puts "bar is $bar"
71
72                      # Variable in another namespace
73                      variable ::someNS::someAry
74                      parray someAry
75                  }
76              }
77

SEE ALSO

79       global(n), namespace(n), upvar(n)
80

KEYWORDS

82       global, namespace, procedure, variable
83
84
85
86Tcl                                   8.0                          variable(n)
Impressum