Skip to content
Tcl Source Code Artifact [d4bb129727]
Login

Artifact [d4bb129727]

Artifact d4bb129727d0f0ea8c257d953bc4a8b5e384faec0b34cc67886f066eb13f8274:


---
CommandName: Tcl_SubstObj
title: Tcl_SubstObj
ManualSection: 3
Version: 8.4
TclPart: Tcl
TclDescription: Tcl Library Procedures
Links:
 - subst(n)
Keywords:
 - backslash substitution
 - command substitution
 - variable substitution
Copyright:
 - Copyright (c) 2001 Donal K. Fellows
---

# Name

Tcl\_SubstObj - perform substitutions on Tcl values

# Synopsis

::: {.synopsis} :::
**#include <tcl.h>**
[Tcl\_Obj \*]{.ret} [Tcl\_SubstObj]{.ccmd} [interp, objPtr, flags]{.cargs}
:::

# Arguments

::: {.arguments} :::

[\*interp]{.carg .in type="Tcl_Interp"}
: Interpreter in which to execute Tcl scripts and lookup variables.  If an error occurs, the interpreter's result is modified to hold an error message.

[\*objPtr]{.carg .in type="Tcl_Obj"}
: A Tcl value containing the string to perform substitutions on.

[flags]{.carg .in type="int"}
: OR'ed combination of flag bits that specify which substitutions to perform.  The flags **TCL\_SUBST\_COMMANDS**, **TCL\_SUBST\_VARIABLES** and **TCL\_SUBST\_BACKSLASHES** are currently supported, and **TCL\_SUBST\_ALL** is provided as a convenience for the common case where all substitutions are desired.

:::

# Description

The **Tcl\_SubstObj** function is used to perform substitutions on strings in the fashion of the [subst] command.  It gets the value of the string contained in *objPtr* and scans it, copying characters and performing the chosen substitutions as it goes to an output value which is returned as the result of the function.  In the event of an error occurring during the execution of a command or variable substitution, the function returns NULL and an error message is left in *interp*'s result.

Three kinds of substitutions are supported.  When the **TCL\_SUBST\_BACKSLASHES** bit is set in *flags*, sequences that look like backslash substitutions for Tcl commands are replaced by their corresponding character.

When the **TCL\_SUBST\_VARIABLES** bit is set in *flags*, sequences that look like variable substitutions for Tcl commands are replaced by the contents of the named variable.

When the **TCL\_SUBST\_COMMANDS** bit is set in *flags*, sequences that look like command substitutions for Tcl commands are replaced by the result of evaluating that script.  Where an uncaught "continue exception" occurs during the evaluation of a command substitution, an empty string is substituted for the command.  Where an uncaught "break exception" occurs during the evaluation of a command substitution, the result of the whole substitution on *objPtr* will be truncated at the point immediately before the start of the command substitution, and no characters will be added to the result or substitutions performed after that point.

# Reference count management

The *objPtr* argument to **Tcl\_SubstObj** must not have a reference count of zero. This function modifies the interpreter result, both on success and on failure; the result of this function on success is exactly the current interpreter result. Successful results should have their reference count incremented if they are to be retained.


[subst]: subst.md