Tcl: A Universal Scripting Language
John Ousterhout
Sun Microsystems Laboratories
[email protected]
http://www.sunlabs.com/~ouster
Introduction
What is a scripting language?
High level (basic units are big).
Most programs are short (extensions, glue).
Dynamic (interpreted).
What is a unversal scripting language?
Single language for many purposes.
Embeddable and extensible.
Benefits:
Easy to learn and use (5-10x faster than C?).
Power: more things programmable, applications work
together.
Active objects: replace data with scripts.
Tcl: A Universal Scripting Language
October 26, 1995, slide 2
Outline
An example:
Tcl: universal scripting language.
Tk: GUI toolkit based on Tcl.
Language rationale:
Unusual design goals (e.g. extensibility).
Key features:
Unstructured (everything's a string).
No grammar.
Quote by default.
Substitutions.
Challenge: raise the level of language design.
Tcl: A Universal Scripting Language
October 26, 1995, slide 3
How I Got Started
Interactive programs need command languages:
Typically redone for each application.
Result: weak, quirky.
emacs and csh powerful, but can't reuse.
Solution: reusable scripting language.
Interpreter is a C library.
Provides basic features: variables, procedures, etc.
Applications extend with additional features.
Tcl: A Universal Scripting Language
October 26, 1995, slide 4
Tcl: Tool Command Language
Simple syntax (similar to sh, C, Lisp):
set a 47
47
Substitutions:
set b $a
set b [expr $a+10]
47
57
Quoting:
set b "a is $a"
a is 47
set b {[expr $a+10]} [expr $a+10]
Tcl: A Universal Scripting Language
October 26, 1995, slide 5
More On The Tcl Language
Rich set of built-in commands:
Variables, associative arrays, lists.
C-like expressions.
Conditionals, looping:
if {$x < 3} {
puts "x is too small"
}
Procedures.
Access to UNIX files, subprocesses.
Only representation is strings:
Easy access from C.
Programs and data interchangeable.
Tcl: A Universal Scripting Language
October 26, 1995, slide 6
Factorial Procedure
proc fac x {
if $x<=1 {return 1}
expr $x*[fac [expr $x-1]]
}
fac 4
24
Tcl: A Universal Scripting Language
October 26, 1995, slide 7
Embedding Tcl In Applications
Application generates scripts.
Tcl parses scripts, passes words
to command procedures.
Application extends built-in
command set:
Tcl
Application
Init
Parser
Command
Loop
Built-In
Commands
Application
Commands
Define new object types in C.
Implement primitive operations
as new Tcl commands.
Build complex features with Tcl
scripts.
Tcl: A Universal Scripting Language
October 26, 1995, slide 8
Extensions
Init
Parser
Command
Loop
Extensions can be developed independently:
Network communication, database access, security, ...
Applications can include combinations of extensions.
Tcl: A Universal Scripting Language
October 26, 1995, slide 9