Ever since I came across a Tcl programming book in my college library I've always been fascinated with it. I return to it every couple of years to play around with meta-programming ideas that I've come up with.
For example, here is a really naive example of implementing a DSL that allows you to write something like
SELECT name age FROM person INTO name age
and have the values of the selected columns be placed in variables specified after the INTO keyword.Implementing that isn't too difficult:
# a pretend database
set language(name) Tcl
set language(created) 1988
set language(created_by) "John Ousterhout"
set language(stable_release) 8.6.11
proc SELECT {args} {
### Lowercase all the args to make things easy
set args [string tolower $args]
set from_index [lsearch $args from]
set cols [lrange $args 0 [incr from_index -1]]
set table [lindex $args [expr $from_index + 2]]
# Get access to the global variable with the value contained in $table
global $table
set into_index [lsearch $args into]
set vars [lrange $args [incr into_index] end]
foreach c $cols v $vars {
upvar $v local_var
set val [lindex [array get $table $c] 1]
set local_var $val
}
}
After that, you could "SELECT created name created_by FROM language INTO x y z" and then puts "$y was created by $z in $x"
>Tcl was created by John Ousterhout in 1988