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
 
For 2023 (and 2024 ...) - we are now fully retired from IT training.
We have made many, many friends over 25 years of teaching about Python, Tcl, Perl, PHP, Lua, Java, C and C++ - and MySQL, Linux and Solaris/SunOS too. Our training notes are now very much out of date, but due to upward compatability most of our examples remain operational and even relevant ad you are welcome to make us if them "as seen" and at your own risk.

Lisa and I (Graham) now live in what was our training centre in Melksham - happy to meet with former delegates here - but do check ahead before coming round. We are far from inactive - rather, enjoying the times that we are retired but still healthy enough in mind and body to be active!

I am also active in many other area and still look after a lot of web sites - you can find an index ((here))
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., 2024: Well House Manor • 48 Spa Road • Melksham, Wiltshire • United Kingdom • SN12 7NY
PH: 01144 1225 708225 • FAX: 01144 1225 793803 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho