Sie sind auf Seite 1von 8

i.

Logo of the programming language

Why Tcl? Because it's a simple and general programming language, that can be used with
success to develop applications in short time. Tcl is a programmable programming language, as you will
discover later. There are few ideas inside, that it's possible to combine to create programs, and to extend
the language in itself in order to attack programming problems in a very direct way.

ii. History

x The Tcl programming language was created in the spring of 1988 by John Ousterhout while
working at the University of California, Berkeley.
x Tcl was created by John Ousterhout around 1988 as an embeddable command language. Today
Ousterhout is no longer actively developing the language, and the evolution of Tcl is in the hands
of the Tcl Core Team, elected to direct the development of the Tcl Core in the summer of 2000,
and the rest of the Tcl comunity.

caradigm
multi-paradigm: object-oriented, functional, procedural, event-driven programming

Appeared in
1988

Designed by
John Ousterhout

Developer
John Ousterhout

Latest release
v8.5. 7 2009-04-15; 4 months ago

yping discipline
dynamic typing, everything can be treated as a string

Major implementations ActiveTcl

c 

Dnfluenced
PowerShell[1]
Ô

Windows, Linux, Mac OS X, Solaris, AIX and HP-UX.

Liscence
Tcl/Tk is open source (based on a BSD-style license), so you can use it and modify it virtually
any way you want, including for commercial uses.

Website
http://www.tcl.tk

iii. 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.
 All data types can be manipulated as strings, including code.
 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.
 extensible, via C, C++, Java, and Tcl.
 Interpreted language using bytecode
 Full Unicode (3.1) support, first released 1999.
 Platform independent: Win32, UNIX, Linux, Mac, etc.
 Close 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 1 megabyte in size)
„ starpack (single-file executable of a script/program, derived from the tclkit technology)
„ BSD-licensed freely distributable source.
Tcl did not originally support object oriented syntax before 8.6 (8.6 provides a 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).

c 

iv. yntax

crogram structure

There is some other information you should know about the basic structure of a Tcl program. First of all,
that different commands in a program are separeted with newlines, so you can edit a text file, and write
this program:

puts "Hello World"


puts "Ciao Mondo"

Save it as hello.tcl, and execute it. If you are a unix user, you may call it from the unix shell as:

tclsh hello.tcl

and see the output. The other thing to know, is that commands can be also separated with a ";", in order
to make it possible to write more commands in a single line. So the following program is perfectly
equivalent to the previous:

puts "Hello World"; puts "Ciao Mondo"

In the next chapters we will write more complex programs that are uncomfortable to type directly in the
àclsh, so you may like to type the code into a file, and run the program passing the file name as argument
to àclsh.

Aariable,command and backslash substitutions

State is maintained in Tcl through the use of variables, and are both declared and instantiated with the
"set" command.

example:

set loopCounter 0

Note that a separate variable declaration is not necessary. Variable values are retrieved using the dollar
sign, which will be familiar to shell programmers and Perl developers, but there are slightly different rules
associated with when and when not to use the dollar sign.

example:

if

