Version 9 of subst

Updated 2004-06-03 14:56:18

http://www.purl.org/tcl/home/man/tcl8.4/TclCmd/subst.htm


What's the use of subst? As Joe English, who has deep experience in the area, explains, "[subst] is massively handy in text-processing applications, especially SGML and XML down-translators. [subst] and [ string map] make Tcl particularly well-suited for this type of task." Many jobs for which Perl uses "right-hand-side" variables with regular expression substitution, Tcl does as REs whose results are subst-ituted.


Very simple example of using [subst] with XML/HTML.

       set html   {<html><head>$title</head></html>}
       set title  "Hello, World!"
       set output [subst -nocommands $html]
       set output
       <html><head>Hello, World!</head></html>

Another alternative would be to use XPath I use something like this in an application of mine. Of course the "text" of the 'html' variable above would probably come from a file.

I would much rather use XPath to hunt down the place to change.


RS most often uses subst for expanding Unicodes: cross-platform, in mostly 8-bit environments, it is most robust to output Unicodes in the \u.... notation - such snippets can be pasted into a text widget and visualized by

 subst [$t get 1.0 end]

[Explain the regsub idiom.]


03jun04 jcw - It would be useful to extend subst so it lets one catch variable accesses, and perhaps even command executions. What I mean is that when you subst text with "... $var ..." then sometimes it is useful to be able to intercept the expansion, by turning it into a call such as "myhandler var" for example, the result of which then gets used as substitution. The same (perhaps less important) might apply to "... [cmd ...] ..." expansions. This makes it simpler to implement tiny languages which also use $var and $var(item) as This makes it simpler to implement tiny languages which also use "$var" and "$var(item)" as access mechanisms, but to things which are not necessarily stored in Tcl variables/arrays. Would it be an idea to extend subst so it optionally passes each of its substitutions to a command? Would it be an idea to extens subst so it optionally passes each of its substitutions to a command? Could be a "-command ..." option, or simply the presence of more args. DGP Am I missing something? Aren't you asking for variable and command traces? Which exist? for variable and command traces? Which exist?


Tcl syntax help - Arts and crafts of Tcl-Tk programming - Category Command