for short and medium-size tasks and whole applications consisting of many source files it might be the most convenient -> scripting language the main competitor for very small tasks may be: bash is on every unix-like system, which on the other hand lacks a GUI system (like Tk) so it's a very helpful approach as opposed to languages with more difficult syntax (Perl). The flexible syntax can be very much be abused, but also very productive.
In the beginning Tcl was not designed to be a serious programming language. It was designed to be a "scripting language" or "glue language", on the assumption that a "scripting language" need not try to be a real programming language.
But over time Tcl has developed the capabilities of one. It has various kinds of data structures like (associative) arrays, native lists and dictionaries. It's not strongly typed, but has integers and double numbers (as opposed to JavaScript having only doubles) Tcl is ok for writing small and large programs, because even large programs can be very well structured using namespaces and modules. Tcl has a syntax which is a bit special, its genius of simplicity is not apparent at first sight. Therefore Tcl syntax in the beginning seems a bit strange to most users. The closes syntax to Tcl is the one of bash, it differs a lot from C, Java and other languages, especially because it doesn't use pointers at all and passes parameters always by value, using a "call by name" mechanism as a substitute for references. Tcl also wasn't designed to be used in a vacuum, but to be embedded into a larger application. The ease with which this can be done is its primary strength. If the language lacks features you desire, you can easily add them. This, too, is relatively easy to do.
ToolCommandLanguage script is a string containing one or more commands everything is a string, code is seen as data, -> extremely weakly typed all expressions have a command syntax -> unit of interpretation is a command but since version 8.? associative arrays have been added which are not strings, but an actual second datatype advantages: simplest syntax (which can be easily extended) strong internationalization support: everything is a Unicode string BSD license allows closed-source use simple to use networking further advantages: substitutions, $varname, weak typing (can be a disadvantage to some) It is commonly used for rapid prototyping, scripted full-blown applications, GUIs, testing and as a language for the web. 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 Tcl command consists of one or more words separated by spaces. The first word is the name of a command and the other words are arguments to that command. command evaluation: break into space separated tokens/words, substitutions, 1st becomes command, rest args newline or ; terminates unless quoted, also: ] terminates if command substitution is done if a word starts with " then the word ends at the next " space,tab,newline,semicolon,closebrackets are included command, variable and backslash substitutions are performed the double quotes are not retained as part of the word. if word starts with “{*}” followed by non-whitespace character the leading “{*}” is removed the rest of the word is parsed and substituted as any other word after substitution, the word is parsed again without substitutions ? whats that good for ? if a word starts with { then the word ends with } can be escaped with \ no substitutions performed inside, except backslash-newline command substitution inside []s recursively invoking the interpreter on the contents withing []s substituting its output variable substitution if word starts with $ $name $name(index) ${name} backslash substitution \" \] \\ \$ allow " ] \ $ to be included in words also some special ones: \a bell, \b backspace, \n newline, \t tab, etc \ooo the unicode character given in octal instead of ooo \xhh the unicode character given in hexa instead of hh \uhhhh the unicode character given in hexa instead of hhhh during substitution the entire value of the variable becomes part of a single word even if the variable's value contains spaces
1 builtin (it runs commands installed to system, just like a shell) 2 extension: c/c++ procedure 3 procedures created with "proc" command When Tcl is used inside an application, the application incorporates its key features into Tcl using the extension mechanism. Other extensions provide object-oriented programming, database access, more graphical capabilities, etc. One of Tcl's greatest advantages for building integration applications is the ease with which it can be extended to incorporate new features or communicate with other resources.
set pi 3.14
set pi
expr $pi*3
set calculate expr
$calculate 562 / $pi
set area [expr $pi * $pi * 5]
set x 1 set y 1 set mathLesson "$x + $y is [expr $x + $y]"
set mathLesson {$x + $y is [expr $x + $y]}
proc power {base p} {
set result 1 while {$p > 0} { set result [expr $result * $base] set p [expr $p - 1] } return $result
}
everything is a string, ie: fundamental datatype/structure, can contain any type of byte, dynamically allocated other data structures built ontop: proc, dict, handle, ... an example string operation: % string map {abc 1 ab 2 a 3 1 0} "1abcaababcabababc" 01321221
set fruits [list apple pear peach banana mellon]
set yourFavouriteFruit [lindex $fruits 2]
lindex "zeroth first second third fourth fifth sixth" 4 foreach fruit $fruits { puts "yummy, ${fruit}s" }
% set temperature(Rome) 31 31 % set temperature(Zurich) 26 26 % set temperature(Munich) 22 22 % set temperature(Oslo) 19 19 % set temperature can't read "temperature": variable is array % puts $temperature can't read "temperature": variable is array
foreach city [array names temperature] { puts "$city $temperature($city)" }
for {set i 1} {$i<=3} {incr i} { for {set j 1} {$j<=5} {incr j} { set matrix($i,$j) [expr {$i * $j}] } }
array names matrix
array get matrix
if, for, foreach, switch I/O, including files on disk, network sockets, and devices such as serial ports file management Events: Tcl allows scripts to wait for certain events to occur, such as an elapsed time or the availability of input data on a network socket. This has been very much hyped in the case of JavaScript, although Tcl had it many years before
proc multilineComment {commentInBraces} { return }
multilineComment {
# fileHandle should refer to this file set fileHandle [open practice.tcl] set wholeContentOfFile [read $fileHandle] # print the 3rd token puts [lindex $wholeContentOfFile 3]
proc forEachCharIn {localFileHandle callback} {
set char [read $localFileHandle 1]
if {$char == ""} return
$callback $char
forEachCharIn $localFileHandle $callback
}
set fileHandle [open practice.tcl]
forEachCharIn $fileHandle puts
}
set fileHandle [open playground.tcl] set num 0 while {$num < 23} { incr num puts [read $fileHandle 1] }
— Preceding unsigned comment added by 46.249.133.113 ( talk) 28 August 2013 (UTC) and modified significantly by 217.50.160.156 ( talk) 5 September 2015 (UTC)
for short and medium-size tasks and whole applications consisting of many source files it might be the most convenient -> scripting language the main competitor for very small tasks may be: bash is on every unix-like system, which on the other hand lacks a GUI system (like Tk) so it's a very helpful approach as opposed to languages with more difficult syntax (Perl). The flexible syntax can be very much be abused, but also very productive.
In the beginning Tcl was not designed to be a serious programming language. It was designed to be a "scripting language" or "glue language", on the assumption that a "scripting language" need not try to be a real programming language.
But over time Tcl has developed the capabilities of one. It has various kinds of data structures like (associative) arrays, native lists and dictionaries. It's not strongly typed, but has integers and double numbers (as opposed to JavaScript having only doubles) Tcl is ok for writing small and large programs, because even large programs can be very well structured using namespaces and modules. Tcl has a syntax which is a bit special, its genius of simplicity is not apparent at first sight. Therefore Tcl syntax in the beginning seems a bit strange to most users. The closes syntax to Tcl is the one of bash, it differs a lot from C, Java and other languages, especially because it doesn't use pointers at all and passes parameters always by value, using a "call by name" mechanism as a substitute for references. Tcl also wasn't designed to be used in a vacuum, but to be embedded into a larger application. The ease with which this can be done is its primary strength. If the language lacks features you desire, you can easily add them. This, too, is relatively easy to do.
ToolCommandLanguage script is a string containing one or more commands everything is a string, code is seen as data, -> extremely weakly typed all expressions have a command syntax -> unit of interpretation is a command but since version 8.? associative arrays have been added which are not strings, but an actual second datatype advantages: simplest syntax (which can be easily extended) strong internationalization support: everything is a Unicode string BSD license allows closed-source use simple to use networking further advantages: substitutions, $varname, weak typing (can be a disadvantage to some) It is commonly used for rapid prototyping, scripted full-blown applications, GUIs, testing and as a language for the web. 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 Tcl command consists of one or more words separated by spaces. The first word is the name of a command and the other words are arguments to that command. command evaluation: break into space separated tokens/words, substitutions, 1st becomes command, rest args newline or ; terminates unless quoted, also: ] terminates if command substitution is done if a word starts with " then the word ends at the next " space,tab,newline,semicolon,closebrackets are included command, variable and backslash substitutions are performed the double quotes are not retained as part of the word. if word starts with “{*}” followed by non-whitespace character the leading “{*}” is removed the rest of the word is parsed and substituted as any other word after substitution, the word is parsed again without substitutions ? whats that good for ? if a word starts with { then the word ends with } can be escaped with \ no substitutions performed inside, except backslash-newline command substitution inside []s recursively invoking the interpreter on the contents withing []s substituting its output variable substitution if word starts with $ $name $name(index) ${name} backslash substitution \" \] \\ \$ allow " ] \ $ to be included in words also some special ones: \a bell, \b backspace, \n newline, \t tab, etc \ooo the unicode character given in octal instead of ooo \xhh the unicode character given in hexa instead of hh \uhhhh the unicode character given in hexa instead of hhhh during substitution the entire value of the variable becomes part of a single word even if the variable's value contains spaces
1 builtin (it runs commands installed to system, just like a shell) 2 extension: c/c++ procedure 3 procedures created with "proc" command When Tcl is used inside an application, the application incorporates its key features into Tcl using the extension mechanism. Other extensions provide object-oriented programming, database access, more graphical capabilities, etc. One of Tcl's greatest advantages for building integration applications is the ease with which it can be extended to incorporate new features or communicate with other resources.
set pi 3.14
set pi
expr $pi*3
set calculate expr
$calculate 562 / $pi
set area [expr $pi * $pi * 5]
set x 1 set y 1 set mathLesson "$x + $y is [expr $x + $y]"
set mathLesson {$x + $y is [expr $x + $y]}
proc power {base p} {
set result 1 while {$p > 0} { set result [expr $result * $base] set p [expr $p - 1] } return $result
}
everything is a string, ie: fundamental datatype/structure, can contain any type of byte, dynamically allocated other data structures built ontop: proc, dict, handle, ... an example string operation: % string map {abc 1 ab 2 a 3 1 0} "1abcaababcabababc" 01321221
set fruits [list apple pear peach banana mellon]
set yourFavouriteFruit [lindex $fruits 2]
lindex "zeroth first second third fourth fifth sixth" 4 foreach fruit $fruits { puts "yummy, ${fruit}s" }
% set temperature(Rome) 31 31 % set temperature(Zurich) 26 26 % set temperature(Munich) 22 22 % set temperature(Oslo) 19 19 % set temperature can't read "temperature": variable is array % puts $temperature can't read "temperature": variable is array
foreach city [array names temperature] { puts "$city $temperature($city)" }
for {set i 1} {$i<=3} {incr i} { for {set j 1} {$j<=5} {incr j} { set matrix($i,$j) [expr {$i * $j}] } }
array names matrix
array get matrix
if, for, foreach, switch I/O, including files on disk, network sockets, and devices such as serial ports file management Events: Tcl allows scripts to wait for certain events to occur, such as an elapsed time or the availability of input data on a network socket. This has been very much hyped in the case of JavaScript, although Tcl had it many years before
proc multilineComment {commentInBraces} { return }
multilineComment {
# fileHandle should refer to this file set fileHandle [open practice.tcl] set wholeContentOfFile [read $fileHandle] # print the 3rd token puts [lindex $wholeContentOfFile 3]
proc forEachCharIn {localFileHandle callback} {
set char [read $localFileHandle 1]
if {$char == ""} return
$callback $char
forEachCharIn $localFileHandle $callback
}
set fileHandle [open practice.tcl]
forEachCharIn $fileHandle puts
}
set fileHandle [open playground.tcl] set num 0 while {$num < 23} { incr num puts [read $fileHandle 1] }
— Preceding unsigned comment added by 46.249.133.113 ( talk) 28 August 2013 (UTC) and modified significantly by 217.50.160.156 ( talk) 5 September 2015 (UTC)