Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

variable tracing question

Status
Not open for further replies.

fernandovs

Technical User
Apr 2, 2011
1
CR
hi,

I'm kinda new in TCL im working right now with the "Tcl and the Tk toolkit book" 2nd edition. I'm checking about variable tracing right now, and I fount this example:

trace add variable size write forceint
proc forceint {name element op} {
upvar 1 $name x ${name}_old x_old
if {[regexp {^\d+$} $x]} {
set x $x_old
error "value must be a positive integer"
}
set x_old $x
}

According to the book this script is going to set up a read-only variable, however it is not working for me, when i run this script on TCL 8.5 on linux i have:

% set size 47
can't set "size": can't read "x_old": no such variable

I have some serious doubts about this variable x ${name}_old, since a write trace is invoked after the variable's value is changed, i don't think that this _old variable exists and of course the script wouldn't work.

Please tell me your thoughts about this script and if it doesn't work is there any workaround to implement this read-only variable in TCL with tracing?

thanks all for your help.
 
It does seem odd to me. I think the statement:
upvar 1 $name x ${name}_old x_old
is expecting that both a variable named whatever the contents of the variable, "name", and a variable, "name" exist in the global scope.

_________________
Bob Rashkin
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top