Sei sulla pagina 1di 6

http://www2.lib.uchicago.

edu/~keith//tcl-course/#emacs

Introduction to the Tcl Programming Language [ excerpts ]

T cl (pronounced Tickle) is a
general purpose program-
ming language originally in-
cations (C programs for example)
as a configuration and extension
language. This minimizes the
Automatic Memory
Management

All Tcl data structures are dy-


tended to be embedded in other number of languages that users
namically allocated and fully
applications as a configuration need to learn in order to config-
variable in size. The program-
and extension language. The suc- ure their applications, and makes
mer never needs to allocate mem-
cess of one of its most important these applications programm-
ory or specify maximum sizes.
embeddings, the Tk toolkit for able with no extra effort. In addi-
the X Windows System, has re- tion, Tcl is a complete and well-
Event-Driven
sulted in Tcl and Tk together be- designed programming language, Programming
ing most heavily used for build- whereas many existing configur-
ing graphical user interfaces ation languages were designed Tcl supports event-driven prog-
(GUIs). It is also heavily used as (to be kind) in an ad hoc manner. ramming (required for GUI prog-
a scripting language like Awk, ramming) with the ability to asso-
Perl or Rexx, and (as Expect) Extensibility ciate Tcl code with any variable
is used to script interactive or array element (the code is exe-
applications (e.g. , to automate Tcl is specially designed to make
cuted automatically whenever the
telnet logins to various informa- it extremely easy to extend the
variable is read or written).
tion vendors). language by the addition of new
primitives in C. These new
Tcl is virtually unique in the com- IPC Between Multiple Tcl
primitives are truly first-class
bination of features it offers: Applications
citizens of the language, sharing
the same error handling and Tcl supports the passing of Tcl
Scripting Language
memory management as the code as messages between Tcl
Tcl is a powerful scripting lang- original primitives. This has led applications running on differ-
uage that runs under Unix, to many useful extensions, such ent machines across the Inter-
Linux, VMS, DOS / Windows, as DBMS access (extensions exist net. This allows client-server
OS/2, and MacOS (at least). It for Oracle, Sybase, Ingres, Post- protocols which incorporate
provides all the usual high-level gres and many other DBMS's), the full power of the Tcl lang-
programming features that we've SNMP, Motif, etc. In addition, uage, and makes it possible to
come to expect from languages this allows Tcl programs to be write very tightly-integrated
like the Unix shell, Awk, Perl, or optimized by moving time- applications.
Rexx, such as : critical code into C.
• Variable-length strings Program Development
Equivalence of Data and Tools
• Associative arrays Programs
• Lists Tcl has a powerful symbolic de-
Like Lisp, Tcl uses the same bugger, timing and profiling
• Keyed lists (aka structs, representation for data and for tools, language sensitive editing
structures or records)
programs. This means that Tcl modes for Emacs, a WYSIWYG
• Pattern matching with reg- programs or scripts can be man- GUI builder (xf), a real-time app-
ular expressions ipulated as data: stored in varia- lication monitor (tkinspect), etc.
• Ability to define or rede- bles, written to and later read
fine procedures at run-time from files or databases, passed Freely Redistributable
• Full file access from one Tcl program to another
across the Internet, etc. In addi- The source code for the Tcl lang-
• Error handling uage system is freely copyable
tion, it means that the program-
mer can create new Tcl control for any purpose, so it can be
Embeddability
structures or error handling rou- used in commercial applications
Tcl is a small language designed tines as easily as writing a simple as well as academic applications
to be embedded in other appli- function. and freeware. □

TCL_Notes page 1 of 6
http://freewrap.sourceforge.net/

freeWrap
Builds stand-alone TCL/TK executables. No compiler required!
OR Use it as a single-file WISH shell

The freewrap program turns freewrap to behave as a stand- freeWrap includes several
TCL/TK scripts into single-file alone, single-file WISH that can Windows-specific commands
binary executable programs. be used to run any TCL/TK script. for installing your wrapped
applications.
The resulting program can be dis- freewrapTCLSH can be used
tributed to machines that do not as a single file stand - alone These commands can be used to
have TCL/TK installed. The exe- TCLSH shell determine the location of Windows'
cutable will also work on mach- special directories and make for
ines that have TCL/TK installed Renaming the freewrapTCLSH easy creation of file extension
program to some other file name associations and shortcuts.
but will use its own TCL / TK
causes freewrapTCLSH to behave
"image". freeWrap itself does not
as a a stand-alone, single-file freeWrap includes commands
need TCL / TK installed to run.
TCLSH shell that can be used to for ZIP file creation and
run any TCL script. extraction.
The freeWrap program is a TCL/
TK script that has been attached Due to freeWrap's use of the ZIP
Shared libraries can be used
to a single-file version of the WISH Virtual File System any ZIP
with your wrapped programs.
shell program. The single-file archive can be opened so its
WISH was created with the help of FreeWrapped applications can contents look like a simple file
the ZIP Virtual File System (ZVFS) load TCL/TK shared library exten- subdirectory. The archive's files
source code provided by D. Richard sions that have been compiled are automatically decompressed
Hipp. The ZVFS code has been with the STUBS interface. when read with TCL commands.
adapted for use with TCL's virtual
The makeZIP command allows
file system interface. Your wrapped programs can
creation and modification of ZIP
be customized with your own
Easy, one-step wrapping. archives from within your free-
window icons.
Wrapped application.
FreeWrap consists of a single exe- The Windows version of freeWrap
cutable file. There is no setup re- can incorporate your own custom- Availability
quired. Wrapping is accomplished ized icon into your wrapped app- FreeWrap executables are freely
with a single command. lication. available for both Linux and
Windows 95 / 98 / NT / 2000 / XP.
freewrapTCLSH can be used No license fees for
Instructions and source code for
to wrap TCL-only scripts. wrapped programs.
building freeWrap on both Win-
FreewrapTCLSH creates a single There are no license fees associ- dows and UNIX platforms are
executable file from a TCL script. ated with freeWrap. See the free- included in the freeWrap source
The wrapping syntax is identical to Wrap documentation [ 1 ] code distribution. See the Down-
the freewrap program. This produ- loads table below for specifics [ 2 ]
ces a console-only type of program. Cross-platform generation
of programs is supported. Versions of freeWrap that include
freeWrap can be used as a the BLT and TkTable extensions
The -w "wrap using" option allows to TCL/TK are also available for
single file stand-alone WISH
cross-platform creation of wrapped download. TCL-only versions of
Renaming the freewrap program applications without the use of the freeWrap are available also for
to some other file name causes target computer system. wrapping TCL (non-TK) scripts. □

[ 1 ] freeWrap documentation: http://freewrap.sourceforge.net/freeWrapDocs.pdf

[ 2 ] Downloads table: http://freewrap.sourceforge.net/#Downloads

TCL_Notes page 2 of 6
TCL Fundamentals http://www.beedub.com/book/3rd/Tclintro.pdf
Brent Welch, K. Jones and J. Hobbs from: http://www.beedub.com/book/ Sample chapters in html, pdf.

«Tcl is a string-based command language. The language has only a few fundamental constructs and
relatively little syntax, which makes it easy to learn. The Tcl syntax is meant to be simple. Tcl is designed
to be a glue that assembles software building blocks into applications. A simpler glue makes the job easier.
In addition, Tcl is interpreted when the application runs. The interpreter makes it easy to build and refine
your application in an interactive manner. A great way to learn Tcl is to try out commands interactively.»

TclTutor http://www.msen.com
© Copyright 1995,2000 by Clif Flynt

«TclTutor is a Computer Aided Instruction package that teaches the basics of the Tcl
programming language. The 40+ lessons in this package can be completed in under 10
minutes each. You'll be ready to start simple programs after the first half dozen lessons.
This program will teach you the syntax for the main Tcl commands and options.» Available
for Mac OS8-10, Unix and Win95+. To download for Windows, pick «Self-Installing Zip» or
(preferably) « Free-Wrapped Executable. »

Programming:Tcl http://en.wikibooks.org/wiki/Programming:Tcl_Print_version

by Richard Suchenwirth

Tcl Tutorial http://www.tcl.tk/man/tcl8.5/tutorial/tcltutorial.html


by Clif Flynt
«Tcl/Tk (called tickleTK) is a very powerful programming language for all sorts of projects, from visual
application development to web scripting to meshing together varieties of applications and utilities written in
different languages in order to create a new application. The program competes very favorably with Visual Basic,
Java, Perl, and UNIX shell languages. It is a great tool for rapid prototype development; runs on almost all hard-
ware, with no code changes; and is much easier to learn than Perl, C/C++, or Java.» -- Library Journal Review.

Tcl/Tk Cookbook http://www.dci.clrc.ac.uk/Publications/Cookbook/index.html

«Tcl is an interpreted language with programming features, available for Unix, Windows and the Mac [8-10]
OSes. Tk the associated toolkit is an easy, efficient way to develop window[ed-type] based applications and has
a wide user base. Ease of learning Tcl/Tk make it most appropriate for teaching GUI design. This cookbook
provides examples with comments so a novice user quickly climbs the learning curve in a relatively short time.»

ACTIVETCL USER GUIDE http://aspn.activestate.com/ASPN/docs/ActiveTcl/8.4/at.pkg_index.html

Active Tcl free download http://www.activestate.com/Products/ActiveTcl/

http://www.beedub.com/book/3rd/tclhttpd.pdf from: http://www.beedub.com/book/

TclHttpd Web Server


«TclHttpd is interesting because as a Tcl script it is easy to add to your application. Suddenly your app
has an interface that is accessible to Web browsers in your company’s intranet or the global Internet.»

Reference: http://wiki.tcl.tk/tclhttpd from: http://mini.net/sdarchive/

TCL_Notes page 3 of 6
http://www.linuxfocus.org/English/September1999/article110.html

The Tcl Syntax


by Brent B. Welch An Introduction to Tcl Syntax
September, 1999

For a scripting language, Tcl has a simple syntax. Grouping and Substitution

cmd arg arg arg The Tcl syntax is used to guide the Tcl parser
A Tcl command is formed by words separated through three steps: argument grouping, result sub-
by white space. The first word is the name of the stitution, and command dispatch.
command, and the remaining words are argu-
ments to the command. 1. Argument grouping. Tcl needs to determine
how to organize the arguments to the commands.
$foo In the simplest case, white space separates argu-
The dollar sign ($) substitutes the value of a vari- ments. The quotation marks and braces syntax is used
able. In this example, the variable name is foo. to group multiple words into one argument. In the
previous example, double quotation marks are used
[clock seconds] to group a single argument to the puts command.
Square brackets execute a nested command. For
example: if you want to pass the result of one com- 2. Result substitution. After the arguments are
mand as the argument to another, you use this syn- grouped, Tcl performs string substitutions. Put
tax. In this example, the nested command is clock simply: it replaces $foo with the value of the vari-
seconds, which gives the current time in seconds. able foo, and it replaces bracketed commands with
their result. That substitutions are done after group-
"some stuff"
ing is crucial. This sequence ensures that unusual
Double quotation marks group words as a single ar- values do not complicate the structure of commands.
gument to a command. Dollar signs and square brack-
ets are interpreted inside double quotation marks. 3. Command dispatch. After substitution, Tcl
uses the command name as a key into a dispatch
{some stuff}
table. It calls the C procedure identified in the table,
Curly braces also group words into a single ar- and the C procedure implements the command.
gument. In this case, however, elements within You also can write command procedures in Tcl.
the braces are not interpreted. There are simple conventions about argument pass-
\
ing and handling errors.
The backslash ( \ ) is used to quote special char-
Another Example
acters. For example: \n generates a newline.
The backslash also is used to "turn off" the set i 0
special meanings of the dollar sign, quotation while {$i < 10} {
marks, square brackets, and curly braces. puts "$i squared = [expr $i*$i]"
incr i
}
A Little Example
Here, curly braces are used to group arguments
Below is a Tcl command that prints the current without doing any substitutions. The Tcl parser
time. It uses three Tcl commands: set, clock, and
knows nothing special about the while command.
puts. The set command assigns the variable. The
clock command manipulates time values. The puts
It treats it like any other command. It is the
command prints the values. implementation of the while command knows that
the first argument is an expression, and the second
set seconds [clock seconds] argument is more Tcl commands. The braces group
puts "The time is [clock format $seconds]" two arguments: the boolean expression that controls
the loop; and the commands in the loop body.
Note that you do not use $ when assigning a variable.
Only when you want the value do you use $. The We also see two math expressions: the boolean
seconds variable isn't needed in the previous example. comparison and multiplication. The while command
You could print the current time with one command: automatically evaluates its first argument as an
expression. In other cases you must explicitly use
puts "The time is [clock format [clock
seconds]]" the expr command to perform math evaluation.

