home *** CD-ROM | disk | FTP | other *** search
/ Sprite 1984 - 1993 / Sprite 1984 - 1993.iso / lib / tclX-6.4 / help / variables / upvar < prev   
Encoding:
Text File  |  1992-12-17  |  1.9 KB  |  35 lines

  1.           upvar ?level? otherVar myVar ?otherVar myVar ...?
  2.                This command arranges for one or more  local  variables
  3.                in  the  current  procedure to refer to variables in an
  4.                enclosing procedure call or to global variables.  Level
  5.                may  have  any  of  the forms permitted for the uplevel
  6.                command, and may be omitted if the first letter of  the
  7.                first  otherVar  isn't # or a digit (it defaults to 1).
  8.                For each otherVar argument, upvar makes the variable by
  9.                that  name in the procedure frame given by level (or at
  10.                global level, if level is #0) accessible in the current
  11.                procedure  by the name given in the corresponding myVar
  12.                argument.  The variable  named  by  otherVar  need  not
  13.                exist  at the time of the call;  it will be created the
  14.                first time myVar is referenced, just like  an  ordinary
  15.                variable.   Upvar  may  only  be  invoked  from  within
  16.                procedures.  Neither otherVar or myVar may refer to  an
  17.                element of an array.  Upvar returns an empty string.
  18.  
  19.                The upvar  command  simplifies  the  implementation  of
  20.                call-by-name procedure calling and also makes it easier
  21.                to build new control constructs as Tcl procedures.  For
  22.                example, consider the following procedure:
  23.  
  24.                     proc add2 name {
  25.                         upvar $name x
  26.                         set x [expr $x+2]
  27.                     }
  28.  
  29.                Add2 is invoked with an argument giving the name  of  a
  30.                variable,  and  it  adds  two  to  the  value  of  that
  31.                variable.  Although add2 could  have  been  implemented
  32.                using  uplevel instead of upvar, upvar makes it simpler
  33.                for  add2  to  access  the  variable  in  the  caller's
  34.                procedure frame.
  35.