Navigation
  • Home
  • Recent
  • Most Active
  • Popular
  • Blog
  • Credits
  • RSS
  •   Interaction
  • Register
  • Statistics
  •   Help
  • Suggestions
  • Contact Us
  • How to Edit
  • Help



  • [Edit]




    Tcl (originally from "Tool Command Language", but nonetheless conventionally rendered as "Tcl" rather than "TCL"; and pronounced like "tickle") is a scripting language created by John Ousterhout. Originally "born out of frustration" — according to the author — with programmers devising their own (poor quality) languages intended to be embedded into applications, Tcl quickly gained wide acceptance on its own and is generally thought to be easy to learn, but powerful in competent hands. It is most commonly used for rapid prototyping, scripted applications, GUIs and testing. Tcl is also used for CGI scripting.

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


        Tcl
            Features
                Very simple and consistent syntax
                    Symbols with a special meaning
                Some examples of commands
                Procedures
                Associative arrays
                Variable scope
            Extension packages
                Hello, world!
                Adding Numbers
                Echo server
                Digital clock
                List of content of associative array
                Intersection of two sets
                Factorial
            History
            See also
    NameTcl
    Paradigmmulti-paradigm programming language
    Year1988
    DesignerJohn Ousterhout
    DeveloperJohn Ousterhout
    Latest Release Version8.4
    Latest Release DateApril 19, 2006
    Typingdynamic typing, everything can be treated as ...
    ImplementationsActiveTcl

    top

    Features
    Tcl's features include:

      Everything can be dynamically redefined and overridden.
      Flexible scope, with variable visibility restricted to lexical (static) scope by default, but uplevel and upvar allowing procs to interact with the enclosing functions' scopes.
      Simple exception handling using exception code returned by all command executions.
      Interpreted language, code can be created and modified dynamically, but still fast due to compiling into bytecode.
      Full Unicode (3.1) support, first released 1999.
      Close integration with windowing (GUI) interface Tk.
      Easy to maintain code. Tcl scripts are often more compact and readable than functionally equivalent code in other languages.
      Can be used for many purposes, and in many environments: as a text-only scripted language, as a GUI-capable language for applications, as an embedded language in: web pages (server-side; or client-side, as Tclets), and databases (server-side, in PostgreSQL).
      Exists as development version (e. g. ActiveState Tcl), as tclkit (kind of runtime version, only about 1 megabyte in size), as starpack (single-file executable of a script/program), as BSD licensed freely distributable source

    Tcl did not originally support object oriented syntax, being a functional language, but recent versions do support extensions which provide OO functionality, such as the XOTcl extension to Tcl. Other OO extensions also exist, such as incr Tcl, Snit, and STOOOP (simple tcl-only object-oriented programming).

    Functional programming can easily be done in Tcl, as higher-order functions or functional abstractions are built into the language, though it is not widely used for this purpose. As an example, consider the ease with which two functions can be composed:
    proc o

    top

    Very simple and consistent syntax
    Tcl has a very simple syntax which is applied in a consistent way. A Tcl script consists of several commands. A command is a list of words separated by whitespace.

    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

    Any argument may be replaced by another command in square brackets. The subcommand is evaluated first and the result is substituted as the argument. Alternatively, any argument placed in curly braces will not be evaluated, but rather will be handed directly back to the original command as an argument.

    To summarize: there is one basic construct (the command) and a set of simple substitution rules, and only the square brackets, the curly braces, quotes, and the backslash have special meaning. 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 the expr command.

    All commands have the same structure: a keyword which is followed by several parameters. A command is terminated by a newline or a semicolon. Even comments are just commands which happen to do nothing.

    Tcl is not statically typed: each variable may contain integers, floats, strings, lists or any other value.

    top

    Symbols with a special meaning


    top

    Some examples of commands

    Assignments are made with the command set, no equality sign.

    set variable value


    'While' loops are implemented by the command while which takes two arguments. The first argument is a Tcl expression (expressions are written in what is essentially a miniature language for doing C-style math and comparison expressions). The second argument is the script to run on every iteration. They are generally put in curly braces to avoid immediate execution.

    while

    If command

    if

    Commands may have no arguments

    pwd

    gives back the current working directory. With

    set wdir pwd

    you store the string describing the working directory in the variable wdir.



    A command may give back as a result a list

    glob aPattern

    gives back a list of file names in the working directory whose names match aPattern.

    top

    Procedures

    Procedures are defined as follows

    proc nameOfProc

    top

    Associative arrays

    The following code snippet creates and initializes an associative array which in other languages is called a map, dictionary, or hash table.

    set capital(France) Paris
    set capital(Italy) Rome
    set capital(Germany) Berlin
    set capital(Poland) Warsaw
    set capital(Russia) Moscow
    set capital(Spain) Madrid

    To query it and put the result on standard output use

    puts $capital(Italy)

    To get a list of all countries for which a capital is defined use

    array names capital

    The result is unsorted because Tcl arrays are based on hash tables.

    Poland Spain Russia Germany Italy France

    If you like to have it sorted use

    lsort array names capital

    Note however that arrays are collections of variables, not first-class objects, and cannot be freely passed around the way strings are. The commands

    set CapitalCopy $capital

    and

    DoSomethingWith $capital

    both produce the error message

    can't read "capital": variable is array

    In general, note that Tcl has a different concept of references than some people might expect. To refer to an array (to pass it by reference), give its variable name:
    proc demo _arr

    To pass the contents of an array by value, use array get resp. array set. For example, to copy one array to another:

    array set Newarr array get Oldarr

    For pure-value associative arrays, Tcl introduced the dict type in version 8.5.

    top

    Variable scope
    For a more detailed explanation of variable scope in tcl, visit here.

    When referring to variables inside a procedure, the program will (by default) only recognize variables that were _also defined in that procedure_ . Another way of saying this is that the program will only recognize local variables by default.

    Here is an example program:

      ! /usr/local/bin/tclsh
    set gvar "123"
      this is a variable created in the global namespace
    proc nest1
    nest1
    puts "$lvar"

    On line 7, the command 'puts "$gvar"' will create an error because it is technically only referring to the variable with that name that is also within the local namespace. However, there is no variable "gvar" within that local namespace because gvar was not declared in the nest1 namespace.

    Also, on line 11, the command 'puts "$lvar"' will create an error because it is looking for a variable called lvar within the current namespace (which happens to be the global namespace). Since there is no lvar that was globally declared, an error will occur.

    This will fix the first problem:

    ...
    proc nest1
    nest1
    puts "$lvar"

    The global call specifically told the program to look for the subsequent variables in the global namespace. But this still doesn't fix the second problem. If you want to be able to recall the variable lvar from global scope, you need to declare it as being within the global scope. Here is how you could accomplish that:

    ...
    proc nest1
    ...

    By adding the namespace path separator ':
    ' without specifying a sub-namespace (for example ':
    IAmASubNamespace' ), you have made it clear to the program that you are actually declaring and defining a variable inside the global namespace.


    If you are doing something where speed really matters, you can find that relevent information at here.

    top

    Extension packages
    The Tcl language has always supported extension packages, which provide additional functionality (such as a GUI, terminal-based application automation, database access, etc.)

    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.


    A number of database extensions are available:
      tclodbc
      mk4tcl
      sqlite
      Pgtcl, pgintcl
      mysqltcl, msqltcl
      AdabasTcl
      FBSQL
      ibtcl
      Oratcl
      Sybtcl
      db2tcl

    top

    Hello, world!

    puts "Hello, world!"

    or

    puts

    top

    Adding Numbers

    Method (A) - Adding using a 'foreach' loop

    set numbers
    set result 0
    foreach number $numbers
    puts $result

    Method (B) - A much more elegant way of adding numbers using the 'join' command

    set numbers
    puts expr join $numbers + + 0

    top

    Echo server
    A simple working example, demonstrating event-based handling of a socket, follows.


      !/usr/bin/env tclsh

      echo server that can handle multiple
      simultaneous connections.

    proc newConnection


    proc handleData

      handle all connections to port given
      as argument when server was invoked
      by calling newConnection
    set port lindex $argv 0
    socket -server newConnection $port

      enter the event loop by waiting
      on a dummy variable that is otherwise
      unused.
    vwait forever


    top

    Digital clock
    Another example using Tk (from A simple A/D clock) and timer events, a digital clock in three lines of code:

    proc every
    pack label .clock -textvar time
    every 1000
      RS

    Explanation: the first line defines a command, "every", which re-schedules an action ('body') every 'ms' milliseconds; the second creates a label whose content is bound to the variable 'time'; the third line arranges so that the variable 'time' is updated to formatted local time every second.

    Note that the "every" command treats its first parameter as a number and its second parameter as a script, resulting in correct calls being those programs that pass in arguments that look like those pieces of syntax. However, the Tcl interpreter assigns no special syntactic interpretation to either argument, meaning that the code is free to interpret those arguments as desired. (By contrast, a language using a conventional BNF description could only achieve the equivalent functionality by defining "every" as a keyword, adding extra syntax to mark the executable argument, or by making the curly braces deeply magical indeed, possibly requiring a more complex declaration.)

    top

    List of content of associative array
    In array tcl_platform, platform-specific properties are kept.
    A list of the names of the properties is obtained by

    array names tcl_platform

    The following snippet lists them together with their values

    foreach i array names tcl_platform

    If the properties should be 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.

    top

    Intersection of two sets
    The filter procedure returns those elements of the list where the script returns TRUE:

    proc filter

    The in procedure is shorthand for list inclusion:

    proc in

    Testing:
    % filter
    b c

    top

    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.

    top

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



    (Need rest of potted history!)

    top

    See also

     
    Search more:
     

       
    Source Privacy License Download Contact Us Atlas
    Scientus.org Dictionary (Yet Another Wiki) RC : 1.39
    MIT OpenCourseWare
    This article is licensed under the GNU Free Documentation License [copyleft]. It uses material from the Wikipedia article "Tcl". link