Version 4 of Script substitution

Updated 2019-11-24 18:13:29 by pooryorick

Command Substitution is one of the fundamental features of Tcl, in which a script is evaluated and the result is substituted into a word in a command. A new stack frame is not created when the script is evaluated, so using return or break or the like will cause the caller to return, etc.

Recursion

PYK 2019-02-12 2019-11-24: To perform each command subsitution, including nested substitutions, Tcl evaluates the script at the current level. No additional levels are created:

puts [
    set n 10
    set a {
        puts $n
        expr {[incr n -1]? [try $a] : {liftoff}}
    }
    try $a
]

Comments

AMG: Command substitution can be used in combination with list and {*} to embed comments in lists. The syntax is awkward, but it does work:

switch -regexp $input [list {*}[
# Handle words starting with a capital letter
] {^[A-Z]} {
    # Do the thing
    theThing
} {*}[
# Handle everything else
] default {
    # Don't do the thing
}]

Though also consider using decomment for this same purpose.