TCL_Notes page 4 of 6
Command Dispatch

Lastly, Tcl calls something else to do the hard work. arguments from the Tcl command and return a new
We've seen that Tcl uses expr to perform math string as their result. It is very easy to write C com-
functions; puts to handle output functions; and set mand procedures. They can do everything from
accessing databases to creating graphical user
to assign variables. These Tcl commands are imple-
interfaces. Tcl, the language, doesn't really know
mented by a C procedure that has registered itself what the commands do. It just groups arguments,
with Tcl. The C command procedures take the string substitutes results, and dispatches the commands.

One Last Example

Here is the factorial procedure:

proc fac {x} {


if {$x < 0} {
error "Invalid argument $x: must be a positive integer"
} elseif {$x <= 1} {
return 1
} else {
return [expr $x * [fac [expr $x-1]]]
}
}

More Reading

Chapter 1 of Practical Programming in Tcl and Tk

http://www.beedub.com/book/2nd/tclintro.doc.html

E. J. Friedman's Tcl/Tk Course:

http://www.linbox.com/ucome.rvt/any/doc_distrib/tcltk-8.3.2/TclCourse/TclWeek1.html

http://www.linbox.com/ucome.rvt/any/doc_distrib/tcltk-8.3.2/TclCourse/TclWeek2.html

