Jump to content

Tcl

From Wikipedia, the free encyclopedia

This is an old revision of this page, as edited by 195.240.187.166 (talk) at 21:18, 29 April 2016 (procedures, procedure calls, and conditional statements are all adressed. Procedure calls are commands and command calls are explained). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.

Tcl
Tcl logo
Paradigmmulti-paradigm: object-oriented, functional, procedural, event-driven programming, imperative
Designed byJohn Ousterhout
DeveloperJohn Ousterhout, Tcl Core Team
First appeared1988; 36 years ago (1988)
Stable release
8.6.5[1] / February 29, 2016; 8 years ago (2016-02-29)
Typing disciplinedynamic typing, everything can be treated as a string
LicenseBSD-style
Filename extensions.tcl
Websitewww.tcl.tk
Major implementations
ActiveTcl
Influenced by
AWK, Lisp
Influenced
PHP,[2] Tea, PowerShell[3]

Tcl (originally from Tool Command Language, but conventionally spelled "Tcl" rather than "TCL"; pronounced as "tickle" or "tee-see-ell"[4]) is a scripting language created by John Ousterhout.[5] Originally "born out of frustration",[6] according to the author, with programmers devising their own languages intended to be embedded into applications, Tcl gained acceptance on its own. It is commonly used for rapid prototyping, scripted applications, GUIs and testing. Tcl is used on embedded systems platforms, both in its full form and in several other small-footprint versions.

The combination of Tcl and the Tk GUI toolkit is referred to as Tcl/Tk.

History

The Tcl programming language was created in the spring of 1988 by John Ousterhout while working at the University of California, Berkeley.[7]

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.
August 1997 Tcl 8.0 introduced a bytecode compiler.[8]
April 1999 Tcl 8.1 introduces full Unicode support.[9]
August 1999 Tcl 8.2 introduces Tcl Extension Architecture (TEA)[10]
August 2000 Tcl Core Team formed, moving Tcl to a more community-oriented development model.
September 2002 Ninth Tcl/Tk conference (Vancouver). Announcement of starkit packaging system. Tcl 8.4.0 released.[11]
December 2007 Tcl 8.5 added new datatypes, a new extension repository, bignums, lambdas.[12]
December 2012 Tcl 8.6 added built-in dynamic object system, TclOO, and stackless evaluation.[13]

Tcl conferences and workshops are held in both the United States and Europe.

Features

Tcl's features include

  • All operations are commands, including language structures. They are written in prefix notation.
  • Commands are commonly variadic.
  • Everything can be dynamically redefined and overridden. Actually, there are no keywords, so even control structures can be added or changed, although this is not advisable.
  • All data types can be manipulated as strings, including source code. Internally, variables have types like integer and double, but converting is purely automatic.
  • Variables are not declared, but assigned to. Use of a non-defined variable results in an error.
  • Fully dynamic, class-based object system, TclOO, including advanced features such as meta-classes, filters, and mixins.
  • Event-driven interface to sockets and files. Time-based and user-defined events are also possible.
  • Variable visibility restricted to lexical (static) scope by default, but uplevel and upvar allowing procs to interact with the enclosing functions' scopes.
  • All commands defined by Tcl itself generate error messages on incorrect usage.
  • Extensibility, via C, C++, Java, and Tcl.
  • Interpreted language using bytecode
  • Full Unicode (3.1) support, first released 1999.
  • Cross-platform: Windows API; Unix, Linux, Macintosh etc.
  • Close, cross-platform integration with windowing (GUI) interface Tk.
  • Multiple distribution mechanisms exist:
    • Full development version (e.g., ActiveState Tcl)
    • tclkit (kind of single-file runtime, only about 2 megabytes in size)
    • starpack (single-file executable of a script/program, derived from the tclkit technology)
    • freewrapTCLSH turns Tcl scripts into single-file binary executable programs.
    • BSD licenses, freely distributable source.

Tcl did not originally have object oriented (OO) syntax (8.6 provides an OO system in Tcl core), so OO functionality was provided by extension packages, such as incr Tcl and XOTcl. Even purely scripted OO packages exist, such as Snit and STOOOP (simple Tcl-only object-oriented programming).

Safe-Tcl is a subset of Tcl that has restricted features. File system access is limited and arbitrary system commands are prevented from execution. It uses a dual interpreter model with the untrusted interpreter running code in an untrusted script. It was designed by Nathaniel Borenstein and Marshall Rose to include active messages in e-mail. Safe-Tcl can be included in e-mail when the application/safe-tcl and multipart/enabled-mail are supported. The functionality of Safe-Tcl has since been incorporated as part of the standard Tcl/Tk releases.[14][15]

Syntax and fundamental semantics

The syntax and semantics are covered by the twelve rules of the dodecalogue[16] (alternative wording[17]).

A Tcl script consists of several command invocations. A command invocation is a list of words separated by whitespace and terminated by a newline or semicolon.

word0 word1 word2 ... wordN

