Talk:Tcl/Archives/2016/October
these are my notes, if you approove I merge them into the article or replace them by
[edit]- CONCLUSION
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.
- INTRO
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.
- COMMAND SYNTAX
- comment, but only at line beginning
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
- COMMAND TYPES
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.
- VARIABLES
set pi 3.14
- assignment, the command returns the new value of the variable
set pi
- read value, the command returns the value of the variable
expr $pi*3
- ??? isn't this unix specific, expr is in coreutils (NO! expr is builtin of Tcl as it is of bash!! NOTHING of core Tcl is Unix-specific!!)
set calculate expr
- ..., since everything is just a string ...
$calculate 562 / $pi
- results in 178.98089171974522
- COMMAND SUBST
set area [expr $pi * $pi * 5]
- area is now 49.298
- QUOTES AND BRACES
set x 1 set y 1 set mathLesson "$x + $y is [expr $x + $y]"
- result is "1 + 1 is 2"
set mathLesson {$x + $y is [expr $x + $y]}
- result is "$x + $y is [expr $x + $y]", because no subst btw {}
- CONTROL STRS
proc power {base p} {
set result 1 while {$p > 0} { set result [expr $result * $base] set p [expr $p - 1] } return $result
}
- single command "proc" with 3 args: procedurname, list of argument names, body of procedure which is a tcl script
- contents of {} are a single argument
- the "while" command takes an expression similarly as in C, which if is nonzero ,the body which is another tcl script is executed recursively
- STRINGS
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
- Replaces characters in string based on the key-value pairs in charMap.
- LISTS
set fruits [list apple pear peach banana mellon]
- command substitution with [] results in a
set yourFavouriteFruit [lindex $fruits 2]
- but it is just a string so the following results in "fourth"
lindex "zeroth first second third fourth fifth sixth" 4 foreach fruit $fruits { puts "yummy, ${fruit}s" }
- ASSOCIATIVE ARRAYS
- are not strings, they are actually another datatype than strings, breaking the "everything is a string" notion to an extent, because array elements are strings again.
% 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
- and indeed they are not strings !
- just the individual elements are strings, since neither "set" nor "puts" can read them
- note that "set" takes plain variablename, while "puts" would need the substituted variable otherwise just literally prints that word
foreach city [array names temperature] { puts "$city $temperature($city)" }
- above uses the "array" command taking as 1st arg "names", others: "exists", "get", "size", etc
- multidimensional example: matrix, where keys are in the form "row,column"
for {set i 1} {$i<=3} {incr i} { for {set j 1} {$j<=5} {incr j} { set matrix($i,$j) [expr {$i * $j}] } }
- print the indexes:
array names matrix
- print our whole multiplication "table"
array get matrix
- OTHER FEATURES
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
- !/usr/bin/tclsh
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)