Version 8 of expand

Updated 2004-06-10 15:12:04 by NEM

A macro and/or template processor written in Tcl for Tcl, by William Duquette. See http://www.wjduquette.com/expand/index.html for its homepage.

This is a useful tool for conversion of tcl-based documents into other formats. The range of applications include

  • Generation of website with uniform look & feel. See the homepage [L1 ] of the expand author, or AK's memchan website at SourceForge [L2 ] for examples of this.
  • Conversion of manpages in a tcl-based markup to HTML, nroff, TMML, etc.
  • Generation and setup of Makefiles for an extension based upon a template.
  • Provide a generic support to update a set of files depending on single definition file (tcl script)

All of this falls under the broad category of Template and Macro processing.


BUT, with 8.5, expand also becomes the closest thing Tcl has to a conventional keyword. See TIP 157 [L3 ] for the details. [expand syntax ...]

[Would someone please take on the painful task of explaining the Tcl 8.5 meaning of expand in a way that a Tcl novice will understand?]

NEM OK - here's an attempt at explaining {expand} in Tcl 8.5. The easiest way to explain is perhaps with an example. Consider the following code, which may be used for launching a browser on Windows:

 eval exec [auto_execok start] [list $url]

The reason that the [eval] is there is that [auto_execok] may return a list which represents the command to be run. This list needs to be flattened for exec. For instance, let's say [auto_execok start] returned:

 % auto_execok start
 c:/windows/command.com /c start

If you just did:

 exec [auto_execok start] $url

You would get an error about "can't find 'c:/windows/command.com /c start' - no such file or directory" or something similar. In order to flatten this list, you currently have to use [eval] which does this for you (as it concats its arguments together before evaluation). But because the $url may contain spaces, we need to protect it, so that it doesn't get flattened. Hence the use of [list], and the full line given at the start. There are lots of gotchas with quoting arguments correctly for [eval]. So, to solve this problem, 8.5 introduced the {expand} syntax, which inline expands arguments in a command invocation. What this means is that {expand}list becomes word word word... at invocation time. So, we can now rewrite our example (in 8.5) as:

 exec {expand}[auto_execok start] $url

Which is clearer, safer and better all round.


Category Application | Category Package, a part of Tcllib (/textutil)

Category Command