Updated 2018-09-20 23:47:35 by jdc

Richard Suchenwirth 2005-09-04 - The following is a chapter in the WikiBook http://en.wikibooks.org/wiki/Programming:Tcl I'm currently working on (mostly by pasting, and polishing, pages from this Wiki :^) . Everybody is welcome to check it out. But I'll continue to put my pages on Tcl here first, and copy them to the WikiBook where appopriate.

RLH Thanks for helping to flesh out the wikibook. RS Welcome - it has grown from less-than-a-page to a kind of brochure by now :)

AMG, regarding [help]: I put a -help option in my [timebox] widget, found at [1] and timeentry.

Interaction with Tcl

Tcl itself is quite a good teacher. Don't be afraid to do something wrong, as you'll most often get a helpful error message. When tclsh is called with no arguments, it starts in interactive mode and displays a "%" prompt. You type something in, and see what comes out (the result, or an error message). Here's a commented session transcript:
 % hello
 invalid command name "hello"

OK, so we're supposed to type in a command. Although it doesn't look so, here's one:
 % hi
     1  hello
     2  hi

Interactive tclsh tries to guess what we mean, and "hi" is the umambiguous prefix of the "history" command, whose results we see here. Another command worth remembering is "info":
 % info
 wrong # args: should be "info option ?arg arg ...?"

Error message: it tells us there should be at least one option, and optionally more arguments.
 % info option
 bad option "option": must be args, body, cmdcount, commands, complete, default,
 exists, functions, globals, hostname, level, library, loaded, locals, nameofexecutable,
 patchlevel, procs, script, sharedlibextension, tclversion, or vars

Another helpful error: "option" is not an option, but the valid ones are listed. To get information about commands, it makes sense to type the following (v 8.4.12):
 % info commands
 tell socket subst lremove open eof tkcon_tcl_gets pwd glob list exec pid echo
 dir auto_load_index time unknown eval lrange tcl_unknown fblocked lsearch gets
 auto_import case lappend proc break dump variable llength tkcon auto_execok
 return pkg_mkIndex linsert error bgerror catch clock info split thread_load
 loadvfs array if idebug fconfigure concat join lreplace source fcopy global
 switch which auto_qualify update tclPkgUnknown close clear cd for auto_load
 file append format tkcon_puts alias what read package set unalias
 pkg_compareExtension binary namespace scan edit trace seek while flush after
 more vwait uplevel continue foreach lset rename tkcon_gets fileevent regexp
 tkcon_tcl_puts observe_var tclPkgSetup upvar unset encoding expr load regsub
 history exit interp puts incr lindex lsort tclLog observe ls less string

Oh my, quite many... How many?
 % llength [info commands]
 115

Now for a more practical task - let's let Tcl compute the value of Pi.
 % expr acos(-1)
 3.14159265359

Hm.. can we have that with more precision?
 % set tcl_precision 17
 17
 % expr acos(-1)
 3.1415926535897931

Back to the first try, where "hello" was an invalid command. Let's just create a valid one:
 % proc hello {} {puts Hi!}

Silently acknowledged. Now testing:
 % hello
 Hi!

It surely would be nice if Tcl had a help command. TclX had a nice one - too bad it didn't catch on more.