Tcl_SubstObj(3tcl)
π Tcl_SubstObj β bestie edition π
okay so this is a page from the Tcl Library Procedures manual, and it's all about Tcl_SubstObj(3tcl). buckle up, it's actually so good.
β¨ NAME β¨
Tcl_SubstObj β do substitutions on Tcl values, literally the subst command's whole thing but as a C function π«Ά
β¨ SYNOPSIS β¨
#include <tcl.h>
Tcl_Obj *
Tcl_SubstObj(interp, objPtr, flags)
β¨ ARGUMENTS β¨
Tcl_Interp *interp(in) β the interpreter where the Tcl scripts run and the variables get looked up. if something goes wrong, its result gets overwritten with the error message. so it's the one holding the receipts πTcl_Obj *objPtr(in) β a Tcl value whose string is the thing you want substitutions performed on. this is the main character of the operation πint flags(in) β an OR'ed-together combo of flag bits saying which substitutions you actually want. the supported ones rn areTCL_SUBST_COMMANDS,TCL_SUBST_VARIABLES, andTCL_SUBST_BACKSLASHES, and there'sTCL_SUBST_ALLas a little convenience for the super common case where you want, like, all of them. slay β¨
β¨ DESCRIPTION β¨
so Tcl_SubstObj is the function that does substitutions on strings the exact same way the subst command does. it grabs the value of the string inside objPtr, scans it, and as it goes it copies characters and performs whichever substitutions you picked, building up an output value that gets returned as the function's result. lowkey elegant π
and if an error pops off during a command or variable substitution? the function returns NULL and drops an error message into interp's result. no cap, that's the deal.
there are three kinds of substitutions supported:
- when the
TCL_SUBST_BACKSLASHESbit is set inflags, sequences that look like backslash substitutions for Tcl commands get swapped out for their corresponding character. π¦ - when the
TCL_SUBST_VARIABLESbit is set inflags, sequences that look like variable substitutions for Tcl commands get replaced by the contents of the named variable. π - when the
TCL_SUBST_COMMANDSbit is set inflags, sequences that look like command substitutions for Tcl commands get replaced by the result of evaluating that script. π
now the spicy part, listen up:
where an uncaught "continue exception" happens during the evaluation of a command substitution, an empty string is substituted in for that command. so it's just... gone. nothing there. π
where an uncaught "break exception" happens during the evaluation of a command substitution, the result of the whole substitution on objPtr gets truncated at the point immediately before the start of that command substitution. nothing gets added to the result after that point and no further substitutions get performed either. it's giving "we stop here, periodt." π₯
β¨ REFERENCE COUNT MANAGEMENT β¨
okay this part matters so don't skip it π
the objPtr argument passed to Tcl_SubstObj must not have a reference count of zero. also, this function modifies the interpreter result β and it does that both when it succeeds and when it fails. the result of this function on success is exactly the current interpreter result, no more no less. and if you wanna hang onto a successful result? bump its reference count, bestie, otherwise it's not sticking around. π«Ά
β¨ SEE ALSO β¨
subst(3tcl)β go read it, it's the sibling energy π
β¨ KEYWORDS β¨
backslash substitution, command substitution, variable substitution π
and that's the whole page, you understood it perfectly, I'm so proud of you. this one's from the Tcl 8.4 era of the manual β shoutout to that vintage, it ate β and yeah, that's Tcl_SubstObj(3tcl), we're done here. mwah ππ
β¨
π¦ where this page comes from
- debian trixie: tcl9.0-doc 9.0.1+dfsg-2
- ubuntu questing: tcl9.0-doc 9.0.2+dfsg-1
- ubuntu resolute: tcl9.0-doc 9.0.3+dfsg-1
- ubuntu stonking: tcl9.0-doc 9.0.4+dfsg-1
- Copyright (c) 2001 Donal K. Fellows
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms