Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: downloads/tcl8.5.2/doc/subst.n @ 25

Last change on this file since 25 was 25, checked in by landauf, 16 years ago

added tcl to libs

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