Updated 2013-11-01 09:23:25 by suchenwi

This template code is for starting a typical tclsh application.
#! /bin/sh
# the next line restarts using tclsh \
exec tclsh "$0" ${1+"$@"}

This is a little more sophisticated than the code suggested on the tclsh man page (see tcl), and is explained in great detail on exec magic.

Typical patterns to continue are:
# verify Tcl version requirements
package require Tcl 8.4    ;# My application uses [lset]
# process stdin:
while {[gets stdin line]>=0} {
    #do something with $line
# process argv:
foreach arg $argv {
    # do something with $arg

RS 2013-11-01 - My typical template for Tcl scripts, as evolved over the years, is now:
#!/usr/bin/env tclsh
set usage {$Id:$
   usage: scriptname.tcl ?-x this? infile > outfile
   Does something useful with infile, outputs to stdout.
   -x this: also do something else
if {[llength $argv] < 1} {puts stderr $usage; exit 1}

proc main argv {
proc foo ...
main $argv

The first line takes care that the script can be called with the necessary interpreter. The second line will contain name, date, version etc. when the script is committed to CVS. The usage message is on top, so one can see it with head, or when calling the script without arguments. I write that before I write the code, as kind of a spec for myself.

After that, (almost) all code is in procs - byte-compiled, variables local by default. I can structure it top-down. When I need global variables, I typically have them all in one array called "g", as reminder.