Version 2 of ?set

Updated 2004-05-14 07:05:03

GPS Oct 16, 2003 - Today it occurred to me that I might accidentally set the wrong variable in my Perpheon language translator, and it could cause me to have a nasty debug session, so I decided to come up with a proc that sets a variable but returns an error if it doesn't exist.

 proc ?set {v_name val} {
  upvar $v_name v
  if {![info exists v]} {
   return -code error "expected $v_name to exist!"
  }
  set v $val
 } ;# GPS

Here is a log demonstrating usage:

 % ?set foo bar
 error: variable doesn't exist.
 % set foo abc
 abc
 % ?set foo bar
 bar
 % set foo
 bar
 % 

See also ?append


Category Command