Short Introduction to the Tool Command Language (Tcl)

Similar documents
ESPResSo Summer School 2012

Running the shell. Tcl/Tk demo. Tcl stuff. Starting Tcl/Tk. If-then-else

A Typing System for an Optimizing Multiple-Backend Tcl Compiler

Unix Shell Scripts. Contents. 1 Introduction. Norman Matloff. July 30, Introduction 1. 2 Invoking Shell Scripts 2

TCL IVR API Version 1.0 Programmer s Guide

Microsoft Windows PowerShell v2 For Administrators

Perl in a nutshell. First CGI Script and Perl. Creating a Link to a Script. print Function. Parsing Data 4/27/2009. First CGI Script and Perl

Advanced Bash Scripting. Joshua Malone

Embedded Systems. Review of ANSI C Topics. A Review of ANSI C and Considerations for Embedded C Programming. Basic features of C

Programming Languages CIS 443

Exercise 1: Python Language Basics

We will learn the Python programming language. Why? Because it is easy to learn and many people write programs in Python so we can share.

Moving from CS 61A Scheme to CS 61B Java

List of FTP commands for the Microsoft command-line FTP client

Hands-On UNIX Exercise:

Shell Scripts (1) For example: #!/bin/sh If they do not, the user's current shell will be used. Any Unix command can go in a shell script

Tel Scripting for Cisco IOS

Embedded Syslog Manager Configuration Guide, Cisco IOS XE Release 3SE (Catalyst 3850 Switches)

Beginners Shell Scripting for Batch Jobs

JAVA - QUICK GUIDE. Java SE is freely available from the link Download Java. So you download a version based on your operating system.

PHP Debugging. Draft: March 19, Christopher Vickery

Sources: On the Web: Slides will be available on:

Computational Mathematics with Python

PHP Tutorial From beginner to master

Top 72 Perl Interview Questions and Answers

Unix Scripts and Job Scheduling

Computational Mathematics with Python

Python Loops and String Manipulation

The programming language C. sws1 1

Exercise 4 Learning Python language fundamentals

Visual Logic Instructions and Assignments

Systems Programming & Scripting

CS Unix Tools & Scripting Lecture 9 Shell Scripting

1 Introduction. 2 An Interpreter. 2.1 Handling Source Code

JavaScript: Introduction to Scripting Pearson Education, Inc. All rights reserved.

$ftp = Net::FTP->new("some.host.name", Debug => 0) or die "Cannot connect to some.host.name: $@";

MetaKit: Quick and Easy Storage for your Tcl Application 1

Writing Simple Programs

Computational Mathematics with Python

University of Hull Department of Computer Science. Wrestling with Python Week 01 Playing with Python

Introduction to Python

C++ INTERVIEW QUESTIONS

Channel Access Client Programming. Andrew Johnson Computer Scientist, AES-SSG

Bachelors of Computer Application Programming Principle & Algorithm (BCA-S102T)

Topics. Parts of a Java Program. Topics (2) CS 146. Introduction To Computers And Java Chapter Objectives To understand:

Informatica e Sistemi in Tempo Reale

Introduction to Python

Introduction to Shell Programming

Lecture 9. Semantic Analysis Scoping and Symbol Table

First Java Programs. V. Paúl Pauca. CSC 111D Fall, Department of Computer Science Wake Forest University. Introduction to Computer Science

Fundamentals of Java Programming

Oracle Database: SQL and PL/SQL Fundamentals NEW

Oracle Database: SQL and PL/SQL Fundamentals NEW

Simulation Tools. Python for MATLAB Users I. Claus Führer. Automn Claus Führer Simulation Tools Automn / 65

1001ICT Introduction To Programming Lecture Notes

Introduction to Shell Scripting

VHDL Test Bench Tutorial

Scoping (Readings 7.1,7.4,7.6) Parameter passing methods (7.5) Building symbol tables (7.6)

HP-UX Essentials and Shell Programming Course Summary

1. Stem. Configuration and Use of Stem

ICS 351: Today's plan

ASCII Encoding. The char Type. Manipulating Characters. Manipulating Characters

Firewall Builder Architecture Overview

PROBLEM SOLVING SEVENTH EDITION WALTER SAVITCH UNIVERSITY OF CALIFORNIA, SAN DIEGO CONTRIBUTOR KENRICK MOCK UNIVERSITY OF ALASKA, ANCHORAGE PEARSON

Certified PHP Developer VS-1054

Learn Perl by Example - Perl Handbook for Beginners - Basics of Perl Scripting Language

Invitation to Ezhil : A Tamil Programming Language for Early Computer-Science Education 07/10/13

