Main Content

Expanding a list of parameters in Tcl - {*} and eval

Archive - Originally posted on "The Horse's Mouth" - 2012-01-17 10:03:03 - Graham Ellis

In Tcl commands, parameters are separated by spaces, just like Tcl lists are. So it would sometimes be very useful to be able to write
  set action {piemiddle apple}
  set $action

to take a list of parameters from a Tcl list - in this case setting the "piemiddle" variable to the value "apple".

But it doesn't work. The problem is that the variable $action is only expanded into the string "piemiddle apple" after the command line has been split into its individual parameters, so that the Tcl program attempts to refer to a single variable called "piemiddle apple" with a space in the middle of it, giving the message:
  can't read "piemiddle apple": no such variable
which is a huge pity.

However, as of Tcl version 8.5 the expand operator was introduced, which allows you to specify that a variable is to be expanded before the command line is split into individual parameters. So if you write
  set action {piemiddle apple}
  set {*}$action

you will actually end up setting a variable called "piemiddle" to the value "apple".

The sequence {*} is, then, know as the expand operator, and forms a part of a parameter that's to be expanded early. In other words, you may not add a space between the {*} and the name of the variable to be expanded. Full source code example [here].

Note - in earlier versions of Tcl, the eval command was the recommended alternative and it remains available today. eval is often very messy to implement, as it applies to the whole command rather than just an individual scalar list, and so you end up having to add all sorts of extra protections. Here's a simple eval example - equivalent of the examples above:
  set pudding {rolypolymiddle jam}
  eval set $pudding
  puts "In rolypoly we have $rolypolymiddle"