Easily to maintain code. Tcl scripts come typically more compact & clear than functionally tantamount code around other languages.
Tcl proper doesn't provide an object oriented framework, but it may be touch provide recently features every bit required. Several 100 extensions stand been written to give OO functionality, including a XOTcl and incr Tcl packages. More OO extensions, including Snit, are written fully inside Tcl.
Functional programming can easily exist when liquidate Tcl, as higher-sequentially functions or even functional abstractions come built into a language, though these are non widely utilized for this purpose. For instance, assume a ease by using which 2 functions may be composed:
proc o
Syntax
Very simple and consistent syntax
Tcl has the super elementary syntax which is applied around the uniform way. The Tcl script consists of many commands. The command occurs as names of words separated by whitespace.
word0 word1 word2 ... wordN
a number 1 word is the title of a command, which is non built into the language, however which is in the library. A as punishment words come arguments. Then i have:
commandName argument1 argument2 ... argumentN
Any argument can be replaced by a second command around square brackets. A subcomm& is evaluated number one and a effect is substituted when a argument. Or else, any argument positioned inside curly braces might non exist as evaluated, however like is handed directly back to the orginal command as an argument.
To summarize: there exists of these basic construct (a comm&) & a placed of elementary substitution system, & sole a prevent, a curly braces and a backslash have a favorite meaning besides the quotes. A only equality sign (=) e.g. is non utilized in a least, & the double equality sign (==) is the line 2 text for equality, & possibly so sole in the expr command.
Wholly commands use the equivalent structure, a keyword which is followed by many parameters. The command is terminated by the newline or even the semicolon. Potentially comments come good commands which happen to clean nothing.
Tcl is non statically typed: for even each one variable can contain whole number, floats, strings, lists or any more value.
Symbols with a special meaning
| word grouping while forgoing substitutions (e.g. is of these word, in which $user is non replaced)
|-
| \
| backslash substitution/escape (watch Tcl documentation for even fully names) or statement continuation (by default, statement stops using a line)
|-
| #
| comment (exclusively at the beginning of a statement)
|-
| ;
| statement separator
|}
Some examples of commands
Assignments come manufactured sustaining a command placed, there is no equality sign.
placed variable value
When loops come implemented per command patch which requires 2 arguments. A arguments come Tcl scripts. It is within curly braces to stay away from execution on the foremost level of interpretation. In a execution of the piece command a scripts come executed.
patch
Whenever command
whenever
Commands will develop there are no arguments
pwd
gives back a todays working directory. With
placed wdir [pwd]
that you store a string describing a working directory in the variable wdir.
The command might repay following the list
ball aPattern
gives back the listings of computer filename in the working directory whose list match aPattern.
Procedures
Procedures come defined when follows
proc nameOfProc
Associative arrays
A charted code snip creates & initializes an associative array which in more languages is known as the map, lexicon, or even hash table.
placed capital(France) Paris
placed capital(Italy) Rome
placed capital(Germany) Berlin
placed capital(Poland) Warsaw
placed capital(Russia) Moscow
placed capital(Spain) Madrid
To query it & put a effect in standard output use
puts $capital(Italy)
For the names of everthing countries for which the capital is defined use
array list capital
A symptom is unsorted because Tcl arrays come depending hash tables.
Poland Spain Russia Germany Italy France
If you such as to develop it sorted use
lsort [array names capital]
Note still that arrays come collections of variables, non first-class objects, and can't exist as freely passed in a area of the way strings come. A commands
placed CapitalCopy $capital
and
DoSomethingWith $capital
two create a error message
may't understand "capital": variable is array
Generally, note that Tcl has the different construct of references than some population will require. To refer to an array (to pass it by information), give its variable title:
proc demonstration _arr
To pass a contents of an array by value, utilise array develop resp. array placed. E.g., to copy a single array to a second:
array placed Newarr [array get Oldarr]
For pure-value associatory arrays, Tcl introduced a dict nature and severity inside version Octonary.Fivesome.
Extension Packages
the Tcl language has universally supported extension packages, which provide extra functionality (like a GUI, terminal-depending application automation, database access, etc.)
A virtually all popular Tcl extension is the Tk toolkit, which provides the graphical user interface library for a kind of operating systems. For even each 1 GUI consists of one or supplementary frames. To each one frame has the layout manager.
A second popular extension is Expect, which allows automated camping of terminal-depending computer program (like passwd, file transfer protocol, telnet & command caused husks).
The total of database extensions come available:
tclodbc
mk4tcl
sqlite
Pgtcl, pgintcl
mysqltcl, msqltcl
AdabasTcl
FBSQL
ibtcl
Oratcl
Sybtcl
db2tcl
& several, numerous others - see [http://wiki.tcl.tk/620 References to Category Database on Tcl'ers Wiki]
Examples
Hello, world!
puts "Hello, world!"
or
puts
Echo server
The elementary working case, demonstrating event-depending treating of the socket, follows.
!/bin/sh
next line restarts applying tclsh inside path \
white house tclsh $0 $
echo server that potty address multiple
concurrent modems.
proc newConnection
proc handleData
}
handgrip a lot modems to port given
while argument when server was invoked
by calling newConnection
placed port [ lindex $argv 0 ]
socket -server newConnection $port
enter a event loop by waiting
in the dummy up variable that is otherwise
unused.
vwait forever
Digital clock
Another example applying Tk (from either [http://mini.net/tcl/2563.html A simple A/D clock]) & timer cases, the digital punch in leash lines of code:
proc each
pack [label .clock -textvar time]
each G ;# RS
Explanation: the number one line defines a command, "every", which re-schedules an action ('person') each 'ms' msec; the 2nd creates a label whose content is attached to the variable 'period'; a third line arranges and so that a variable 'period' is updated to formatted local instance each 2nd.
Note that a "every" comm& treats its foremost parameter as a total and its 2nd parameter as a script, consequent witharound right calls existence people software online that pass in arguments that look prefer people pieces of syntax. Nevertheless, a Tcl interpreter assigns there are no favorite syntactical interpretation to either argument, meaning that a code is loose to interpret victims arguments when desired. (By direct contrast, a language utilizing the conventional BNF description can merely achieve the tantamount functionality by shaping "every" as the keyword, adding extra syntax to mark a viable argument, or even by making a curly braces deeply sorcerous indeed, even requiring a more complex declaration.)
List of content of associative array
Within an array tcl_platform, platform-specific properties come saved.
The list of the names of the properties is found by
array list tcl_platform
A ensuing snip lists the two together by owning their values
foreach we [array names tcl_platform]
In case a properties should become sorted
foreach i [lsort [array names tcl_platform]]
This demonstrates how commands may be nested. In fact they may be nested to any depth. If you want it fancier (keys padded with blanks so that the equal signs align) just use the proc parray that comes delivered with Tcl.
Intersection of two sets
The filter procedure returns those elements of the list where the script returns TRUE:
proc filter
foreach e $list
set res
}
The in procedure is shorthand for list inclusion:
proc in
Testing:
% filter
b c
Factorial
proc ! x
This demonstrates that any string can be a command name, and the ?: operator as from C is available in Tcl expressions too. Also, recursion is easily possible, although Tcl has no tail call optimisation so the maximum depth of recursion is restricted.
History
The Tcl programming language was created in the spring of 1988 by John Ousterhout while working at the University of California at Berkeley.
|+ Timeline
! Date || Event
|-
| January 1990 || Tcl announced beyond Berkeley (Winter USENIX).
|-
| June 1990 || Expect announced (Summer USENIX).
|-
| January 1991 || First announcement of Tk (Winter USENIX).
|-
| June 1993 || First Tcl/Tk conference (Berkeley). [table] geometry manager (forerunner of [grid]), [incr Tcl], TclDP and Groupkit, announced there.
|}
(Need rest of potted history!)
=