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

NAME

8       upvar - Create link to variable in a different stack frame
9

SYNOPSIS

11       upvar ?level? otherVar myVar ?otherVar myVar ...?
12______________________________________________________________________________
13
14

DESCRIPTION

16       This  command  arranges  for one or more local variables in the current
17       procedure to refer to variables in an enclosing procedure  call  or  to
18       global  variables.   Level  may have any of the forms permitted for the
19       uplevel command, and may be omitted (it defaults to 1).  For each  oth‐
20       erVar  argument, upvar makes the variable by that name in the procedure
21       frame given by level (or at global level, if level is #0) accessible in
22       the  current  procedure  by  the  name given in the corresponding myVar
23       argument.  The variable named by otherVar need not exist at the time of
24       the  call;  it will be created the first time myVar is referenced, just
25       like an ordinary variable.  There must not exist a variable by the name
26       myVar  at  the  time  upvar is invoked.  MyVar is always treated as the
27       name of a variable, not an array element.  An error is returned if  the
28       name  looks like an array element, such as a(b).  OtherVar may refer to
29       a scalar variable, an array, or an array  element.   Upvar  returns  an
30       empty string.
31
32       The  upvar command simplifies the implementation of call-by-name proce‐
33       dure calling and also makes it easier to build new  control  constructs
34       as Tcl procedures.  For example, consider the following procedure:
35
36              proc add2 name {
37                  upvar $name x
38                  set x [expr {$x + 2}]
39              }
40
41       If  add2  is invoked with an argument giving the name of a variable, it
42       adds two to the value of that variable.  Although add2 could have  been
43       implemented  using uplevel instead of upvar, upvar makes it simpler for
44       add2 to access the variable in the caller's procedure frame.
45
46       namespace eval is another way (besides procedure calls)  that  the  Tcl
47       naming context can change.  It adds a call frame to the stack to repre‐
48       sent the namespace context.  This means  each  namespace  eval  command
49       counts as another call level for uplevel and upvar commands.  For exam‐
50       ple, info level 1 will return a  list  describing  a  command  that  is
51       either  the  outermost  procedure  call or the outermost namespace eval
52       command.  Also, uplevel #0 evaluates a script at top-level in the  out‐
53       ermost namespace (the global namespace).
54
55       If  an upvar variable is unset (e.g. x in add2 above), the unset opera‐
56       tion affects the variable it is linked  to,  not  the  upvar  variable.
57       There is no way to unset an upvar variable except by exiting the proce‐
58       dure in which it is defined.  However, it is possible  to  retarget  an
59       upvar variable by executing another upvar command.
60

TRACES AND UPVAR

62       Upvar  interacts  with  traces  in a straightforward but possibly unex‐
63       pected manner.  If a variable trace is defined on otherVar, that  trace
64       will  be triggered by actions involving myVar.  However, the trace pro‐
65       cedure will be passed the name of myVar, rather than the name of other‐
66       Var.   Thus, the output of the following code will be “localVar” rather
67       than “originalVar”:
68
69              proc traceproc { name index op } {
70                  puts $name
71              }
72              proc setByUpvar { name value } {
73                  upvar $name localVar
74                  set localVar $value
75              }
76              set originalVar 1
77              trace variable originalVar w traceproc
78              setByUpvar originalVar 2
79
80       If otherVar refers to an element of an array, then variable traces  set
81       for  the  entire  array will not be invoked when myVar is accessed (but
82       traces on the particular element will still be invoked).   In  particu‐
83       lar, if the array is env, then changes made to myVar will not be passed
84       to subprocesses correctly.
85

EXAMPLE

87       A decr command that works like incr except it subtracts the value  from
88       the variable instead of adding it:
89
90              proc decr {varName {decrement 1}} {
91                  upvar 1 $varName var
92                  incr var [expr {-$decrement}]
93              }
94

SEE ALSO

96       global(n), namespace(n), uplevel(n), variable(n)
97

KEYWORDS

99       context, frame, global, level, namespace, procedure, upvar, variable
100
101
102
103Tcl                                                                   upvar(n)
Impressum