package require Itcl
itcl::class testtrace {
variable member "";#[eval $$name1]
constructor {} {
# Here is the trace set to a local variable. Note use of itcl::scope. This calls the method traceit of the class.
trace add variable [itcl::scope member] write "$this traceit"
}
# Here is the method traceit which is called whenever member is modified. Here I output
# simply the object's name, the variable modified and its new value. You might like a stack trace as well, that is an exercise for the interested reader.
method traceit {name1 name2 op} { ;# the method which "debugs" the class
set v [$this info variable $name1 -value]
puts "Class $this, variable \"$name1\" set to >>$v<<" }
method setmember {v} { set member $v}
method getmember {} { return $member }
}
set tic [testtrace tickle] ;# one instance of class testtrace
set tock [testtrace secondclass] ;# another instance of class testtrace
$tic setmember blob ;# set the member, returns a traceit
$tic setmember gurkin
$tic getmember
$tic setmember "Open trace"
$tock setmember "Hello from trace"
$tic setmember "Open the [$tic getmember] door"
The output from this is:Class ::tickle, variable "member" set to >>blob<< Class ::tickle, variable "member" set to >>gurkin<< Class ::tickle, variable "member" set to >>Open trace<< Class ::secondclass, variable "member" set to >>Hello from trace<< Class ::tickle, variable "member" set to >>Open the Open trace door<<
[tcllib] | [Category Development] | [Debugging] | [Object Orientation | Itcl ]
