Re: tcl-trace
- From: Eric Hassold <hassold@xxxxxxxxxx>
- Date: Tue, 28 Nov 2006 22:47:16 +0100
Florian.Murr@xxxxxxxxxxx wrote :
I am using tcl-trace since a very long time, but today I am baffled at
the following behaviour (may be I am just overworked ;-):
proc testeTclTrace {} {
puts "[info level 0] Tcl$::tcl_patchLevel"
set ::a 0
proc ::trc {args} { puts " [info level 0] a = [set ::a]" }
trace add variable ::a {write unset} "::trc"
incr ::a
trace add variable ::a {write unset} "::trc"
incr ::a
trace add variable ::a {write unset} "::trc"
unset ::a
}
I get the following result:
testeTclTrace Tcl8.4.13
::trc ::a {} write a = 1
::trc ::a {} write a = 2
::trc ::a {} write a = 2
Where is the 'unset'-trace call?
Why do I get the 'write'-trace multiple times? (I thought, when I
register the very same command twice, it still gets called just once?!)
No, if you define several traces, and they will all get executed. From the man page:
If there are multiple traces on a variable they are
invoked in order of creation, most-recent first. If one
trace returns an error, then no further traces are
invoked for the variable.
That's why the second incr generates two calls to trc with "write" argument. Registered traces need to be explicitely disabled using "trace remove".
About the unset callback: when the unset trace is invoked, the variable has already been unset. So, unset trace is actually called, but it fails because [set ::a] is then invalid. Try to define :
proc ::trc {args} {
if {[catch {set value [set ::a]}]} {
set value "<UNDEFINED>"
}
puts " [info level 0] a = $value"
}
and you will get, as expected:
::trc ::a {} write a = 1
::trc ::a {} write a = 2
::trc ::a {} write a = 2
::trc ::a {} unset a = <UNDEFINED>
::trc ::a {} unset a = <UNDEFINED>
::trc ::a {} unset a = <UNDEFINED
Note that unset trace is called 3 times, since you registered it 3 times.
Regards,
Eric
-----
Eric Hassold
Evolane - http://www.evolane.com/
.
- References:
- tcl-trace
- From: Florian . Murr
- tcl-trace
- Prev by Date: Re: ANNOUNCE: TkTreeCtrl 2.2
- Next by Date: tls/http socket issue
- Previous by thread: Re: tcl-trace
- Next by thread: Tcl (Cisco) ping program.....
- Index(es):
Relevant Pages
|