Tcl Tutorial Lesson 0

Introduction

Welcome to the Tcl tutorial. We wrote it with the goal of helping you to learn Tcl. It is aimed at those who have some knowledge of programming, although you certainly don't have to be an expert. The tutorial is intended as a companion to the Tcl manual pages which provide a reference for all Tcl commands.

It is divided into brief sections covering different aspects of the language. Depending on what system you are on, you can always look up the reference documentation for commands that you are curious about. On Unix for example, man while would bring up the man page for the while command.

Each section is accompanied by relevant examples showing you how to put to use the material covered.

Available lessons

Getting started

Introduction

Running Tcl

Simple Text Output

Assigning values to variables

Evaluation and Substitutions 1: Grouping arguments with ""

Evaluation and Substitutions 2: Grouping arguments with {}

Evaluation and Substitutions 3: Grouping arguments with []

Results of a command - Math 101

Computers and Numbers

Flow Control

Numeric Comparisons 101 - if

Textual Comparison - switch

Looping 101 - While loop

Looping 102 - For and incr

Adding new commands to Tcl - proc

Variations in proc arguments and return values

Variable scope - global and upvar

Data Types

Tcl Data Structures 101 - The list

Adding and Deleting members of a list

More list commands - lsearch, lsort, lrange

Simple pattern matching - "globbing"

String Subcommands - length index range

String comparisons - compare match first last wordend

Modifying Strings - tolower, toupper, trim, format

Regular Expressions 101

More Examples Of Regular Expressions

More Quoting Hell - Regular Expressions 102

Associative Arrays

More On Arrays - Iterating and use in procedures

Dictionaries

Input and Output

File Access 101

Changing Working Directory - cd, pwd

Information about Files - file, glob

Invoking Subprocesses from Tcl - exec, open

Channel I/O: socket, fileevent, vwait

More channel I/O - fblocked and fconfigure

Communicating with other programs - socket, fileevent

Time and Date - clock

Using databases

Introspection, Debugging and Performance

Learning the existence of commands and variables - info

State of the interpreter - info

Information about procs - info

Debugging and Errors - errorInfo errorCode catch error return

More Debugging - trace

Timing scripts

Modularization and Code Reuse

Modularization - source

Building reusable libraries - packages and namespaces

Building and distributing applications

Cooperating with the interpreter

Creating Commands - eval

More command construction - format, list

Substitution without evaluation - format, subst

Command line arguments and environment strings

Child interpreters

Programming techniques

Object-oriented programming 101

More on object-oriented programming

Programming with coroutines

Additional Resources

The Tcl community is an exceedingly friendly one. It's polite to try and figure things out yourself, but if you're struggling, we're more than willing to help. Here are some good places to get help:

  • The comp.lang.tcl newsgroup. Accessible via a newsreader, or Google Groups.
  • The Tcl developer site brings a great many resources together, ranging from basic tutorials to advanced development.
  • The Wiki has a great deal of useful code, examples and discussions of the finer points of Tcl usage.
  • If you need help right away, there is often someone on the #tcl channel on irc.freenode.net who can help you out, but please don't be impatient if no one can help you instantly - if you need that level of support, consider hiring a consultant.
  • There are several recommended books for those who wish to gain more in-depth knowledge of Tcl. Clif Flynt, the original author of this tutorial is also the author of Tcl/Tk: A Developer's Guide. Other popular books: Practical Programming in Tcl and Tk. More can be found via the page for Online books on the Wiki.

A recent book on Tcl, one that describes the latest features, is The Tcl Programming Language by Ashok P. Nadkarni.

Credits

Thanks first and foremost to Clif Flynt for making his material available under a BSD license. The following people also contributed:

Of course, we also welcome comments and suggestions about how it could be improved - or if it's great the way it is, we don't mind a bit of thanks, either!