The first word is the name of a command, which is not built into the language, but which is in the library. The following words are arguments. So we have:

commandName argument1 argument2 ... argumentN

An example, using the puts command to display a string on the host console, is:

puts "Hello, World!"

This sends the string "Hello, World!" to the 'stdout' device, with an appended newline character.

Variables and the results of other commands can be substituted inside strings too, such as in this example where we use set and expr (no assignment operator =) to store a calculation result in a variable, and puts (short for "put string") 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."
# with curly braces, variable substitution is delayed until call time (dynamic scoping)
set x 1
set sum [expr {$x + 2 + 3 + 4 + 5}] # $x is not substituted, and the expression is preserved
puts "The sum of the numbers 1..5 is $sum." # 1 is substituted for $x, and the expression is evaluated
# 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
puts "2 * 3 is $res." # 6 is substituted for $res

There is one basic construct (the command) and a set of simple substitution rules.

Formally, words are either written as-is, with double-quotes around them (allowing whitespace characters to be embedded), or with curly-brace characters around them, which suppresses all substitutions inside (except for backslash-newline elimination). In bare and double-quoted words, three types of substitution occur (once, in a single left-to-right scan through the word):

  • 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 with the result of evaluating the contained expression (i.e. 6) since that's what the expr command does.
  • Variable substitution replaces a dollar-sign followed by the name of a variable with the contents of the variable. For example, “$foo” is replaced with the contents of the variable called “foo”. The variable name may be surrounded in curly braces so as to delimit what is and isn't the variable name in otherwise ambiguous cases.
  • Backslash substitution replaces a backslash followed by a letter with another character. For example, “\n” is replaced with a newline.