Name: Class: Date: 9. The compiler ignores all comments they are there strictly for the convenience of anyone reading the program.

VI(Visual) Editor Reference manual

Bash shell programming Part II Control statements

Bash Guide for Beginners

Oracle Database: SQL and PL/SQL Fundamentals

Oracle Database: SQL and PL/SQL Fundamentals

Web Development using PHP (WD_PHP) Duration 1.5 months

Comparing RTOS to Infinite Loop Designs

Installing C++ compiler for CSc212 Data Structures

Java SE 8 Programming

Chapter 13. Introduction to SQL Programming Techniques. Database Programming: Techniques and Issues. SQL Programming. Database applications

TIP: To access the WinRunner help system at any time, press the F1 key.

Programming Exercises

6.170 Tutorial 3 - Ruby Basics

Lecture 2 Notes: Flow of Control

PageR Enterprise Monitored Objects - AS/400-5

UNIX: Introduction to TELNET and FTP on UNIX

Big Data and Analytics by Seema Acharya and Subhashini Chellappan Copyright 2015, WILEY INDIA PVT. LTD. Introduction to Pig

How to Avoid Learning Expect or Automating Automating Interactive Programs

grep, awk and sed three VERY useful command-line utilities Matt Probert, Uni of York grep = global regular expression print

Regular expressions are a formal way to

Eventia Log Parsing Editor 1.0 Administration Guide

Facebook Twitter YouTube Google Plus Website

Windows PowerShell Essentials

Transcription:

Short Introduction to the Tool Command Language (Tcl) http://www.faculty.jacobs-university.de/jschoenwae/anl-2008/ September 22, 2008

Language Overview 1 Language Overview 2 Substitutions, Quoting, Comments 3 Expressions and Control Commands 4 Lists and Arrays 5 Procedures and Scopes 6 Additional Core Tcl Commands

Why a new command language? Common problem Interactive programs need a command language Most programs use a different command language Many command languages are limited and ad-hoc Some command languages are good but not reusable Tcl solution: Reusable command language interpreter Implemented as an embeddable C library Procedures, variables, associative arrays, lists, expressions, loops, exceptions, introspection, etc. Applications can add new commands to the interpreter

Why should I use Tcl in my Application? All applications using Tcl use the same syntax Tcl makes applications easily programmable Tcl extensions support the integration of simple applications as components into larger applications Breaking a complex application into modules written in different languages: Complex data structures and speed C components Composition of components Tcl scripts Tcl is freely available, well documented and very stable

Real-world usage of Tcl Web applications (AOL server) Testing and automation (see the expect extension) Scripting language of some Cisco devices Embedded language of some set-top boxes Configuration language of the ns-2 network simulator Software regression testing Scripting of embedded database systems Industrial control language (CAD, EDA)...

Design Goals Simple syntax, easy to learn Supports all constructs of real programming languages Easy to implement and easy to interpret Simple interface to the C programming language No distinction between built-in and extension commands Usage simplicity is more important than efficiency Portability: Win32, Unix, MacOS, Linux,...

Basic Principles of Tcl Commands have a sequence of arguments, separated by white space Commands can be separated by line ends or semicolons Variables: set a hello set b $a Substitution of results of Tcl commands: set b 42 set a [expr $b+2] Quoting (almost) like in a shell: ", {, \ Simple substitution rules (but sometimes confusing)

Data Types and Commands Tcl data types: Everything can be represented as a character string Character strings can be interpreted as lists Associative arrays support fast keyed access to data Programm text and data is interchangeable Tcl commands: All language constructs are commands (no keywords) Some command arguments can be arithmetic expressions Conditional commands and loop commands Procedures are sequences of commands that can be called Built-in commands to access file systems and basic operating system services

Tcl Syntax Rules A Tcl script is a sequence of Tcl commands Commands are separated by line ends or semicolons The words of a command are separated by white space The first word of a command is the command name The remaning words are the arguments passed to the command Examples set a 23; set b 33 set t Hello string match *ll* Hello expr 23 * 2-4

Command Execution in the Interpreter Tcl parser command procedure command tokenizing and substitution 1. word (command name) 2. word (1. argument) 3. word (2. argument) 4. word (3. argument) 5. word (4. argument) interpretation result (character string)

