die Amika Methode Akazien Verlag   
Lesezeichen [ Info # QR-Code # Haikus ]Mi 23 Aug 2017 14:09:12 # Grundlagen der Programmierung | Tcl Tutorial.


. Variable scope - global and upvar .

[ Previous | Index | Next ]

Tcl evaluates a variable name within one of two scopes: the local scope within a proc, and a global scope (the code and variables outside of any proc). Like C, Tcl defines only one global space.

The scope in which a variable will be evaluated can be changed with the global or upvar command.

The global command will cause a variable in a local scope to be evaluated in the global scope instead.

The upvar command behaves similarly. Upvar ties the name of a variable in the current scope to a variable in a different scope. This is commonly used to simulate pass-by-reference to procs.

The syntax for upvar is: upvar ?level? otherVar1 myVar1 ?otherVar2 myVar2 ...?

Upvar causes myVar1 to become a reference to otherVar1, and myVar2 to become a reference to otherVar2, etc. The otherVar variable is declared to be at level relative to the current procedure. By default level is 1, the next level up.

If a number is used for the level, then level references that many levels up the stack from the current level.

If the level number is preceded by a # symbol, then it references that many levels down from the global scope. If level is #0, then the reference is to a variable at the global level.

My personal opinion is that using upvar with anything except #0 or 1 is asking for trouble.

The use of global is hard to avoid, but you should avoid having too many global variables. If you start needing lots of globals, you may want to look at your design again.

Note that since there is only one global space it is surprisingly easy to have name conflicts if you are importing other peoples code and aren't careful. It is recommended that you start global variables with an identifiable prefix to help avoid unexpected conflicts.


. Example .

   proc SetPositive {variable value } {
    upvar $variable myvar
    if {$value < 0} {
     set myvar [expr -$value]
    } else {
     set myvar $value
    return $myvar

   SetPositive x 5
   SetPositive y -5

   puts "X : $x  Y: $y\n"

   proc two {y} {
    upvar 1 $y z		;# Tie the calling value to variable z
    upvar 2 x a			;# Tie variable x two levels up to a
    puts "two: Z: $z A: $a"	;# Output the values, just to confirm
    set z 1		    	;# Set z, the passed variable to 1;
    set a 2			;# Set x, two layers up to 2;

   proc one {y} {
    upvar $y z		    	;# This ties the calling value to variable z 
    puts "one: Z: $z"	    	;# Output that value, to check it is 5
    two z 		    	;# Call proc two, which will change the value

   one y			;# Call one, and output X and Y after the call.
   puts "\nX: $x  Y: $y"

[ Home | Top ]
[ . Previous | Index | Next . ]
Der Inhalt dieser Seite wurde am 24.05.2016 um 12.17 Uhr aktualisiert.
Navigation Seminare Magic Software Projekte Publikationen Kontakt Home
   huecker dot com * Germany
© 1999, 2017 Franz-Josef Hücker. All Rights Reserved.
Send Page Print Page LinkedIn follow me on twitter RSS Feeds & Podcasts