Tcl basics in 5 minutes
Tcl stands for 'tool command language' and is pronounced 'tickle.'
Starting
You start tcl by typing tcl in your Unix shell. Thus
you enter an interactive mode within tcl. You can
leave with the tcl command exit.
If you want to use the tcl tool kit (TclTk) you use wish
instead of tcl.
- tcl starting tcl
- wish starting tcl+tk
- janus starting tcl+tk+janus
> tcl
tcl> # this is a comment because the line starts with '#'
tcl> # now we define the variable text
tcl> set text "hello world"
tcl> puts $text
hello world
tcl> exit
>
Variables
Variables in tcl can be defined with the command set and
the value can be used with $variable_name. Arrays can be
indexed with arbitrary names in (). Curly braces are used to separate
variable names from following characters.
tcl> set name1 Hans
tcl> puts $name1
Hans
tcl> set name2 $name1
tcl> puts ${name2}_im_Glueck
Hans_im_Glueck
tcl> set data(name) Hans
tcl> set data(age) 35
tcl> set data(1,2) something
tcl> set index name
tcl> puts $data($index)
Hans
Commands, grouping and procedures
Commands and procedures are called with their name followed by arguments. Arguments are
separated by spaces. They can be grouped together with "" or {}. The difference is that
variables within "" will be replaced. ';' separates commands in one line.
tcl> set a 1
tcl> puts "$a + 1"
1 + 1
tcl> puts {$a + 1}
$a + 1
tcl> puts "{$a} + 1"
{1} + 1
tcl> set b 1; puts $b; # bla bla
A command and arguments within [] will be executed and [command arg1 arg2 ..]
will be replaced with the return value.
tcl> expr 1 + 2
3
tcl> puts "1 + 2 = [expr 1 + 2]"
1 + 2 = 3
The interpretation of $variable and [] can be switched off with \.
tcl> set a 999
tcl> puts "\[$a \$\]"
[999 $]
tcl> puts {[$a $]}
[$a $]
New commands or better procedures can be defined with the command proc.
tcl> proc add {a b} {return [expr $a + $b]}
tcl> add 1 2
3
Note that the procedure name 'add', the variable list '{a b}' and the body
of the function '{return [expr $a + $b]}' are the arguments of the command 'proc'.
You can also use optional arguments with their default value.
tcl> proc printText {times {text "hello word"}} {
=> for {set i 0} {$i<$times} {incr i} {
=> puts $text
=> }
=> return $times
=> }
tcl> printText 2
hello word
hello word
tcl> printText 1 "hello Monika"
hello Monika
Each procedure has a local scope for variables. But you can use the 'global'
command in a procedure to access global variables.
tcl> proc putsnames {} {global name1; puts $name1; puts $name2}
tcl> putsnames
can't read "name1": no such variable
tcl> set name1 Tanja
tcl> set name2 Petra
tcl> putsnames
Tanja
can't read "name2": no such variable
Control flow
tcl> if {$i > 0} {puts "1"} else {puts "0"}
tcl> if {"$name" == "Tilo"} {
=> #
=> #do something here
=> #
=> }
tcl> for {set i 0} {$i < 10} {incr i} {puts $i}
tcl> foreach value {1 2 3 5} {puts stdout "$value"}
tcl> while {$i>0} {incr i -1}
tcl> switch $i {
=> 1 {puts "i = 1"}
=> "hello" {puts "hi"}
=> default {puts "?"}
=> }
You can exit a loop with 'break' or 'continue' with the next iteration.
Errors
With 'catch' errors can be trapped.
tcl> if [catch {expr 1.0 / $a} result ] {
=> puts stderr $result
=> } else {
=> puts "1 / $a = $result"
=> }
File I/O
tcl> set FP [open $fileName r]
tcl> set found 0
tcl> while {[gets $FP line] >= 0} {
=> if {[string compare "ABC" $line] == 0} {set found 1; break} ;# found exactly "ABC"
=> if ![string compare "XYZ" $line] {set found 2; break} ;# found exactly "XYZ"
=> if [string match ABC*XYZ $line] {set found 3; break} ;# found "ABC..something..XYZ"
=> }
tcl> close $FPI
tcl> set FP [open $fileName r]
tcl> set first100bytes [read $FP 100]
tcl> set rest [read $FP]
tcl> close $FPI
Maintainer: westphal@ira.uka.de