Here is a code example:
set my_ref {$undefined_array(some_key)}
set my_val [subst $my_ref]
which returns:
can't read "undefined_array(some_key)": no such variable
while executing
"subst $my_ref"
According to http://wiki.tcl.tk/1501
Looks like there is no way to catch this right now
When subst
attempts to perform substitutions on the text you gave it, it needs an existing variable with a matching name. If no such variable exists, subst
throws a TCL LOOKUP VARNAME
exception.
How to catch that? You can catch the exception after subst
has failed as usual, with catch
or try
. The discussion you referred to was AFAICT about catching exceptions before subst
has failed, which I believe still isn't possible.
ETA:
Proof-of-concept for my "discriminating try" comment. This code has tons of potential problems but at least demonstrates basically how it could be done. In the example, the handler reacts by creating a variable that has its own name in uppercase as value.
# CAUTION: demonstration code, do not use without modification
proc handler varName {
upvar 1 $varName name
set name [string toupper $varName]
}
unset -nocomplain foo bar
set baz xyz
set str {$foo $bar $baz}
while true {
try {
subst $str
} on ok res {
break
} trap {TCL LOOKUP VARNAME} {msg opts} {
handler [lindex [dict get $opts -errorcode] end]
}
}
set res
# -> FOO BAR xyz