Tcl (programming language)

Uses your system voice; voice and rate in settings.

Snapshot

≈ 8 min read

Names in other languages

No names in other languages are held for this entry yet.

Article bodies are English and are not machine-translated. A name in another language is not a translation of the article.

Tcl (pronounced as an acronym, , like tickle, or as an initialism, , like T-C-L; originally Tool Command Language) is a high-level, general-purpose, interpreted, dynamic programming language. It was designed with the goal of being very simple but powerful. Tcl casts everything into the mold of a command, even programming constructs like variable assignment and procedure definition. Tcl supports multiple programming paradigms, including object-oriented, imperative, functional, and procedural styles. It is commonly embedded into C applications for rapid prototyping, scripted applications, GUIs, and testing. Tcl interpreters are available for many operating systems, allowing Tcl code to run on a wide variety of systems. Because Tcl is a very compact language, it is used on embedded systems platforms, both in its full form and in several other small-footprint versions. The popular combination of Tcl with the Tk extension is referred to as Tcl/Tk (pronounced "tickle teak" or "tickle TK") and enables building a graphical user interface (GUI) natively in Tcl. Tcl/Tk is included in the standard Python installation in the form of Tkinter.

Tcl (pronounced as an acronym, , like tickle, or as an initialism, , like T-C-L; originally Tool Command Language) is a high-level, general-purpose, interpreted, dynamic programming language. It was designed with the goal of being very simple but powerful. Tcl casts everything into the mold of a command, even programming constructs like variable assignment and procedure definition. Tcl supports multiple programming paradigms, including object-oriented, imperative, functional, and procedural styles. It is commonly embedded into C applications for rapid prototyping, scripted applications, GUIs, and testing. Tcl interpreters are available for many operating systems, allowing Tcl code to run on a wide variety of systems. Because Tcl is a very compact language, it is used on embedded systems platforms, both in its full form and in several other small-footprint versions. The popular combination of Tcl with the Tk extension is referred to as Tcl/Tk (pronounced "tickle teak" or "tickle TK") and enables building a graphical user interface (GUI) natively in Tcl. Tcl/Tk is included in the standard Python installation in the form of Tkinter. puts "Hello, World!" This sends the string "Hello, World!" to the standard output device along with an appended newline character. Variables and the results of other commands can be substituted into strings, such as in this example which uses the set and expr commands to store the result of a calculation in a variable (note that Tcl does not use = as an assignment operator), and then uses puts to print the result together with some explanatory text: expr evaluates text string as an expression set sum [expr 1+2+3+4+5] puts "The sum of the numbers 1..5 is $sum." The # character introduces a comment. Comments can appear anywhere the interpreter is expecting a command name. with curly braces, variable substitution is performed by expr set x 1 set sum [expr {$x + 2 + 3 + 4 + 5}]; # $x is not substituted before passing the parameter to expr; # expr substitutes 1 for $x while evaluating the expression puts "The sum of the numbers 1..5 is $sum."; # sum is 15 without curly braces, variable substitution occurs at the definition site (lexical scoping) set x 2 set op * set y 3 set res [expr $x$op$y]; # $x, $op, and $y are substituted, and the expression is evaluated to 6 puts "$x $op $y is $res."; # $x, $op, $y, and $res are substituted and evaluated as strings As seen in these examples, there is one basic construct in the language: the command. Quoting mechanisms and substitution rules determine how the arguments to each command are processed. One special substitution occurs before the parsing of any commands or arguments. If the final character on a line (i.e., immediately before a newline) is a backslash, then the backslash-newline combination (and any spaces or tabs immediately following the newline) are replaced by a single space. This provides a line continuation mechanism, whereby long lines in the source code can be wrapped to the next line for the convenience of readers. Continuing with normal argument processing, a word that begins with a double-quote character ( " ) extends to the next double-quote character. Such a word can thus contain whitespace and semicolons without those characters being interpreted as having any special meaning (i.e., they are treated as normal text characters). A word that begins with an opening curly-brace character ( { ) extends to the next closing curly-brace character ( } ). Inside curly braces all forms of substitution are suppressed except the previously mentioned backslash-newline elimination. Words not enclosed in either construct are known as bare words. In bare and double-quoted words, three types of substitution may occur: Command substitution replaces the contents of balanced square brackets with the result of evaluating the script contained inside. For example, [expr 1+2+3] is replaced by the result of evaluating the contained expression (in this case 6). Variable substitution replaces the name of a variable prefixed with a dollar sign with the contents (or value) of the variable. For example, $foo is replaced by the contents of the variable called "foo". The variable name may be surrounded by curly braces to separate it from subsequent text in otherwise ambiguous cases (e.g., ${foo}ing ). Backslash substitution replaces a backslash followed by a letter with another character. For example, \n is replaced by a newline. Substitution proceeds left-to-right in a single scan through each word. Any substituted text will not be scanned again for possible further substitutions. However, any number of substitutions can appear in a single word. From Tcl 8.5 onwards, any word may be prefixed by {*} , which causes the word to be split apart into its constituent sub-words for the purposes of building the command invocation (similar to the ,@ sequence of Lisp's quasiquote feature). As a consequence of these rules, the result of any command may be used as an argument to any other command. Note that, unlike in Unix command shells, Tcl does not reparse any string unless explicitly directed to do so, which makes interactive use more cumbersome, but scripted use more predictable (e.g., the presence of spaces in filenames does not cause difficulties). The single equality sign ( = ) serves no special role in the language at all. The double equality sign ( == ) is the test for equality which is used in expression contexts such as the expr command and in the first argument to if . (Both commands are part of the standard library; they have no special place in the library and can be replaced if desired.) The majority of Tcl commands, especially in the standard library , are variadic, and the proc (the constructor for scripted command procedures) allows one to define default values for unspecified arguments and a catch-all argument to allow the code to process arbitrary numbers of arguments. Tcl is not statically typed: each variable may contain integers, floats, strings, lists, command names, dictionaries, or any other value; values are reinterpreted (subject to syntactic constraints) as other types on demand. However, values are immutable and operations that appear to change them actually just return a new value instead. proc for {initCmd testExpr advanceCmd bodyScript} { uplevel 1 $initCmd set testCmd [list expr $testExpr] while {[uplevel 1 $testCmd]} { uplevel 1 $bodyScript uplevel 1 $advanceCmd } } proc decr {varName {decrement 1}} { upvar 1 $varName var incr var [expr {-$decrement}] } proc collatz {n} { puts $n if {$n > 1} { if {$n % 2 == 0} { tailcall collatz [expr {$n >> 1}] } else { tailcall collatz [expr {$n * 3 + 1}] } } } The tailcall command is not limited to the caller and can be used to invoke other functions as well. oo::class create fruit { method eat {} { puts "yummy!" } } oo::class create banana { superclass fruit constructor {} { my variable peeled set peeled 0 } method peel {} { my variable peeled set peeled 1 puts "skin now off" } method edible? {} { my variable peeled return $peeled } method eat {} { if {![my edible?]} { my peel } next } } set b [banana new] $b eat → prints "skin now off" and "yummy!" fruit destroy $b eat → error "unknown command" Tcl did not have object oriented (OO) syntax until 2012, so various extension packages emerged to enable object-oriented programming. They are widespread in existing Tcl source code. Popular extensions include: incr Tcl XOTcl Itk Snit STOOOP TclOO was not only added to build a strong object oriented system, but also to enable extension packages to build object oriented abstractions using it as a foundation. After the release of TclOO, incr Tcl was updated to use TclOO as its foundation.

Text derived from https://en.wikipedia.org/w/index.php?oldid=1366336528 under CC BY-SA 4.0; modifications have been made.

Sources cited in this revision

Extracted from the cited revision's reference blocks; absent citations are not invented.