($loopCounter > 10) {

c 

do something

$ variable substitution: variables are not declared -- they are all of type string of arbitrary length.
Variable substitution can occur anywhere in a word (e.g. button .b$num).

[ ] command substitution: everything inside the brackets is evaluated as a Tcl script.

\ backslash substitution: escape for $, newline, quotes, etc.

EAERYHD D A RD

In Tcl every kind of data is represented by strings: numbers, lists, code, dictionaries. The user should not
care to provide a command with the right data type: "2" is the same as 2, or { 2} for Tcl: it's the number
two if a given procedure what to interpret it this way, or may be just a string for another procedure, so
arguments of commands are always just strings that the commands will interpret in some way.

For instance, to compute mathematical expressions in Tcl there is a command called expr, that gets a
mathematical expression as argument and returns the value of the expression.

% expr 20+30
50
%

but I may also write this code:

% set a "2"; set b "0+30"; expr $a$b


50
%

It looks strange no? Well, but actually it's logical. The Tcl interpreter will substitute $a and $b with "2" and
"0+30", and then call expr with the argument "20+30". The expr command accepts more than one
argument so that the mathematical expressions can be mixed with spaces:

% expr 1 + 2 * 40
81

Expr is also able to compute boolean experssions:

% set a 5; set b 2
2
% expr $a > $b
1

It returns 1 because 5 > 2. Like in C, expr's logical expressions will return 1 for true, and 0 for false.

You may wonder why math in Tcl is not accomplished using a command for every mathematical operand,
like a command for +, one for -, and so on, like this:

%+12
3

And this:

c 

% + 1 [* 2 40]
81

Just for the user convenience. Personally I prefer this way, but the Tcl designers introduced expr
because most people write math expressions in the infix notation at school. And actually for complex
math expressions it's quite comfortable. Still there is a proposal to add this commands to Tcl. But of
course we can define our own procedures in Tcl, so let's show how to create a + command if we like it.

Df else and switch

if - Execute scripts conditionally

? expr1 ?? body1 ? expr2 ?? body2 ? ... ?? ?bodyN?

The ? command evaluates expr1 as an expression (in the same way that expr evaluates its
argument). The value of the expression must be a boolean (a numeric value, where 0 is false and
anything is true, or a string value such as true or yes for true and false or no for false); if it is true
then body1 is executed by passing it to the Tcl interpreter. Otherwise expr2 is evaluated as an
expression and if it is true then body2 is executed, and so on. If none of the expressions evaluates to
true then bodyN is executed. The then and else arguments are optional ³noise words´ to make the
command easier to read. There may be any number of elseif clauses, including zero. BodyN may
also be omitted as long as else is omitted too. The return value from the command is the result of the
body script that was executed, or an empty string if none of the expressions was non-zero and there
was no bodyN.

switch - Evaluate one of several scripts, depending on a given value

? ?opà?ons? sàr?ng paààern body ?paààern body ...?


? ?opà?ons? sàr?ng {paààern body ?paààern body ...?}

The switch command matches its sàr?ng argument against each of the paààern arguments in
order. As soon as it finds a paààern that matches sàr?ng it evaluates the following body argument by
passing it recursively to the Tcl interpreter and returns the result of that evaluation. If the last paààern
argument is default then it matches anything. If no paààern argument matches sàr?ng and no default is
given, then the switch command returns an empty string.

If the initial arguments to switch start with - then they are treated as options unless there are exactly
two arguments to switch (in which case the first must the sàr?ng and the second must be the
paààern/body list). The following options are currently supported:

Looping

or - 'For' loop


sàarà àesà nexà body

For ?s a loop?ng command, s?m?lar ?n sàrucàure ào àhe C or sàaàemenà. The sàarà, nexà, and
body argumenàs musà be Tcl command sàr?ngs, and àesà ?s an express?on sàr?ng. The or command
?rsà ?nvokes àhe Tcl ?nàerpreàer ào execuàe sàarà. Then ?à repeaàedly evaluaàes àesà as an express?on; ?

c  

àhe resulà ?s non-zero ?à ?nvokes àhe Tcl ?nàerpreàer on body, àhen ?nvokes àhe Tcl ?nàerpreàer on nexà,
àhen repeaàs àhe loop. The command àerm?naàes when àesà evaluaàes ào 0. I a conà?nue command ?s
?nvoked w?àh?n body àhen any rema?n?ng commands ?n àhe currenà execuà?on o body are sk?pped;
process?ng conà?nues by ?nvok?ng àhe Tcl ?nàerpreàer on nexà, àhen evaluaà?ng àesà, and so on. I a
break command ?s ?nvoked w?àh?n body or nexà, àhen àhe or command w?ll reàurn ?mmed?aàely. The
operaà?on o break and conà?nue are s?m?lar ào àhe correspond?ng sàaàemenàs ?n C. For reàurns an
empày sàr?ng.

Noàe: àesà should almosà always be enclosed ?n braces. I noà, var?able subsà?àuà?ons w?ll be made
be ore àhe or command sàaràs execuà?ng, wh?ch means àhaà var?able changes made by àhe loop body
w?ll noà be cons?dered ?n àhe express?on. Th?s ?s l?kely ào resulà ?n an ?n ?n?àe loop. I àesà ?s enclosed ?n
braces, var?able subsà?àuà?ons are delayed unà?l àhe express?on ?s evaluaàed (be ore each loop
?àeraà?on), so changes ?n àhe var?ables w?ll be v?s?ble.

wh?le - Execuàe scr?pà repeaàedly as long as a cond?à?on ?s meà

?àesà body

The while command evaluates àesà as an expression (in the same way that expr evaluates its
argument). The value of the expression must a proper boolean value; if it is a true value then body is
executed by passing it to the Tcl interpreter. Once body has been executed then àesà is evaluated
again, and the process repeats until eventually àesà evaluates to a false boolean value. Continue
commands may be executed inside body to terminate the current iteration of the loop, and break
commands may be executed inside body to cause immediate termination of the while command. The
while command always returns an empty string.

Note: àesà should almost always be enclosed in braces. If not, variable substitutions will be made
before the while command starts executing, which means that variable changes made by the loop
body will not be considered in the expression. This is likely to result in an infinite loop. If àesà is
enclosed in braces, variable substitutions are delayed until the expression is evaluated (before each
loop iteration), so changes in the variables will be visible.

‘ ‘  -an?pulaàe array var?ables

Arrays are a bit different in TCL than most other languages in the sense that they are associative : they
work with key values not numeric indices. For instance, this is an array named highesttemp with keys
corresponding to month names (the array could store the highest temperature of the month) :

set highesttemp(January) 67

set highesttemp(June) 98

We can then access those values like this :

puts $highesttemp(June)

c 


You can use whatever value you want for a key value, just as long as it's unique. Also note, that unlike
most languages, the array can contain data of different types. This isn't entirely true, since everything in
TCL is a string, but the effect is that anything can be put in any array.

The great advantage of associative arrays like this is that there is no limit on their size. They are
expandable, an effect that requires linked lists or trees in most other languages.

There is also an easy way to display all the elements in the array :

parray highesttemp

v. ample programs

variable declaration

seà mam joc

seà hello ganda

seà space _

puàs $mam$space$hello

ouàpuà would be: joc_ganda

if

seà mam joc

seà Hello ganda

seà Space _

? { "$mam" == "joc" } àhen {puàs ³maba?à yan!´} else {puàs ³ewan lang naà?n!´}

FÔR

#Pr?nà ouà àhe powers o àwo rom 1 ào 1024:

or {seà x 1} {$x<=1024} {seà x [expr {$x * 2}]} {

puàs "x ?s $x"

WHDLE

seà x 0

wh?le {$x<10} {

c  

puàs "x ?s $x"

?ncr x

ARRAY

Looping through an array searching for a value isn't easy. Here is an example that sets up an array
holding details on a person. It then searches for a particular number representing the date of birth :

set person(name) "Dane"


set person(born) 1980
set person(salary) 78000
set search [array startsearch person]
while {[array anymore person $search]} {
set arrayelement [array nextelement person $search]
if {$person($arrayelement) == 1980} then {
puts "Found the date of birth"
}
}

c  


Das könnte Ihnen auch gefallen