From Tcl 8.5 onwards, any word may be prefixed by “{*}” to cause that 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. Also, there is no operator or command for string concatenation, as the language concatenates directly. 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 (=) for example is not used at all, and the double equality sign (==) is the test for equality, and even then only in expression contexts such as the expr command or the first argument to if. (Both of those commands are just part of the standard library; they have no particularly special place in the library and can be replaced, if so 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.

Basic commands

The most important commands that refer to program execution and data operations are:

  • set: writes a new value to a variable (creates a variable if did not exist). If used only with one argument, it returns the value of the given variable (it must exist in this case).
  • proc: defines a new command, which's execution results in executing a given Tcl script, written as a set of commands.

The usual execution control commands are:

  • if: executes given script body (second argument), if the condition (first argument) is satisfied. It can be followed by additional arguments starting from elseif with the alternative condition and body, or else with the complementary block.
  • while: repeats executing given script body, as long as the condition (first argument) remains satisfied
  • foreach: executes given body where the control variable is assigned list elements one by one.
  • for: shortcut for initializing the control variable, condition (as in while) and the additional "next iteration" statement (command executed after executing the body)

Those above looping commands can be additionally controlled by the following commands:

  • break: interrupts the body execution and returns from the looping command
  • continue: interrupts the body execution, but the control is still given back to the looping command. For while it means to loop again, for for and foreach, pick up the next iteration.

Additionally important commands:

  • return: interrupts the execution of the current body no matter how deep inside a procedure, until reaching the procedure boundary, and returns given value to the caller.
  • expr: passes the argument to a separate expression interpreter and returns the evaluated value. Note that the same interpreter is used also for "conditional" expression for if and looping commands.

Interfacing with other languages

Tcl interfaces natively with the C language. This is because it was originally written to be a framework for providing a syntactic front-end to commands written in C, and all commands in the language (including things that might otherwise be keywords, such as if or while) are implemented this way. Each command implementation function is passed an array of values that describe the (already substituted) arguments to the command, and is free to interpret those values as it sees fit.

Digital logic simulators often include a Tcl scripting interface for simulating Verilog, VHDL and SystemVerilog hardware languages.

Tools exist (e.g. SWIG, ffidl) to automatically generate the necessary code to connect arbitrary C functions and the Tcl runtime, and Critcl does the reverse, allowing embedding of arbitrary C code inside a Tcl script and compiling it at runtime into a DLL.

Module Files

Environment Modules are written in the Tcl (Tool Command Language) and are interpreted by the modulecmd program via the module[18] user interface.

  • Environment Modules provides a set of extensions to the "standard" Tcl package including setenv, unsetenv, append-path, prepend-path, set-alias and more as defined in the modulefiles man page[19] which, along with the built-in functionality of Tcl, provides a rich environment for handling setting defaults and initializing into an environment.

Extension packages

The Tcl language has always allowed for extension packages, which provide additional functionality, such as a GUI, terminal-based application automation, database access, and so on. Commonly used extensions include:

Tk
The most popular Tcl extension is the Tk toolkit, which provides a graphical user interface library for a variety of operating systems. Each GUI consists of one or more frames. Each frame has a layout manager.
Expect
One of the other very popular Tcl extensions is Expect extension. The early close relationship of Expect with Tcl is largely responsible for the popularity of Tcl in prolific areas of use such as in Unix testing, where Expect was (and still is today) employed very successfully to automate telnet, ssh, and serial sessions to perform many repetitive tasks (i.e., scripting of formerly interactive-only applications). Tcl was the only way to run Expect, so Tcl became very popular in these areas of industry.
Tile/Ttk
Tile/Ttk[20] is a styles and theming widget collection that can replace most of the widgets in Tk with variants that are truly platform native through calls to an operating system's API. Themes covered in this way are Windows XP, Windows Classic, Qt (that hooks into the X11 KDE environment libraries) and Aqua (Mac OS X). A theme can also be constructed without these calls using widget definitions supplemented with image pixmaps. Themes created this way include Classic Tk, Step, Alt/Revitalized, Plastik and Keramik. Under Tcl 8.4, this package is known as Tile, while in Tcl 8.5 it has been folded into the core distribution of Tk (as Ttk).
Tix
Tix, the Tk Interface eXtension, is a set of user interface components that expand the capabilities of Tcl/Tk and Python applications. It is an open source software package maintained by volunteers in the Tix Project Group and released under a BSD-style license.[21]
Itcl/IncrTcl
Itcl is an object system for Tcl, and is normally named as [incr Tcl] (that being the way to increment in Tcl, similar in fashion to the name C++).
Tcllib
Tcllib is a set of scripted packages for Tcl that can be used with no compilation steps.
Tklib
Tklib is a collection of utility modules for Tk, and a companion to Tcllib.
TclUDP
The TclUDP[22] extension provides a simple library to support User Datagram Protocol (UDP) sockets in Tcl.
Databases
Tcl Database Connectivity (TDBC), part of Tcl 8.6, is a common database access interface for Tcl scripts. It currently supports drivers for accessing MySQL, ODBC, PostgreSQL and SQLite databases. More are planned for the future. Access to databases is also supported through database-specific extensions, of which there are many available.[23]

See also

References

  1. ^ "Latest Release: Tcl/Tk 8.6.5 (Feb 29, 2016)". Tcl Developer Xchange. 2016-02-29. Retrieved 2016-02-29.
  2. ^ Lerdorf, Rasmus (2007-04-26). "PHP on Hormones – history of PHP presentation by Rasmus Lerdorf given at the MySQL Conference in Santa Clara, California". The Conversations Network. Retrieved 2009-12-11.
  3. ^ Windows PowerShell : PowerShell and WPF: WTF
  4. ^ From the Tcler's Wiki Tcl vs. TCL
  5. ^ John Ousterhout. "History of Tcl". Personal pages. Stanford University. Retrieved 2011-08-09.
  6. ^ From the inside flap of Tcl and the Tk Toolkit, ISBN 0-201-63337-X
  7. ^ History of Tcl
  8. ^ "Tcl/Tk 8.0". tcl.tk. Retrieved 2014-07-01.
  9. ^ "Tcl/Tk 8.1". tcl.tk. Retrieved 2014-07-01.
  10. ^ "Tcl/Tk 8.2 Release Announcement". tcl.tk. 1999-08-18. Retrieved 2014-07-01.
  11. ^ "Tcl/Tk 8.4". tcl.tk. 2013-06-01. Retrieved 2014-07-01.
  12. ^ "Tcl/Tk 8.5". tcl.tk. 2013-09-18. Retrieved 2014-07-01.
  13. ^ "Tcl/Tk 8.6". tcl.tk. 2013-09-20. Retrieved 2014-07-01.
  14. ^ Brown, Lawrie (September 18–20, 1996). "Mobile Code Security". Proceedings, 2nd Joint Conference, AUUG '96 and Asia-Pacific WWW '96. Melbourne, Australia. p. 50. Retrieved 2011-03-22. {{cite conference}}: Unknown parameter |booktitle= ignored (|book-title= suggested) (help); Unknown parameter |editors= ignored (|editor= suggested) (help)
  15. ^ Welch, Brent B.; Jones, Ken; Hobbs, Jeffrey (2003). Practical programming in Tcl and Tk. Vol. 1 (4th ed.). Prentice Hall PTR. p. 291. ISBN 0-13-038560-3.{{cite book}}: CS1 maint: multiple names: authors list (link)
  16. ^ "Tcl manual page - Tcl Built-In Commands". tcl.tk. Retrieved 2014-06-14.
  17. ^ "Dodekalogue". Wiki.tcl.tk. Retrieved 2014-06-14.
  18. ^ "module - command interface to the Modules package". man page. July 2009. Retrieved 9 February 2014.
  19. ^ "modulefile - files containing Tcl code for the Modules package". man page. July 2009. Retrieved 9 February 2014.
  20. ^ "TK Table Sourceforge Project". ActiveTcl. Retrieved August 7, 2012.
  21. ^ "Tix License". Sourceforge. Retrieved August 7, 2012.
  22. ^ "TCL UDP". Tcl'ers Wiki. Retrieved August 7, 2012.
  23. ^ "TDBC". Tcl'ers Wiki. Retrieved August 7, 2012.

Further reading