summaryrefslogtreecommitdiffstats
path: root/doc/upvar.n
diff options
context:
space:
mode:
Diffstat (limited to 'doc/upvar.n')
-rw-r--r--doc/upvar.n28
1 files changed, 16 insertions, 12 deletions
diff --git a/doc/upvar.n b/doc/upvar.n
index a255485..60e5324 100644
--- a/doc/upvar.n
+++ b/doc/upvar.n
@@ -21,8 +21,7 @@ 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 if the first letter of the first \fIotherVar\fR
-is not \fB#\fR or a digit (it defaults to \fB1\fR).
+command, and may be omitted (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
@@ -43,16 +42,18 @@ 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 \fBadd2\fR
+instead of \fBupvar\fR, \fBupvar\fR makes it simpler for \fIadd2\fR
to access the variable in the caller's procedure frame.
.PP
\fBnamespace eval\fR is another way (besides procedure calls)
@@ -60,7 +61,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 1\fR will return a list
+For example, \fBinfo level\fR \fB1\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
@@ -83,13 +84,14 @@ 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
@@ -104,15 +106,17 @@ 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, variable
+context, frame, global, level, namespace, procedure, upvar, variable
+.\" Local Variables:
+.\" mode: nroff
+.\" End: