1 | '\" |
---|
2 | '\" Copyright (c) 1994 The Regents of the University of California. |
---|
3 | '\" Copyright (c) 1994-1996 Sun Microsystems, Inc. |
---|
4 | '\" Copyright (c) 2001 Donal K. Fellows |
---|
5 | '\" |
---|
6 | '\" See the file "license.terms" for information on usage and redistribution |
---|
7 | '\" of this file, and for a DISCLAIMER OF ALL WARRANTIES. |
---|
8 | '\" |
---|
9 | '\" RCS: @(#) $Id: subst.n,v 1.16 2007/12/13 15:22:33 dgp Exp $ |
---|
10 | '\" |
---|
11 | .so man.macros |
---|
12 | .TH subst n 7.4 Tcl "Tcl Built-In Commands" |
---|
13 | .BS |
---|
14 | '\" Note: do not modify the .SH NAME line immediately below! |
---|
15 | .SH NAME |
---|
16 | subst \- Perform backslash, command, and variable substitutions |
---|
17 | .SH SYNOPSIS |
---|
18 | \fBsubst \fR?\fB\-nobackslashes\fR? ?\fB\-nocommands\fR? ?\fB\-novariables\fR? \fIstring\fR |
---|
19 | .BE |
---|
20 | .SH DESCRIPTION |
---|
21 | .PP |
---|
22 | This command performs variable substitutions, command substitutions, |
---|
23 | and backslash substitutions on its \fIstring\fR argument and |
---|
24 | returns the fully-substituted result. |
---|
25 | The substitutions are performed in exactly the same way as for |
---|
26 | Tcl commands. |
---|
27 | As a result, the \fIstring\fR argument is actually substituted twice, |
---|
28 | once by the Tcl parser in the usual fashion for Tcl commands, and |
---|
29 | again by the \fIsubst\fR command. |
---|
30 | .PP |
---|
31 | If any of the \fB\-nobackslashes\fR, \fB\-nocommands\fR, or |
---|
32 | \fB\-novariables\fR are specified, then the corresponding substitutions |
---|
33 | are not performed. |
---|
34 | For example, if \fB\-nocommands\fR is specified, command substitution |
---|
35 | is not performed: open and close brackets are treated as ordinary characters |
---|
36 | with no special interpretation. |
---|
37 | .PP |
---|
38 | Note that the substitution of one kind can include substitution of |
---|
39 | other kinds. For example, even when the \fB\-novariables\fR option |
---|
40 | is specified, command substitution is performed without restriction. |
---|
41 | This means that any variable substitution necessary to complete the |
---|
42 | command substitution will still take place. Likewise, any command |
---|
43 | substitution necessary to complete a variable substitution will |
---|
44 | take place, even when \fB\-nocommands\fR is specified. See the |
---|
45 | \fBEXAMPLES\fR below. |
---|
46 | .PP |
---|
47 | If an error occurs during substitution, then \fBsubst\fR will return |
---|
48 | that error. If a break exception occurs during command or variable |
---|
49 | substitution, the result of the whole substitution will be the |
---|
50 | string (as substituted) up to the start of the substitution that |
---|
51 | raised the exception. If a continue exception occurs during the |
---|
52 | evaluation of a command or variable substitution, an empty string |
---|
53 | will be substituted for that entire command or variable substitution |
---|
54 | (as long as it is well-formed Tcl.) If a return exception occurs, |
---|
55 | or any other return code is returned during command or variable |
---|
56 | substitution, then the returned value is substituted for that |
---|
57 | substitution. See the \fBEXAMPLES\fR below. In this way, all exceptional |
---|
58 | return codes are |
---|
59 | .QW caught |
---|
60 | by \fBsubst\fR. The \fBsubst\fR command |
---|
61 | itself will either return an error, or will complete successfully. |
---|
62 | .SH EXAMPLES |
---|
63 | .PP |
---|
64 | When it performs its substitutions, \fIsubst\fR does not give any |
---|
65 | special treatment to double quotes or curly braces (except within |
---|
66 | command substitutions) so the script |
---|
67 | .CS |
---|
68 | set a 44 |
---|
69 | \fBsubst\fR {xyz {$a}} |
---|
70 | .CE |
---|
71 | returns |
---|
72 | .QW "\fBxyz {44}\fR" , |
---|
73 | not |
---|
74 | .QW "\fBxyz {$a}\fR" |
---|
75 | and the script |
---|
76 | .CS |
---|
77 | set a "p\e} q \e{r" |
---|
78 | \fBsubst\fR {xyz {$a}} |
---|
79 | .CE |
---|
80 | returns |
---|
81 | .QW "\fBxyz {p} q {r}\fR" , |
---|
82 | not |
---|
83 | .QW "\fBxyz {p\e} q \e{r}\fR". |
---|
84 | .PP |
---|
85 | When command substitution is performed, it includes any variable |
---|
86 | substitution necessary to evaluate the script. |
---|
87 | .CS |
---|
88 | set a 44 |
---|
89 | \fBsubst\fR -novariables {$a [format $a]} |
---|
90 | .CE |
---|
91 | returns |
---|
92 | .QW "\fB$a 44\fR" , |
---|
93 | not |
---|
94 | .QW "\fB$a $a\fR" . |
---|
95 | Similarly, when |
---|
96 | variable substitution is performed, it includes any command |
---|
97 | substitution necessary to retrieve the value of the variable. |
---|
98 | .CS |
---|
99 | proc b {} {return c} |
---|
100 | array set a {c c [b] tricky} |
---|
101 | \fBsubst\fR -nocommands {[b] $a([b])} |
---|
102 | .CE |
---|
103 | returns |
---|
104 | .QW "\fB[b] c\fR" , |
---|
105 | not |
---|
106 | .QW "\fB[b] tricky\fR" . |
---|
107 | .PP |
---|
108 | The continue and break exceptions allow command substitutions to |
---|
109 | prevent substitution of the rest of the command substitution and the |
---|
110 | rest of \fIstring\fR respectively, giving script authors more options |
---|
111 | when processing text using \fIsubst\fR. For example, the script |
---|
112 | .CS |
---|
113 | \fBsubst\fR {abc,[break],def} |
---|
114 | .CE |
---|
115 | returns |
---|
116 | .QW \fBabc,\fR , |
---|
117 | not |
---|
118 | .QW \fBabc,,def\fR |
---|
119 | and the script |
---|
120 | .CS |
---|
121 | \fBsubst\fR {abc,[continue;expr {1+2}],def} |
---|
122 | .CE |
---|
123 | returns |
---|
124 | .QW \fBabc,,def\fR , |
---|
125 | not |
---|
126 | .QW \fBabc,3,def\fR . |
---|
127 | .PP |
---|
128 | Other exceptional return codes substitute the returned value |
---|
129 | .CS |
---|
130 | \fBsubst\fR {abc,[return foo;expr {1+2}],def} |
---|
131 | .CE |
---|
132 | returns |
---|
133 | .QW \fBabc,foo,def\fR , |
---|
134 | not |
---|
135 | .QW \fBabc,3,def\fR |
---|
136 | and |
---|
137 | .CS |
---|
138 | \fBsubst\fR {abc,[return -code 10 foo;expr {1+2}],def} |
---|
139 | .CE |
---|
140 | also returns |
---|
141 | .QW \fBabc,foo,def\fR , |
---|
142 | not |
---|
143 | .QW \fBabc,3,def\fR . |
---|
144 | .SH "SEE ALSO" |
---|
145 | Tcl(n), eval(n), break(n), continue(n) |
---|
146 | .SH KEYWORDS |
---|
147 | backslash substitution, command substitution, variable substitution |
---|