Training, Open Source computer languages
PerlPHPPythonMySQLApache / TomcatTclRubyJavaC and C++LinuxCSS 
Search for:
Home Accessibility Courses Diary The Mouth Forum Resources Site Map About Us Contact
Tracing a variable in Tcl

Posted by admin (Graham Ellis), 30 July 2002
Do you want to trace a variable in a Tcl program?  Be told when it's changed, and perhaps when it's read too?   The trace command lets you do this, but you do need top provide a proc to be performed whenever the variable is written / read.

Here's an example:


#!/usr/bin/tcl

puts -nonewline "Give me a big number: "
flush stdout

gets stdin value

while {$value > 100} {
       set value [expr $value / 2 - 2]
       }

puts "final value is $value"


In operation:


$ ./vt
Give me a big number: 567
final value is 67
$


OK - is that right?    Let's add some traces and watch the variable


#!/usr/bin/tcl

# tracing a variable - example

proc watch {varname key op} {
       if {$key != ""} {
               set varname ${varname}($key)
               }
       upvar $varname var
       puts "$varname is $var (operation $op)"
       }

# ---------------------------------------
trace variable value w watch
trace variable value r watch
# ---------------------------------------

puts -nonewline "Give me a big number: "
flush stdout

gets stdin value

while {$value > 100} {
       set value [expr $value / 2 - 2]
       }

puts "final value is $value"


And run:


$ ./vt
Give me a big number: 567
value is 567 (operation w)
value is 567 (operation r)
value is 567 (operation r)
value is 281 (operation w)
value is 281 (operation r)
value is 281 (operation r)
value is 138 (operation w)
value is 138 (operation r)
value is 138 (operation r)
value is 67 (operation w)
value is 67 (operation r)
value is 67 (operation r)
final value is 67
$


Good - we can see how it has worked now.

trace variable takes three parameters - the name of the variable to trace, the operation (r for read, w for write), and the name of the command to run whenever the read or write operation happens.  You can turn a trace back off using the trace vdelete command.

The proc itself takes three parameters - the variable name, the name of the element (if it's an array element that you're tracing), and a variable telling you if it's a read or a write (you can also use u for unset).  In our example, we've added code to allow you trace an array element if there's an element name, and we've also used upvar to get at the value of the variable - probably a requirement that you'll always have in a trace procedure.




This page is a thread posted to the opentalk forum at www.opentalk.org.uk and archived here for reference. To jump to the archive index please follow this link.

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