summaryrefslogtreecommitdiffstats
path: root/doc/upvar.n
diff options
context:
space:
mode:
authordkf <donal.k.fellows@manchester.ac.uk>2004-10-27 14:43:09 (GMT)
committerdkf <donal.k.fellows@manchester.ac.uk>2004-10-27 14:43:09 (GMT)
commit4955bdc67b8a5c3d9d12cf70cfe0b73c8a4f4ca8 (patch)
tree7a733c8f49dc66ae55cca35e7652f5bb0b1c51c6 /doc/upvar.n
parent45beb64f7dcb09a6ce83532702bca760f72e6f4d (diff)
downloadtcl-4955bdc67b8a5c3d9d12cf70cfe0b73c8a4f4ca8.zip
tcl-4955bdc67b8a5c3d9d12cf70cfe0b73c8a4f4ca8.tar.gz
tcl-4955bdc67b8a5c3d9d12cf70cfe0b73c8a4f4ca8.tar.bz2
Finished user-level documentation backport
Diffstat (limited to 'doc/upvar.n')
-rw-r--r--doc/upvar.n26
1 files changed, 17 insertions, 9 deletions
diff --git a/doc/upvar.n b/doc/upvar.n
index 2b2175e..b73d6a4 100644
--- a/doc/upvar.n
+++ b/doc/upvar.n
@@ -5,7 +5,7 @@
'\" See the file "license.terms" for information on usage and redistribution
'\" of this file, and for a DISCLAIMER OF ALL WARRANTIES.
'\"
-'\" RCS: @(#) $Id: upvar.n,v 1.5 2000/11/21 15:56:21 dkf Exp $
+'\" RCS: @(#) $Id: upvar.n,v 1.5.18.1 2004/10/27 14:43:15 dkf Exp $
'\"
.so man.macros
.TH upvar n "" Tcl "Tcl Built-In Commands"
@@ -47,8 +47,8 @@ as Tcl procedures.
For example, consider the following procedure:
.CS
\fBproc add2 name {
- upvar $name x
- set x [expr $x+2]
+ \fBupvar\fR $name x
+ set x [expr $x+2]
}\fR
.CE
\fBAdd2\fR is invoked with an argument giving the name of a variable,
@@ -75,8 +75,7 @@ upvar variable. There is no way to unset an upvar variable except
by exiting the procedure in which it is defined. However, it is
possible to retarget an upvar variable by executing another \fBupvar\fR
command.
-
-.SH Traces and upvar
+.SH "TRACES AND UPVAR"
.PP
Upvar interacts with traces in a straightforward but possibly
unexpected manner. If a variable trace is defined on \fIotherVar\fR, that
@@ -86,24 +85,33 @@ than the name of \fIotherVar\fR. Thus, the output of the following code
will be \fBlocalVar\fR rather than \fBoriginalVar\fR:
.CS
\fBproc traceproc { name index op } {
- puts $name
+ puts $name
}
proc setByUpvar { name value } {
- upvar $name localVar
- set localVar $value
+ \fBupvar\fR $name localVar
+ set localVar $value
}
set originalVar 1
trace variable originalVar w traceproc
setByUpvar originalVar 2
}\fR
.CE
-
+.PP
If \fIotherVar\fR refers to an element of an array, then variable
traces set for the entire array will not be invoked when \fImyVar\fR
is accessed (but traces on the particular element will still be
invoked). In particular, if the array is \fBenv\fR, then changes
made to \fImyVar\fR will not be passed to subprocesses correctly.
.VE
+.SH EXAMPLE
+A \fBdecr\fR command that works like \fBincr\fR except it subtracts
+the value from the variable instead of adding it:
+.CS
+proc decr {varName {decrement 1}} {
+ \fBupvar\fR 1 $varName var
+ incr var [expr {-$decrement}]
+}
+.CE
.SH "SEE ALSO"
global(n), namespace(n), uplevel(n), variable(n)