diff options
author | dkf <dkf@noemail.net> | 2004-10-27 14:43:09 (GMT) |
---|---|---|
committer | dkf <dkf@noemail.net> | 2004-10-27 14:43:09 (GMT) |
commit | d20465b6debfc29faac6251cf27bd97f8ada9ee5 (patch) | |
tree | 7a733c8f49dc66ae55cca35e7652f5bb0b1c51c6 /doc/upvar.n | |
parent | 283036b2fce11f6302bae2d3b3b2f00217546af9 (diff) | |
download | tcl-d20465b6debfc29faac6251cf27bd97f8ada9ee5.zip tcl-d20465b6debfc29faac6251cf27bd97f8ada9ee5.tar.gz tcl-d20465b6debfc29faac6251cf27bd97f8ada9ee5.tar.bz2 |
Finished user-level documentation backport
FossilOrigin-Name: 92cb8415a84f82b13add50091cd0406e10d24d30
Diffstat (limited to 'doc/upvar.n')
-rw-r--r-- | doc/upvar.n | 26 |
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) |