Tcl (pronounced /tí.quel/, originated from the English acronym Tool Command Language or language of command tools, currently written as “Tcl” instead of “TCL”), is a scripting language created by John Ousterhout, which It has been designed with a simple syntax to facilitate learning, without compromising functionality and expressiveness. It is mainly used for rapid development of prototypes, “script” applications, graphical interfaces and tests. The combination of Tcl with Tk (from English Tool Kit) is known as Tcl / Tk, and is used for the creation of graphical interfaces.
Summary
[ hide ]
- 1 Features
- 2 Background
- 3 Fundamental syntax and semantics
- 1 Grouping
- 2 Types of variables
- 3 Control structures
- 4 Examples
- 5 Development and execution
- 6 Object-oriented programming
- 7 Sources
characteristics
Tcl is an interpreted language , and its code can be created and modified dynamically. Its syntactic rules are extremely simple and it has dynamic scope rules. It allows you to write code that is easy to maintain. Tcl scripts are often more compact and readable than functionally equivalent programs in other programming languages . It is a multiplatform language, with interpreters that run on Windows , Linux , UNIX, MacOS and OSX and even PIC microprocessors. All elements of a program are commands, including language structures. These commands are written inPolish notation and can be dynamically redefined or overwritten. A notable feature is that the data is handled as Unicode character strings , including the source code , supporting Unicode since the release of version 8.1, in 1999 . One of the most used features of Tcl is its extensibility. For example, if an application requires some functionality not offered by the standard Tcl, the new Tcl commands can be implemented using the C language , an extremely easy integrator. Tcl is “extensible” through C , C ++, and Java. Through an extension, it enables object-oriented programming. It can also be extended to graphical environments , through an interface called Tk . Event-oriented programming is performed on sockets and files, in addition, time-based and user-defined events are possible.
Background
The Tcl language was originally intended to be a languagereusable command key. The developers of Tcl were creating a series of interactive tools, each consisting of its own command language. Since they began to be more interested in these tools than in the command languages they would use, these languages began to be built quickly without considering the proper design, without much importance. After implementing several scripting languages created in this way and experiencing problems with each of them, they decided to focus their attention on implementing one overarching goal: an efficient scripting language that could be easily integrated into new applications. It is in this way that the Tcl language (Tool Command Language) was born, whose acronym in English means Command Tools Language. Since then, the Tcl language has been used as a code language. In many cases, Tcl is used in combination with the libraryTk (“Tool Kit”), a set of commands and procedures that make it relatively easy to program graphical user interfaces . Since Tcl began to be an easily extensible language, many extensions have been written for specific tasks, and they are generally freely available on the Internet.
Fundamental syntax and semantics
A Tcl script consists of several command invocations. An invocation of a command is a list of words separated by a space and terminated in a newline character or a semicolon.
word0 word1 word2 … wordN
The first word is the name of a command, which is not in the language, but in a library. The next words are the arguments. So you have:
commandName argument1 argument2 … argumentN
A practical example, using the puts command which sends a string of characters to an output (adding a new line at the end) by default on the stdout channel:
puts “Hello world!”
Variables and the results of other commands can also be entered into character strings, as in this example where we use set (variable assignment command) and expr (arithmetic expression command) to store the result of a calculation in a variable, and puts to print the result along with explanatory text:
set sum [expr {1 + 2 + 3 + 4 + 5}]; # assigns to the sum variable the result of the evaluation of the arithmetic expression 1 + 2 + 3 + 4 + 5 puts “The sum of the numbers 1. .5 is $ sum. ” ; # displays a string of characters with the result stored in the variable sum
Semicolon ; It is used to put two or more commands on the same line, or to add a comment after a command (a comment begins with the # symbol):
- Comment on a line without commands
puts “Hello world!” ; puts “another line of characters” set i 0; #Comment, with a ‘;’ to separate it from the command
Grouping
The ways to group elements in Tcl are:
- With “” (blocks between double quotes) strings are represented with the addition that the value of variables can be interleaved (substitutes the content of the variable) using the symbol $ followed by the name of the variable.
- With {} (blocks between braces) they can contain strings, but variables cannot be interleaved as in the case of double quotes, for example, in a tcl console (invoking tclsh in a terminal):
% set number 545; # Assignment to the variable number with 545 545% puts “The value is $ number”; # With double quotes we can put the content of the variable number The value is 545% puts {The value is $ number}; # But between braces it shows the $ symbol followed by the name of the variable The value is $ number The blocks between braces also serve for control structures, since they are treated like any other command.
- The () (parentheses) are used to enclose the indexes in the associative tables, or to group in arithmetic expressions.
- With [] (blocks between brackets) the substitution of the content of a result is performed, for example, in a tcl console:
% puts [expr {1 + [set u 342]}]; # Displays the result of the expression of adding 1 to the variable u that is assigned before 342 343% puts $ u; # Displays the content of u 342
Types of variables
Variables are not explicitly typed. However we can define six types
- Boolean: 0 / no / off (in case of false) and 1 / yes / on / (in case of true) or a number other than 0
- Numbers: 1 0 3.14 1.e-10, are treated as strings, except in arithmetic expressions where it becomes binary first.
- Strings: Welcome, “Madame De Labas”, 32, {z 3.14}
- Lists: Boolean, names, strings or lists (being able to nest lists)
- Dictionaries: They are key-value pairs, similar to associative tables. They can contain any type of data in the value. This type of variables is included from Tcl 8.5 onwards.
- Associative tables: T (key) M (2,3) that can contain the 5 previous types, even mixed.
The first five types are called scalar types, so they can be passed by value and returned when calling a procedure. The scalar types are automatically converted to the proper type, for example, a string can be a list as seen below using a tcl console:% set apricot fruits% puts $ apricot fruits% set fruits “grapefruit orange banana” grapefruit orange banana% puts [lindex $ fruit end] banana% puts [lindex $ fruit 1] orange A function can return a string or a list, but not an associative table since it is not a scalar variable. These can be passed on by reference. Dictionaries can be used as a parameter to a procedure, and can even be returned.
Control structures
Contrary to what happens with other programming languages, the words used for the control structures in Tcl are not reserved words with a special syntax. “for”, “if”, “while” are procedures that follow the usual Tcl syntax.
while {aLogicalExpression} {
aCommando
aCommando
….
}
if {$ x <0} {
set x 0
} elseif {$ x <5} {
set x 5
} else {
set x 10
}
for {set i 0} {$ i <10} {incr i} {
puts $ i
}
foreach i [list “youpii” “aujourd’hui” “il fait beau” “bravo!” “bravo!”] {
puts $ i
}
# it is not necessary to use the list command with fixed values; this is equivalent
foreach i {“youpii” “aujourd’hui” “il fait beau” “bravo!” “bravo!”} {
puts $ i
}
foreach a $ listA b $ listB c $ listC {
puts “$ a $ b $ c”
}
foreach {xy} [list Mr Gaston Mrs Jeanne Mr Robert Mrs Raymonde] {
puts “$ x $ y”
}
blue color set
switch $ color {
red {puts “FF0000”}
blue {puts “0000FF”}
green {puts “00FF00”}
white {puts “FFFFFF”}
black {puts “000000”}
default {puts “unknown”}
}
Examples
- Factorial ( functional programmingstyle ):
proc! x {expr {$ x <2? 1: $ x * [! [incr x -1]]}}
- Adding a list of numbers
With a foreach loop that follows the list: set numbers {10 9 8 7 6 5 4 3 2 1} set sum 0 foreach i $ numbers {
set sum [expr {$ sum + $ i}]
} Or more compact using the join command instead of the foreach loop: set sum [expr [join $ numbers +]]
- An operator is a character like any other, so it can be stored in a variable:
foreach opr {+ * – /} {
set result [expr [join $ numbers $ opr]]
puts “The result for the $ opr operator is $ result”
}
- Download an HTML page
package require http set tok [http :: geturl http://mini.net/tcl/540 ] puts [http :: data $ tok] http :: cleanup $ tok
Development and execution
Eclipse running a Tcl program that uses the incr Tcl extension for object-oriented programming. On Linux and other Unix-derived platforms, the Tcl / Tk interpreter is often installed by default. If this is not the case, it is usually necessary to install the packages corresponding to the distribution or else use the Tcl source code and compile it and then proceed with its installation. On the other hand, the execution of a Tcl code is carried out by typing in the command interpreter or console: tclsh file.tcl A Tcl console can also be obtained by writing only tclsh in it. There are other consoles for Tcl, such as TkCon, which is an improved interpreter written by Jeff Hobbs. Tcl / Tk programming is supported by a wide variety of IDEs , such asEclipse through the DLTK (Dynamic Languages Toolkit) project, which contains plugins for compilers of dynamic languages Tcl, Ruby, Javascript and Python.6
Object-oriented programming
Tcl does not have specific constructs in its syntax to support the object- oriented programming (OOP) paradigm . The inclusion of OOP in Tcl is a recurring topic of discussion within the community of users of this language. Opinions are divided because OOP is not the only adaptable paradigm, because there is a desire to keep TCL compact and because of the great variety of possible adaptations to object concepts. For this reason there are several different extensions that implement OOP.
