# Tcl

Appeared in:
1988
Influenced by:
Typing discipline:
File extensions:
.tcl
Versions and implementations (Collapse all | Expand all):
Programming language

Tcl (abbreviation for “Tool Command Language”) is a scripting language. It is used for rapid prototyping, writing automated testing harnesses and scripting applications.

Tcl was created by John Ousterhout in 1988. The intention was to create a simple interpreted language that would provide a set of basic programming facilities, would be extensible and could be easily integrated and reused in many tools.

Around 1990 Tcl got accompanied by Tk — a GUI toolkit that allowed to develop graphical user interfaces for Unix. The combination of Tcl’s embeddability and Tk’s ease of use led to rapid growth of language popularity.

Main features of Tcl:

• programs are compiled to bytecode and then interpreted.
• all elements of the program, including things like loops and function definitions, are commands.
• commands are written in prefix notation, i.e., command name is first and arguments follow.
• most commands are variadic, i.e., can take varying number of arguments.
• all commands generate error messages when used improperly.
• the language provides multiple data types; however, all data and source code can be manipulated as strings.
• all values are immutable: whenever they need to be changed, they are just replaced with newly created values.
• Tcl didn’t provide support for object-oriented programming until version 8.6; but OOP could be used via extension packages.

Tcl expressions are written either in curly braces (to suppress all substitutions within them) or as-is or in double braces, in which case three types of substitution are used:

• command substitution replaces the contents of squared brackets with the result of its evaluation, for example, `[expr 1+2]` will be replaced with 3.
• variable substitution replaces the name of a variable preceded by `\$` sign with its contents.
• backslash substitution is similar to other languages: a backslash followed by a character is replaced with another character.

## Elements of syntax:

Inline comments # set { ... } < > <= >= proc () or ; OR newline if {condition} { ... } if {condition} { ... } elseif {condition} { ... } else { ... } while (condition) ... for {set i 0} {\$i <= 10} {incr i} { ... } Tcl logo

## Examples:

### Fibonacci numbers:

Example for versions ActiveTcl 8.5, Tcl 8.5.7

This example uses iterative definition of Fibonacci numbers. `lassign` assigns successive elements from the list passed as first argument (created by `[list ...]`) to the variables given by the next arguments (`fib1` and `fib2` in this case). This command was moved to language core in Tcl 8.5; before that it was part of TclX package.

``````set fib1 0
set fib2 1
set s ""
for {set i 0} {\$i < 16} {incr i} {
lassign [list \$fib2 [incr fib2 \$fib1]] fib1 fib2
append s "\$fib1, "
}
puts "\$s..."
``````

### Fibonacci numbers:

Example for versions ActiveTcl 8.5, Tcl 8.5.7

This example uses recursive definition of Fibonacci numbers. Function `fib` is defined in namespace `tcl::mathfunc`, so that it can be used as math function in `expr` expressions.

``````proc tcl::mathfunc::fib {n} {
if {\$n<=1} {
return 1
} else {
return [expr fib([expr {\$n - 1}]) + fib([expr {\$n - 2}])]
}
}

set s ""
for {set i 0} {\$i < 16} {incr i} {
append s [expr fib(\$i)] ", "
}
puts "\$s..."
``````

### Hello, World!:

Example for versions ActiveTcl 8.5, JTcl 2.1.0, Tcl 8.4, Tcl 8.5.7
``````puts "Hello, World!"
``````

### Factorial:

Example for versions ActiveTcl 8.5, JTcl 2.1.0, Tcl 8.4, Tcl 8.5.7

This example uses iterative factorial definition. In Tcl 8.4 values of factorials starting with 13! are calculated incorrectly due to overflow. In later versions and other implementations all values are correct.

``````set fact 1
for {set i 0} {\$i <= 16} {incr i} {
puts "\$i! = \$fact"
set fact [expr {\$fact * (\$i + 1)}]
}
``````

