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

TRACES AND UPVAR

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

EXAMPLE

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

SEE ALSO

94       global(n), namespace(n), uplevel(n), variable(n)
95
96

KEYWORDS

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