uplevel.n
上传用户:rrhhcc
上传日期:2015-12-11
资源大小:54129k
文件大小:4k
- '"
- '" Copyright (c) 1993 The Regents of the University of California.
- '" Copyright (c) 1994-1997 Sun Microsystems, Inc.
- '"
- '" See the file "license.terms" for information on usage and redistribution
- '" of this file, and for a DISCLAIMER OF ALL WARRANTIES.
- '"
- '" RCS: @(#) $Id: uplevel.n,v 1.3.18.1 2004/10/27 14:43:15 dkf Exp $
- '"
- .so man.macros
- .TH uplevel n "" Tcl "Tcl Built-In Commands"
- .BS
- '" Note: do not modify the .SH NAME line immediately below!
- .SH NAME
- uplevel - Execute a script in a different stack frame
- .SH SYNOPSIS
- fBuplevel fR?fIlevelfR?fI arg fR?fIarg ...fR?
- .BE
- .SH DESCRIPTION
- .PP
- All of the fIargfR arguments are concatenated as if they had
- been passed to fBconcatfR; the result is then evaluated in the
- variable context indicated by fIlevelfR. fBUplevelfR returns
- the result of that evaluation.
- .PP
- If fIlevelfR is an integer then
- it gives a distance (up the procedure calling stack) to move before
- executing the command. If fIlevelfR consists of fB#fR followed by
- a number then the number gives an absolute level number. If fIlevelfR
- is omitted then it defaults to fB1fR. fILevelfR cannot be
- defaulted if the first fIcommandfR argument starts with a digit or fB#fR.
- .PP
- For example, suppose that procedure fBafR was invoked
- from top-level, and that it called fBbfR, and that fBbfR called fBcfR.
- Suppose that fBcfR invokes the fBuplevelfR command. If fIlevelfR
- is fB1fR or fB#2fR or omitted, then the command will be executed
- in the variable context of fBbfR. If fIlevelfR is fB2fR or fB#1fR
- then the command will be executed in the variable context of fBafR.
- If fIlevelfR is fB3fR or fB#0fR then the command will be executed
- at top-level (only global variables will be visible).
- .PP
- The fBuplevelfR command causes the invoking procedure to disappear
- from the procedure calling stack while the command is being executed.
- In the above example, suppose fBcfR invokes the command
- .CS
- fBuplevelfR 1 {set x 43; d}
- .CE
- where fBdfR is another Tcl procedure. The fBsetfR command will
- modify the variable fBxfR in fBbfR's context, and fBdfR will execute
- at level 3, as if called from fBbfR. If it in turn executes
- the command
- .CS
- fBuplevelfR {set x 42}
- .CE
- then the fBsetfR command will modify the same variable fBxfR in fBbfR's
- context: the procedure fBcfR does not appear to be on the call stack
- when fBdfR is executing. The command ``fBinfo levelfR'' may
- be used to obtain the level of the current procedure.
- .PP
- fBUplevelfR makes it possible to implement new control
- constructs as Tcl procedures (for example, fBuplevelfR could
- be used to implement the fBwhilefR construct as a Tcl procedure).
- .PP
- fBnamespace evalfR is another way (besides procedure calls)
- that the Tcl naming context can change.
- It adds a call frame to the stack to represent the namespace context.
- This means each fBnamespace evalfR command
- counts as another call level for fBuplevelfR and fBupvarfR commands.
- For example, fBinfo level 1fR will return a list
- describing a command that is either
- the outermost procedure call or the outermost fBnamespace evalfR command.
- Also, fBuplevel #0fR evaluates a script
- at top-level in the outermost namespace (the global namespace).
- .SH EXAMPLE
- As stated above, the fBuplevelfR command is useful for creating new
- control constructs. This example shows how (without error handling)
- it can be used to create a fBdofR command that is the counterpart of
- fBwhilefR except for always performing the test after running the
- loop body:
- .CS
- proc do {body while condition} {
- if {$while ne "while"} {
- error "required word missing"
- }
- set conditionCmd [list expr $condition]
- while {1} {
- fBuplevelfR 1 $body
- if {![fBuplevelfR 1 $conditionCmd]} {
- break
- }
- }
- }
- .CE
- .SH "SEE ALSO"
- namespace(n), upvar(n)
- .SH KEYWORDS
- context, level, namespace, stack frame, variables