subst(n)

✨ subst(n) β€” the Tcl glow-up machine ✨

okay bestie, so this one lives in Tcl Built-In Commands and it's literally called subst, and no cap it's the one that does your backslash, command, and variable substitutions for you πŸ’…


πŸŽ€ NAME πŸŽ€

subst β€” perform backslash, command, and variable substitutions


🌸 SYNOPSIS 🌸

subst ?-nobackslashes? ?-nocommands? ?-novariables? string

πŸ’– DESCRIPTION πŸ’–

So like, this command does variable substitutions, command substitutions, and backslash substitutions on its string argument, and hands you back the fully-substituted result. The substitutions happen in exactly the same way as for Tcl commands. Which means β€” and this is the part that gets people, bestie β€” the string argument is actually substituted twice: once by the Tcl parser in the usual fashion for Tcl commands, and then again by the subst command. two glow-ups. double the slay. πŸ”₯

If any of -nobackslashes, -nocommands, or -novariables are specified, then the corresponding substitutions are not performed. For example, if -nocommands is specified, command substitution is not performed: open and close brackets are treated as ordinary characters with no special interpretation. they're just vibing as plain text at that point πŸ¦‹

Now the plot twist: the substitution of one kind can include substitution of other kinds. For example, even when the -novariables option is specified, command substitution is performed without restriction. This means that any variable substitution necessary to complete the command substitution will still take place. Likewise, any command substitution necessary to complete a variable substitution will take place, even when -nocommands is specified. See the EXAMPLES below, because honestly the examples eat πŸ’…

If an error occurs during substitution, then subst will return that error. If a break exception occurs during command or variable substitution, the result of the whole substitution will be the string (as substituted) up to the start of the substitution that raised the exception. If a continue exception occurs during the evaluation of a command or variable substitution, an empty string will be substituted for that entire command or variable substitution (as long as it is well-formed Tcl). If a return exception occurs, or any other return code is returned during command or variable substitution, then the returned value is substituted for that substitution. See the EXAMPLES below. In this way, all exceptional return codes are β€œcaught” by subst. The subst command itself will either return an error, or will complete successfully. it catches everything, it's giving main character energy fr fr πŸ‘‘


🌈 EXAMPLES 🌈

When it performs its substitutions, subst does not give any special treatment to double quotes or curly braces (except within command substitutions) so the script

set a 44
subst {xyz {$a}}

returns β€œxyz {44}”, not β€œxyz {$a}” and the script

set a "p\} q \{r"
subst {xyz {$a}}

returns β€œxyz {p} q {r}”, not β€œxyz {p} q {r}”. see? the braces are just vibes to subst, it does not care 😭

When command substitution is performed, it includes any variable substitution necessary to evaluate the script.

set a 44
subst -novariables {$a [format $a]}

returns β€œ$a 44”, not β€œ$a $a”. Similarly, when variable substitution is performed, it includes any command substitution necessary to retrieve the value of the variable.

proc b {} {return c}
array set a {c c [b] tricky}
subst -nocommands {[b] $a([b])}

returns β€œ[b] c”, not β€œ[b] tricky”. lowkey iconic how it does that 🧚

The continue and break exceptions allow command substitutions to prevent substitution of the rest of the command substitution and the rest of string respectively, giving script authors more options when processing text using subst. For example, the script

subst {abc,[break],def}

returns β€œabc,”, not β€œabc,,def” and the script

subst {abc,[continue;expr {1+2}],def}

returns β€œabc,,def”, not β€œabc,3,def”.

Other exceptional return codes substitute the returned value

subst {abc,[return foo;expr {1+2}],def}

returns β€œabc,foo,def”, not β€œabc,3,def” and

subst {abc,[return -code 10 foo;expr {1+2}],def}

also returns β€œabc,foo,def”, not β€œabc,3,def”. the return really said "I'm the value now" πŸ’€


πŸ‘‘ SEE ALSO πŸ‘‘


⭐ KEYWORDS ⭐

  • backslash substitution
  • command substitution
  • quoting
  • substitution
  • variable substitution

ngl bestie, the double-substitution thing is the one to remember β€” if your string has a $ or a [ in it, Tcl already looked at it once before subst even showed up πŸ™ˆ

okay that's the whole page, you're gonna slay this, periodt πŸ’–βœ¨

this has been subst(n) from Tcl 7.4 β€” yes, 7.4, vintage but still serving πŸ«ΆπŸŽ€

πŸ“¦ where this page comes from