Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

trace question

103 views
Skip to first unread message

Manfred

unread,
May 25, 2015, 8:19:03 AM5/25/15
to
Hi

I have a small script:

##script start

set ::y 8
set ::x 5

proc tuwas {args} {set ::x $::y}

trace add variable ::x read "tuwas"

pack [label .en -textvariable ::x]

puts "X1 :: $x"

##label show "8", commandline show "X1 :: 8" like expected

set ::y 45
set ::x

##label show "8", I expected 45. commandline show "X2:: 45", like expected.

after 5000 "set ::x $::y"

##label show "45", like expected.

##script end

Why this behavior?

regards

Manfred

Andreas Leitgeb

unread,
Jul 2, 2015, 7:49:11 AM7/2/15
to
Manfred <man...@antispam.com> wrote:
> Hi
> I have a small script:
> ##script start
> set ::y 8
> set ::x 5
> proc tuwas {args} {set ::x $::y}
> trace add variable ::x read "tuwas"
> pack [label .en -textvariable ::x]
> puts "X1 :: $x"
> ##label show "8", commandline show "X1 :: 8" like expected

Upon creation, the label once reads out the current value of its
variable and that turns out 8 due to the read-trace. So far so good.

> set ::y 45
> set ::x
>
> ##label show "8", I expected 45. commandline show "X2:: 45", like expected.

After that, it'll register it's own write-trace on its variable x,
and this trace doesn't get fired, if the assignment happens from
within another trace namely from the read-trace triggered from "set x".

> after 5000 "set ::x $::y"
> ##label show "45", like expected.

Here, the assignment to x happens outside any trace-handler, and thus
seen by the label.

Manfred

unread,
Jul 6, 2015, 1:39:08 PM7/6/15
to
Thank's for the answer.

But if I call the proc "tuwas" in the script
e.g:
after 5000 "tuwas"

the label show 45.

I have one proc "tuwas",
one time called by trace,
one time called in the script,
but I have two different behavior.

Why?
I don't understand this.

regards

manfred

Andreas Leitgeb

unread,
Jul 7, 2015, 3:19:43 PM7/7/15
to
Manfred <man...@antispam.com> wrote:
> Thank's for the answer.
> But if I call the proc "tuwas" in the script
> e.g:
> after 5000 "tuwas"
> the label show 45.

Yes, because an after-script is not a trace-handler, so does not
prevent other traces, whereas a trace-handler does.
Tcl doesn't call any trace-handlers while one trace-handler is
still on the stack - no matter how far that is up.

You could effectively disable traces completely for a script, if
you put your scripts main actions into a trace on some variable
and then do the traced action. ;-)

It doesn't matter, if the trace-script does the "set" directly,
or indirectly through a procedure call.

Manfred

unread,
Jul 7, 2015, 3:59:01 PM7/7/15
to
Now I have a solution.
I changed the proc "tuwas" a little bit.

proc tuwas {args} {after 0 "set ::x $::y"}

With the "after" in the proc it work's like expected.

regards

Manfred

heinrichmartin

unread,
Jul 8, 2015, 3:02:55 AM7/8/15
to
On Tuesday, July 7, 2015 at 9:59:01 PM UTC+2, Manfred wrote:
> proc tuwas {args} {after 0 "set ::x $::y"}

Make it {after 0 [list set ::x $::y]} or it fails if $::y contains whitespace.

(And iirc after callbacks are executed in the global namespace, so {after 0 [list set x $y]} will do.)

heinrichmartin

unread,
Jul 8, 2015, 6:55:05 AM7/8/15
to
Erratum: $::y is necessary as it is evaluated inside tuwas.
0 new messages