Home Accessibility Courses Diary The Mouth Facebook Resources Site Map About Us Contact
Tcl - global, upvar and uplevel.

In Tcl, variables are "scoped" to the procedure in which they're used. That's a sensible default, since you want to be able to write main applications without the risk of interfering with variables within your procs.

on a tcl/tk courseA global command in a proc allows you to refer to a variable in the main code from within a proc. (No need to declare it global at the top level too)

An upvar command allows you to reference a variable in your calling code from within your proc, and is commonly used for "pass by name" requirements where you're writing a proc that must alter an incoming variable.

The uplevel command allows you to run a piece of code as if it was in the namespace of your calling code and not in your proc - and you can even specify a number of levels of code up or an absolute level number.

Suggestion. Use upvar whenever you need to call by name. Use global sparingly. And think very carefully before you ever use uplevel!

Example:

proc demo {what where} {
global counter
upvar $where hereitis
uplevel incr dc
append hereitis ", UK"
incr counter
puts "Demo run"
return "$what $hereitis"
}
#
set dc 0
set counter 0
set place Melksham
set rz [demo "eat in" place]
puts "function returns: $rz"
puts "place will have UK on the end: $place"
puts "counter is number of calls to demo: $counter"
puts "variable dc has become: $dc"


Runs as

Dorothy:~/oct07 grahamellis$ tclsh uld
Demo run
function returns: eat in Melksham, UK
place will have UK on the end: Melksham, UK
counter is number of calls to demo: 1
variable dc has become: 1
Dorothy:~/oct07 grahamellis$


(written 2007-10-24, updated 2011-03-01)

 
Associated topics are indexed as below, or enter http://melksh.am/nnnn for individual articles
T207 - Tcl/Tk - Procedures and Variable Scope
  [3629] Sharing lots of values in Tcl without having lots of global declarations - (2012-02-28)
  [3414] Passing back multiple results in Tcl - upvar and uplevel - (2011-09-01)
  [3192] Tcl - Some example of HOW TO in handling data files and formats - (2011-03-04)
  [2929] Passing a variable number of parameters in to a function / method - (2010-08-20)
  [2520] Global and Enable - two misused words! - (2009-11-30)
  [2476] Tcl - uplevel to run code at calling level - (2009-10-24)
  [2466] Tcl - passing arrays and strings in and back out of procs - (2009-10-22)
  [1782] Calling procs in Tcl and how it compares to Perl - (2008-09-02)
  [1163] A better alternative to cutting and pasting code - (2007-04-26)
  [775] Do not duplicate your code - (2006-06-23)
  [409] Functions and commands with dangerous names - (2005-08-11)
  [308] Call by name v call by value - (2005-05-11)
  [122] Passing arrays to procs in Tcl - (2004-11-18)
  [96] Variable Scope - (2004-10-22)


Back to
Square Bracket protection in Tcl
Previous and next
or
Horse's mouth home
Forward to
Sorting in Tcl - lists and arrays
Some other Articles
Wireless hotel tips - FTP and Skype connections failing
Reading from another process in Tcl (pipes and sockets)
Away or home - which do I prefer?
Sorting in Tcl - lists and arrays
Tcl - global, upvar and uplevel.
Square Bracket protection in Tcl
Tcl - append v lappend v concat
Tcl - using [] or {} for conditions in an if (and while)
Dark Dawn
Pictures Framed
4254 posts, page by page
Link to page ... 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86 at 50 posts per page


This is a page archived from The Horse's Mouth at http://www.wellho.net/horse/ - the diary and writings of Graham Ellis. Every attempt was made to provide current information at the time the page was written, but things do move forward in our business - new software releases, price changes, new techniques. Please check back via our main site for current courses, prices, versions, etc - any mention of a price in "The Horse's Mouth" cannot be taken as an offer to supply at that price.

Link to Ezine home page (for reading).
Link to Blogging home page (to add comments).

You can Add a comment or ranking to this page

© WELL HOUSE CONSULTANTS LTD., 2014: Well House Manor • 48 Spa Road • Melksham, Wiltshire • United Kingdom • SN12 7NY
PH: 01144 1225 708225 • FAX: 01144 1225 899360 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho

PAGE: http://www.wellho.net/mouth/1404_Tcl ... evel-.html • PAGE BUILT: Sun Mar 30 15:20:58 2014 • BUILD SYSTEM: WomanWithCat