| 1 |
'\" |
| 2 |
'\" Copyright (c) 1993 The Regents of the University of California. |
| 3 |
'\" Copyright (c) 1994-1997 Sun Microsystems, Inc. |
| 4 |
'\" |
| 5 |
'\" See the file "license.terms" for information on usage and redistribution |
| 6 |
'\" of this file, and for a DISCLAIMER OF ALL WARRANTIES. |
| 7 |
'\" |
| 8 |
.TH upvar n "" Tcl "Tcl Built-In Commands" |
| 9 |
.so man.macros |
| 10 |
.BS |
| 11 |
'\" Note: do not modify the .SH NAME line immediately below! |
| 12 |
.SH NAME |
| 13 |
upvar \- Create link to variable in a different stack frame |
| 14 |
.SH SYNOPSIS |
| 15 |
\fBupvar \fR?\fIlevel\fR? \fIotherVar myVar \fR?\fIotherVar myVar \fR...? |
| 16 |
.BE |
| 17 |
|
| 18 |
.SH DESCRIPTION |
| 19 |
.PP |
| 20 |
This command arranges for one or more local variables in the current |
| 21 |
procedure to refer to variables in an enclosing procedure call or |
| 22 |
to global variables. |
| 23 |
\fILevel\fR may have any of the forms permitted for the \fBuplevel\fR |
| 24 |
command, and may be omitted (it defaults to \fB1\fR). |
| 25 |
For each \fIotherVar\fR argument, \fBupvar\fR makes the variable |
| 26 |
by that name in the procedure frame given by \fIlevel\fR (or at |
| 27 |
global level, if \fIlevel\fR is \fB#0\fR) accessible |
| 28 |
in the current procedure by the name given in the corresponding |
| 29 |
\fImyVar\fR argument. |
| 30 |
The variable named by \fIotherVar\fR need not exist at the time of the |
| 31 |
call; it will be created the first time \fImyVar\fR is referenced, just like |
| 32 |
an ordinary variable. There must not exist a variable by the |
| 33 |
name \fImyVar\fR at the time \fBupvar\fR is invoked. |
| 34 |
\fIMyVar\fR is always treated as the name of a variable, not an |
| 35 |
array element. An error is returned if the name looks like an array element, |
| 36 |
such as \fBa(b)\fR. |
| 37 |
\fIOtherVar\fR may refer to a scalar variable, an array, |
| 38 |
or an array element. |
| 39 |
\fBUpvar\fR returns an empty string. |
| 40 |
.PP |
| 41 |
The \fBupvar\fR command simplifies the implementation of call-by-name |
| 42 |
procedure calling and also makes it easier to build new control constructs |
| 43 |
as Tcl procedures. |
| 44 |
For example, consider the following procedure: |
| 45 |
.PP |
| 46 |
.CS |
| 47 |
proc \fIadd2\fR name { |
| 48 |
\fBupvar\fR $name x |
| 49 |
set x [expr {$x + 2}] |
| 50 |
} |
| 51 |
.CE |
| 52 |
.PP |
| 53 |
If \fIadd2\fR is invoked with an argument giving the name of a variable, |
| 54 |
it adds two to the value of that variable. |
| 55 |
Although \fIadd2\fR could have been implemented using \fBuplevel\fR |
| 56 |
instead of \fBupvar\fR, \fBupvar\fR makes it simpler for \fIadd2\fR |
| 57 |
to access the variable in the caller's procedure frame. |
| 58 |
.PP |
| 59 |
\fBnamespace eval\fR is another way (besides procedure calls) |
| 60 |
that the Tcl naming context can change. |
| 61 |
It adds a call frame to the stack to represent the namespace context. |
| 62 |
This means each \fBnamespace eval\fR command |
| 63 |
counts as another call level for \fBuplevel\fR and \fBupvar\fR commands. |
| 64 |
For example, \fBinfo level\fR \fB1\fR will return a list |
| 65 |
describing a command that is either |
| 66 |
the outermost procedure call or the outermost \fBnamespace eval\fR command. |
| 67 |
Also, \fBuplevel #0\fR evaluates a script |
| 68 |
at top-level in the outermost namespace (the global namespace). |
| 69 |
.PP |
| 70 |
If an upvar variable is unset (e.g. \fBx\fR in \fBadd2\fR above), the |
| 71 |
\fBunset\fR operation affects the variable it is linked to, not the |
| 72 |
upvar variable. There is no way to unset an upvar variable except |
| 73 |
by exiting the procedure in which it is defined. However, it is |
| 74 |
possible to retarget an upvar variable by executing another \fBupvar\fR |
| 75 |
command. |
| 76 |
.SH "TRACES AND UPVAR" |
| 77 |
.PP |
| 78 |
Upvar interacts with traces in a straightforward but possibly |
| 79 |
unexpected manner. If a variable trace is defined on \fIotherVar\fR, that |
| 80 |
trace will be triggered by actions involving \fImyVar\fR. However, |
| 81 |
the trace procedure will be passed the name of \fImyVar\fR, rather |
| 82 |
than the name of \fIotherVar\fR. Thus, the output of the following code |
| 83 |
will be |
| 84 |
.QW "\fIlocalVar\fR" |
| 85 |
rather than |
| 86 |
.QW "\fIoriginalVar\fR" : |
| 87 |
.PP |
| 88 |
.CS |
| 89 |
proc \fItraceproc\fR { name index op } { |
| 90 |
puts $name |
| 91 |
} |
| 92 |
proc \fIsetByUpvar\fR { name value } { |
| 93 |
\fBupvar\fR $name localVar |
| 94 |
set localVar $value |
| 95 |
} |
| 96 |
set originalVar 1 |
| 97 |
trace variable originalVar w \fItraceproc\fR |
| 98 |
\fIsetByUpvar\fR originalVar 2 |
| 99 |
.CE |
| 100 |
.PP |
| 101 |
If \fIotherVar\fR refers to an element of an array, then variable |
| 102 |
traces set for the entire array will not be invoked when \fImyVar\fR |
| 103 |
is accessed (but traces on the particular element will still be |
| 104 |
invoked). In particular, if the array is \fBenv\fR, then changes |
| 105 |
made to \fImyVar\fR will not be passed to subprocesses correctly. |
| 106 |
.SH EXAMPLE |
| 107 |
A \fBdecr\fR command that works like \fBincr\fR except it subtracts |
| 108 |
the value from the variable instead of adding it: |
| 109 |
.PP |
| 110 |
.CS |
| 111 |
proc decr {varName {decrement 1}} { |
| 112 |
\fBupvar\fR 1 $varName var |
| 113 |
incr var [expr {-$decrement}] |
| 114 |
} |
| 115 |
.CE |
| 116 |
.SH "SEE ALSO" |
| 117 |
global(n), namespace(n), uplevel(n), variable(n) |
| 118 |
.SH KEYWORDS |
| 119 |
context, frame, global, level, namespace, procedure, upvar, variable |
| 120 |
.\" Local Variables: |
| 121 |
.\" mode: nroff |
| 122 |
.\" End: |