Command Arguments The parser does not associate any semantics to command arguments Different commands can interpret arguments in differently Examples set a "5+9" -> 5+9 set a -> 5+9 expr "5+9" -> 14 eval "set a 5+9" -> 5+9 exec ls wc -l -> 12 if {5+9<15 { puts stdout Abraccadabra -> ok

Substitutions, Quoting, Comments 1 Language Overview 2 Substitutions, Quoting, Comments 3 Expressions and Control Commands 4 Lists and Arrays 5 Procedures and Scopes 6 Additional Core Tcl Commands

Variable Substitution A $ in front of a variable name leads to the substitution of the name with the value of the named variable A name of a variable may contain letters, digits, and underscores (actually more if you really want) Variable substitution can happen anywhere in a word (unless care has been taken to prevent it) Examples set b 66 -> 66 set a b -> b set a $b -> 66 set a $b+$b -> 66+66 set a $b.3 -> 66.3 set a 2$b -> 266 set a $b2 -> no such variable

Command Substition A Tcl script enclosed in brackets [] will be evaluated and replaced by the result of the evaluation Command substitutions can be nested Command substitution can happen anywhere in a word (unless care has been taken to prevent it) Examples set b 66 -> 66 set a [expr $b+2] -> 68 set a b+2=[expr $b+2] -> b+2=68 set a [expr $b+[string length $a]] -> 72

Quoting Rules Double quotes " prevent word splitting but substitutions take place within the enclosed character string Curly braces { prevent word splitting and substitutions A backslash \ replaces the following character with a special sequence of charaters or the character itself, for example \r \n \{ Substitutions do not impact word boundaries Examples set b 66 -> 66 set a "b value $b" -> b value 66 set a {b value $b -> b value $b set c "{value" -> {value set a "b $c [set b]" -> b {value 66

Comments A hash character # starts a comment when the Tcl parser expects the start of a command. A comment ends at the end of the line A # character within a comment does not have a special meaning Examples # comment set a one; # set a two -> one set a # -> # set a one # comment -> wrong # args set a one ; # comment -> one

Expressions and Control Commands 1 Language Overview 2 Substitutions, Quoting, Comments 3 Expressions and Control Commands 4 Lists and Arrays 5 Procedures and Scopes 6 Additional Core Tcl Commands

Command: expr The expr command evaluates expressions Expressions are similar to expressions in the C language Expression may include string comparisons and a number of maths functions Examples expr (5*8)+2 -> 42 expr (5*8)+2.0 -> 42.0 expr cos(0) -> 1.0 expr 5/3 -> 1 expr 5/double(3) -> 1.66667 expr {"Alf" < "Bert" -> 1 expr "Alf" < "Bert" -> syntax error...

Command: expr Tcl allows command and variable substituions in expressions Numbers with a leading 0 are treated as octal numbers Numbers starting with 0x are treated as hexadecimal numbers Precision is controlled by the Tcl variable tcl_precision Examples set a 22 -> 22 expr ($a*4)-3 -> 85 expr {($a * [string length "date"]) -> 88 expr 011 -> 9 expr 09 -> syntax error set tcl_precision 12 -> 12 expr 5/3.0 -> 1.66666666667

Command: if The if command evaluates the first argument as an expression and, depending of the result, one of the script arguments is evaluated The words (arguments) then and else are optional Chaining of expressions using elseif is possible Examples if {$a == "yes" then { set a 1 elseif {$a == "no" { set a 0 else { set a "" if $n { set a $n { set a ""

Command: switch The switch command compares the first argument against a number of pattern and evaluates the corresponding script Options set the type of comparison (exact, glob, regexp) Examples: switch $a { yes { set a 1 no { set a 0 default { set a "" switch -glob $a { [yy]* { set a 1 [nn]* { set a 0 * { set a ""

Command: for The for command is implemented as follows: 1 Evaluate the first argument as a Tcl script 2 Evaluates the second argument as an expression and stop if the result is zero 3 Evaluate the fourth argument as a Tcl script. 4 Evaluate the third argument as a Tcl script and repeat from step two Example for {set i 0 {$i < 10 {incr i { puts "$i [expr sqrt($i)]"

Command: while The while command is implemented as follows: 1 Evaluate the first argument as an expression and stop if the result is zero 2 Evaluate the second argument as a Tcl script an repeat from step one Example set i 0 while {$i < 10 { puts "$i [expr sqrt($i)]" incr i

Command: foreach The foreach command successively assigns the values contained in the (list of the) second argument to the variable named in the first argument and executes the third argument as a Tcl script after each assignment Examples: foreach letter "a e i o u" { puts "$letter is a vocal" foreach file [glob *.c] { if {! [file readable $file] { puts "$file not readable"; break

Command: break and continue The break command stops the execution of the enclosing for, while, or foreach command The continue command causes a jump to the next iteration in a for, while, or foreach loop Examples: foreach file [glob *.c] { if {! [file isfile $file] { continue if {! [file readable $file] { break

Lists and Arrays 1 Language Overview 2 Substitutions, Quoting, Comments 3 Expressions and Control Commands 4 Lists and Arrays 5 Procedures and Scopes 6 Additional Core Tcl Commands

Lists A list consists of elements separates by white space Curly braces can be used to construct lists within a list Lists can always be split into commands - every list element represents a word Commands for manipulating lists: list lappend linsert llength lindex lsearch lreplace lrange lsort concat foreach Examples set a "a b" -> a b set l [list $a b] -> {a b b lappend l $l -> {a b b {{a b b llength $l -> 3 lindex $l 2 -> {a b b

Arrays An array is constructed by adding parenthesis enclosing an index value to a variable name Arrays are associative (indexed by strings) A variable is either a scalar or an array but never both The array env contains the environment variables The array command allows to iterate over arrays Examples set a(0) zero set env(shell) set env foo -> zero -> /bin/sh -> variable is array foreach index [lsort [array names env]] { puts "$index \t $env($index)"

Procedures and Scopes 1 Language Overview 2 Substitutions, Quoting, Comments 3 Expressions and Control Commands 4 Lists and Arrays 5 Procedures and Scopes 6 Additional Core Tcl Commands

Procedures The proc command creates procedures. 1 The first argument defines the name of the procedure 2 The second argument defines the list of argument names 3 The third argument defines the body of the procedure Prozedures behave like all other Tcl commands An argument name consisting of a list of two elements defines a default value The special argument name args allows a variable number of arguments The return command returns the result produced by the procedure and returns from the procedure call

Procedures Examples proc write {text {file stdout { puts -nonewline $file $text proc writeln {text {file stdout { puts $file $text proc sum {args { set sum 0 foreach num $args { incr sum $num return $sum

Variable Scopes Every Tcl procedure invocation maintains its own set of variables Variables within a procedure have by default local scope The global command marks variables as global Examples proc b {b { return [set a $b] set a 42 puts "[b 24] $a" set term $env(term) proc initialize { { global term puts $term

Variable Scopes Example upvar binds a local variable to a variable in the selected context in the call stack uplevel evaluates a script in the selected context in the call stack proc ldelete {varname element { upvar 1 $varname list set result "" foreach e $list { if {$e!= $element { lappend result $e set list $result

Variable Scopes Examples proc do { body while expr { if {$while!= "while" { error "expected while but got \"$while\"" uplevel 1 $body uplevel 1 [list while $expr $body] proc try {body {foo { { if {$foo == "" { catch [list uplevel 1 $body] else { if {[catch [list uplevel 1 $body] errormsg] { catch [list uplevel 1 $foo]

Additional Core Tcl Commands 1 Language Overview 2 Substitutions, Quoting, Comments 3 Expressions and Control Commands 4 Lists and Arrays 5 Procedures and Scopes 6 Additional Core Tcl Commands

String Manipulation Commands append format scan string regexp regsub split join Examples scan $date "%*s %*s %*s %s %*s %*s" time -> 1 set $time -> 15:55:30 split $time : -> 15 55 30 join [split $time :] - -> 15-55-30 string compare gross klein -> -1 set name [string tolower Mayer] -> mayer string match {m[ae][iy]er $name -> 1 regsub -all {\\"([aaoouu]) {j\"urgen {\1e name -> juergen

File Manipulation Commands open close seek tell eof gets read puts flush exec file glob cd pwd source Examples foreach file [lsort [glob *]] { puts [format "%8d %s" [file size $file] $file] proc ftp {user hostname server remotefile localfile { set f [open " ftp -n" "r+"] puts $f "open $server" puts $f "user anonymous $user@$hostname" puts $f "get $remotefile $localfile" puts $f "close" flush $f close $f

Errors and Exceptions Example An error stops the execution of a command and returns an error message as the error result up the call stack The global variable errorinfo contains the call stack trace The catch command evaluates a script and catches any errors so that scripts can handle them The error command generates (throws) an error proc sc { { while {[gets stdin expr] > 0 { if [catch {expr $expr msg] { puts stderr "Error : $msg" else { puts "$expr ==> $msg"

References J. K. Ousterhout. TCL: An Embeddable Command Language. In Proc. Winter USENIX Conference, pages 133 146, 1990. J. K. Ousterhout. An X11 Toolkit Based on the TCL Language. In Proc. Winter USENIX Conference, pages 105 115, 1991. J. K. Ousterhout. Tcl and the Tk Toolkit. Addison-Wesley, April 1994. J. K. Ousterhout. Scripting: Higher-level programming for the 21st century. IEEE Computer, 31(3), March 1998. B. Welch and K. Jones. Practical Programming in Tcl and Tk. Prentice Hall, 4 edition, 2003.