http://www.linbox.com/ucome.rvt/any/doc_distrib/tcltk-8.3.2/TclCourse/TclWeek3.html

http://www.linbox.com/ucome.rvt/any/doc_distrib/tcltk-8.3.2/TclCourse/TclWeek4.html

http://www.linbox.com/ucome.rvt/any/doc_distrib/tcltk-8.3.2/TclCourse/TclWeek5.html

Brent B. Welch is the author of "Practical Programming in Tcl and Tk." Welch received a BS in
Aerospace Engineering at the University of Colorado, Boulder, in 1982 and an MS in Computer Science
at the University of California, Berkeley, in 1986 and a PhD in Computer Science at the University of
California, Berkeley, in 1990. Previously Welch was a member of research staff at Xerox PARC work-
ing on distributed systems, and later a member of the Tcl/Tk team at Sun Microsystems Laboratories.
He is a member of the ACM and the IEEE Computer Society. Home Page: http://www.beedub.com/

TCL_Notes page 5 of 6
http://www.infoworld.com/cgi-bin/displayNew.pl?/petrel/980928np.htm

September 28, 1998

[ excerpts ]

The salacious truth of the scripting paradigm shift to Tcl/Tk and Python

H ere's a buzz phrase that should become


big in the next year or so -- "scripting
language." Though you can build full-blown
You're much better off writing a simple script.
That's not to say you can't give the script a GUI
front end, even if it is nothing more than a button
applications with many scripting languages, they labeled "Go." But the administrative task itself
are generally used to automate a complex series is nevertheless script material.
of tasks you would normally have to type out at a
So expect scripting in general to take off soon. Of
command prompt. Perl, sh, ksh, csh, bash,
the scripting languages available, I predict Tcl/Tk
Tcl / Tk, Rexx, Scheme, and Python are all
and Python will eventually gain the popularity
considered scripting languages.
Perl now enjoys. Although it's somewhat of an
The Web has raised the visibility of Perl in recent apples and oranges comparison, both Tcl/Tk and
years. Perl has become practically synonymous with Python are easier to learn than Perl. And like
CGI, due to its elegant character - string handling. Perl, there is a version of Tcl/Tk and Python for
practically every platform, including Windows 95
Look for other scripting languages to get a boost
and Windows NT.
soon, as PCs get more robust and invade the
enterprise infrastructure where the advanced Both Python and Tcl/Tk are actually combination
workstations and minicomputers once ruled. packages. Tcl consists of the Tool Command Lang-
uage, which provides most of its scripting cap-
Why? -- As much as some people would like to
abilities; and Tk, or TookKit, which adds the GUI
think PCs can replace big machines without
features to Tcl. Likewise, when people talk about
having to resort to a command-line paradigm,
Python, they're usually referring to a combination
I'm here to tell you it just ain't possible. GUIs of Python, Tkinter, and Tk. Python is the object-
are by nature general purpose interfaces. They're oriented scripting language. Tkinter is the wrapper
great for introducing simplicity and consistency that allows Python to use Tk as its de facto stand-
to applications that perform a predictable set of ard for GUI features.
individual tasks. But they're really lousy at hand-
ling huge, complicated, and unpredictable admin- Most important, Tcl/Tk and Python are easily
istrative operations that step outside the nice, neat extensible. They hook into C, C++, and other
function libraries and programs quite easily.
boundaries of predefined groups of users or files.
Enterprising programmers have tapped this capa-
Imagine that you must grant specific limited bility to give Tcl/Tk and Python access to every-
access to 30 directories, 253 files, five printers, thing from IMAP4 e-mail features to Informix,
and 17 discussion groups for the Argyle Sock Oracle, and Sybase database access. You can even
Division of the worldwide sales force, but give get an ODBC interface for Tcl/Tk on Windows.
less-limited rights to any member of that subset
Last but not least, the best selling point of Tcl/Tk
who is also on the management task force but
and Python is that they are free for download.
not based in France. Now imagine this task
requires administrative changes across multiple Are you using PCs more and enjoying it less?
applications and OS platforms. A generic GUI Have you begun to discover the wonderful world
administration tool just isn't going to cut it. of scripting as a result? 

TCL_Notes page 6 of 6

Potrebbero piacerti anche