On Wednesday, 24 June 2015 15:29:08 UTC+2, Don Porter wrote:
> On 06/22/2015 05:54 AM, tombert wrote:
> > Hi All,
> >
> > I was trying to trace a NS (origin) variable from another NS (testns). But when the CommandPrefix procedure is triggered, it is without the NS context - Instead of "origin::myvar1" it gets "myvar1" only.
> >
> > The manual says: "CommandPrefix executes in the same context as the code that invoked the traced operation" but simply adding a "variable $name" inside the CommandPrefix "proc global" does not help.
> >
> > A workaround would be adding some custom command parameters to the "CommandPrefix" but I was hoping for a better explanation if I am doing something wrong.
>
> Quick answer: you need to use [upvar].
>
> --
> | Don Porter Applied and Computational Mathematics Division |
> | donald dot porter et
nist.gov Information Technology Laboratory |
> |
http://math.nist.gov/~DPorter/ NIST |
> |______________________________________________________________________|
thx, this helped a little bit. Still I do not get the context. I tried using uplevel, then call [namespace current] but it returns the context of "testns::sync". Wheras I expect it to be "origin".
proc global {name index op} {
uplevel 1 [list
puts [namespace current] ;# returns wrong context
puts "'$name' was changed to '[set $name]'" ;# error no such variable
]
}
And I tried [upvar]. Here I can read the correct value of "myvar". But the request to [namespace which -variable myvar] results in an empty string.
proc global {name index op} {
upvar $name myvar
puts [namespace which -variable myvar] ;# returns an emtpy string
puts "'$name' was changed to '[set $name]'" ;# returns correct value
}
When reading the manual "CommandPrefix executes in the same context as the code that invoked the traced operation" I would expect either [uplevel] or [upvar] together with [namespace current] or [namespace which -variable] to get the actual context returned.
thx