diff options
Diffstat (limited to 'doc/upvar.n')
-rw-r--r-- | doc/upvar.n | 30 |
1 files changed, 13 insertions, 17 deletions
diff --git a/doc/upvar.n b/doc/upvar.n index 91defe6..91db24a 100644 --- a/doc/upvar.n +++ b/doc/upvar.n @@ -4,7 +4,7 @@ '\" '\" See the file "license.terms" for information on usage and redistribution '\" of this file, and for a DISCLAIMER OF ALL WARRANTIES. -'\" +'\" .TH upvar n "" Tcl "Tcl Built-In Commands" .so man.macros .BS @@ -21,7 +21,8 @@ This command arranges for one or more local variables in the current procedure to refer to variables in an enclosing procedure call or to global variables. \fILevel\fR may have any of the forms permitted for the \fBuplevel\fR -command, and may be omitted (it defaults to \fB1\fR). +command, and may be omitted if the first letter of the first \fIotherVar\fR +is not \fB#\fR or a digit (it defaults to \fB1\fR). For each \fIotherVar\fR argument, \fBupvar\fR makes the variable by that name in the procedure frame given by \fIlevel\fR (or at global level, if \fIlevel\fR is \fB#0\fR) accessible @@ -42,18 +43,16 @@ The \fBupvar\fR command simplifies the implementation of call-by-name procedure calling and also makes it easier to build new control constructs as Tcl procedures. For example, consider the following procedure: -.PP .CS proc \fIadd2\fR name { - \fBupvar\fR $name x - set x [expr {$x + 2}] + \fBupvar\fR $name x + set x [expr {$x + 2}] } .CE -.PP If \fIadd2\fR is invoked with an argument giving the name of a variable, it adds two to the value of that variable. Although \fIadd2\fR could have been implemented using \fBuplevel\fR -instead of \fBupvar\fR, \fBupvar\fR makes it simpler for \fIadd2\fR +instead of \fBupvar\fR, \fBupvar\fR makes it simpler for \fBadd2\fR to access the variable in the caller's procedure frame. .PP \fBnamespace eval\fR is another way (besides procedure calls) @@ -61,7 +60,7 @@ that the Tcl naming context can change. It adds a call frame to the stack to represent the namespace context. This means each \fBnamespace eval\fR command counts as another call level for \fBuplevel\fR and \fBupvar\fR commands. -For example, \fBinfo level\fR \fB1\fR will return a list +For example, \fBinfo level 1\fR will return a list describing a command that is either the outermost procedure call or the outermost \fBnamespace eval\fR command. Also, \fBuplevel #0\fR evaluates a script @@ -84,14 +83,13 @@ will be .QW "\fIlocalVar\fR" rather than .QW "\fIoriginalVar\fR" : -.PP .CS proc \fItraceproc\fR { name index op } { - puts $name + puts $name } proc \fIsetByUpvar\fR { name value } { - \fBupvar\fR $name localVar - set localVar $value + \fBupvar\fR $name localVar + set localVar $value } set originalVar 1 trace variable originalVar w \fItraceproc\fR @@ -106,17 +104,15 @@ made to \fImyVar\fR will not be passed to subprocesses correctly. .SH EXAMPLE A \fBdecr\fR command that works like \fBincr\fR except it subtracts the value from the variable instead of adding it: -.PP .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) + .SH KEYWORDS -context, frame, global, level, namespace, procedure, upvar, variable -.\" Local Variables: -.\" mode: nroff -.\" End: +context, frame, global, level, namespace, procedure, variable |