Example for versions ActiveTcl 8.5, JTcl 2.1.0, Tcl 8.4, Tcl 8.5.7
``````set A [gets stdin]
if {\$A==0} {
return
}
set B [gets stdin]
set C [gets stdin]
set D [expr {\$B*\$B-4*\$A*\$C}]
set r [expr {-0.5*\$B/\$A}]
set i [expr {0.5*sqrt(abs(\$D))/\$A}]
if {\$D==0} {
puts "x = \$r"
} elseif {\$D>0} {
puts "x1 = [expr {\$r+\$i}]"
puts "x2 = [expr {\$r-\$i}]"
} else {
puts "x1 = (\$r, \$i)"
puts "x2 = (\$r, [expr {-\$i}])"
}
``````

### CamelCase:

Example for versions ActiveTcl 8.5, JTcl 2.1.0, Tcl 8.4, Tcl 8.5.7

This example shows character-wise string processing. A regular expression is used to check whether current character is a letter.

``````set S [gets stdin]
set S [string tolower \$S]
set L [string length \$S]
set lastSpace 1
set cc ""
for {set i 0} {\$i < \$L} {incr i} {
set letter [string index \$S \$i]
if { [string match {[a-z]} \$letter] } {
if { \$lastSpace == 1 } { set letter [string toupper \$letter] }
append cc \$letter
set lastSpace 0
} else {
set lastSpace 1
}
}
puts \$cc
``````

### Fibonacci numbers:

Example for versions ActiveTcl 8.5, JTcl 2.1.0, Tcl 8.4, Tcl 8.5.7

This example uses iterative definition of Fibonacci numbers.

``````set fib1 0
set fib2 1
set s ""
for {set i 0} {\$i < 16} {incr i} {
set fib3 [expr {\$fib1 + \$fib2}]
set fib1 \$fib2
set fib2 \$fib3
append s "\$fib1, "
}
puts "\$s..."
``````

### Fibonacci numbers:

Example for versions ActiveTcl 8.5, JTcl 2.1.0, Tcl 8.4, Tcl 8.5.7

This example uses tail recursion to calculate the numbers. `eval` command allows to evaluate the result of calling `fib` with given arguments without declaring `fib` as part of any namespace.

``````proc fib {f1 f2 n} {
if {\$n==0} {
return \$f1
} else {
return [eval fib \$f2 [expr {\$f1 + \$f2}] [expr {\$n - 1}]]
}
}

set s ""
for {set i 0} {\$i < 16} {incr i} {
append s [eval fib 1 1 \$i] ", "
}
puts "\$s..."
``````
{"ft_lang_label":"__label__en","ft_lang_prob":0.6674441,"math_prob":0.84056455,"size":5981,"snap":"2023-14-2023-23","text_gpt3_token_len":1767,"char_repetition_ratio":0.11092521,"word_repetition_ratio":0.14985591,"special_character_ratio":0.31600067,"punctuation_ratio":0.1638935,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.9849624,"pos_list":[0,1,2],"im_url_duplicate_count":[null,3,null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2023-06-09T04:09:25Z\",\"WARC-Record-ID\":\"<urn:uuid:408358c3-5f36-4fc8-8d48-86e2474995e0>\",\"Content-Length\":\"35258\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:ca3658ca-bbdc-44fe-ae9c-b3da17e10f07>\",\"WARC-Concurrent-To\":\"<urn:uuid:0bb226c9-aeb9-4834-9b5d-0c2cbc91d338>\",\"WARC-IP-Address\":\"216.237.118.42\",\"WARC-Target-URI\":\"http://progopedia.com/language/tcl/\",\"WARC-Payload-Digest\":\"sha1:KDDHDHEVILS5JQ7NHOOWKP72Z3V7NPVE\",\"WARC-Block-Digest\":\"sha1:4HIQIV5NINKEM2EHUVM2LIXBQSZY5KPF\",\"WARC-Identified-Payload-Type\":\"application/xhtml+xml\",\"warc_filename\":\"/cc_download/warc_2023/CC-MAIN-2023-23/CC-MAIN-2023-23_segments_1685224655247.75_warc_CC-MAIN-20230609032325-20230609062325-00496.warc.gz\"}"}