An Introduction To Tcl Scripting John Ousterhout Sun Microsystems Laboratories Tcl/Tk Tutorial, Part II.

Slides:



Advertisements
Similar presentations
Lecture 2 Introduction to C Programming
Advertisements

Introduction to C Programming
1 Chapter 2 Introduction to Java Applications Introduction Java application programming Display ____________________ Obtain information from the.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 2 - Introduction to C Programming Outline 2.1Introduction 2.2A Simple C Program: Printing a Line.
Introduction to C Programming
Tcl and Otcl Tutorial Part I Internet Computing KUT Youn-Hee Han.
Programming Using Tcl/Tk These slides are based upon u several Tcl/Tk text books u material byDr. Ernest J. Friedman-Hill.
Shell Programming Software Tools. Slide 2 Shells l A shell can be used in one of two ways: n A command interpreter, used interactively n A programming.
NS-2 LAB: TCL Semester A Miriam Allalouf.
Writing Tcl Scripts (cont.) Outline –Variable Scoping –Strings –Eval –File/Channel I/O –Processes –System Info –Errors –Reflection/Debugging –Libraries.
Introduction to Perl Learning Objectives: 1. To introduce the features provided by Perl 2. To learn the basic Syntax & simple Input/Output control in Perl.
 2007 Pearson Education, Inc. All rights reserved Introduction to C Programming.
Writing Tcl Scripts Outline Goal Reading Syntax Data Types
Introduction to Perl Software Tools. Slide 2 Introduction to Perl l Perl is a scripting language that makes manipulation of text, files, and processes.
Tcl/Tk 2 CS 414, Software Engineering I Mark Ardis Rose-Hulman Institute December 5, 2002.
Shell Programming Learning Objectives: 1. To understand the some basic utilities of UNIX File 2. To compare UNIX shell and popular shell 3. To learn the.
XP Tutorial 1 New Perspectives on JavaScript, Comprehensive1 Introducing JavaScript Hiding Addresses from Spammers.
Introduction to C Programming
Shell Script Examples.
Computer Programming for Biologists Class 2 Oct 31 st, 2014 Karsten Hokamp
Innovation Intelligence ® 1 Chapter 1: Introduction to TCL.
1 Operating Systems Lecture 3 Shell Scripts. 2 Brief review of unix1.txt n Glob Construct (metacharacters) and other special characters F ?, *, [] F Ex.
An Introduction to Unix Shell Scripting
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 2 Input, Processing, and Output.
UNIX Shell Script (1) Dr. Tran, Van Hoai Faculty of Computer Science and Engineering HCMC Uni. of Technology
Tcl/TK Tutorial. 2 Learning Tcl/TK What is Tcl/TK? –An interpreted programming language Build on-the-fly commands, procedures Platform-independent Easy.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 2 Chapter 2 - Introduction to C Programming.
Introduction to C Programming Angela Chih-Wei Tang ( 唐 之 瑋 ) Department of Communication Engineering National Central University JhongLi, Taiwan 2010 Fall.
 Pearson Education, Inc. All rights reserved Introduction to Java Applications.
Advanced Topics- Functions Introduction to MATLAB 7 Engineering 161.
1 Introduction to Perl CIS*2450 Advanced Programming Techniques.
Shell Programming Learning Objectives: 1. To understand the some basic utilities of UNIX File 2. To compare UNIX shell and popular shell 3. To learn the.
Programming Using Tcl/Tk Week 2 Seree Chinodom
TCL TK. Tcl/Tk C functions can become Tcl commands that are invoked interactively Tk = scriptable, portable user interface –Windows, X (Unix), MacOS,
Introduction to Tcl/Tk TraNese Christy U.S. Army Research Laboratory.
Perl Variables: Array Web Programming1. Review: Perl Variables Scalar ► e.g. $var1 = “Mary”; $var2= 1; ► holds number, character, string Array ► e.g.
Dongsoo S. Kim  Tcl: Tool Command Language Interpreted programming (scripting) language Build on-the-fly commands and procedures Embeddable.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 2 - Introduction to C Programming Outline.
Introduction to Programming with Tcl. Introduction Using two languages  C++ for “ data ”  Otcl for control Tcl has been widely used as a scripting language.
Scripting.
1 Lecture 2 - Introduction to C Programming Outline 2.1Introduction 2.2A Simple C Program: Printing a Line of Text 2.3Another Simple C Program: Adding.
© 2010 Robert K. Moniot1 Chapter 6 Introduction to JavaScript.
TCL/TK Tool Command Language/Tool Kit. What is TCL? u Tool Command Language u An interpreted programming language  Created by John Ousterhout.John Ousterhout.
Perl & TCL Vijay Subramanian, Modified from : perl_basics_06.ppt.
Web Database Programming Using PHP
TCL Training Joshi Sravan Kumar K 21-Nov-2011.
Java Script Introduction. Java Script Introduction.
Topics Designing a Program Input, Processing, and Output
CS 330 Class 7 Comments on Exam Programming plan for today:
Chapter 2 - Introduction to C Programming
Web Database Programming Using PHP
Introduction to Scripting
Chapter 2 - Introduction to C Programming
Programming Using Tcl Maxwell Created By: Quan Nguyen Created Date: 12/12/2016 TCL Training1.
TCL/TK Tool Command Language/Tool Kit.
Statements, Comments & Simple Arithmetic
Perl Variables: Array Web Programming.
Chapter 2 - Introduction to C Programming
Chapter 2 - Introduction to C Programming
NS-2 LAB: TCL Miriam Allalouf Semester A.
Introduction to C++ Programming
Chapter 2 - Introduction to C Programming
Chapter 2 - Introduction to C Programming
Python Primer 1: Types and Operators
Topics Designing a Program Input, Processing, and Output
Topics Designing a Program Input, Processing, and Output
Chapter 2 - Introduction to C Programming
Introduction to Bash Programming, part 3
Introduction to C Programming
Presentation transcript:

An Introduction To Tcl Scripting John Ousterhout Sun Microsystems Laboratories Tcl/Tk Tutorial, Part II

Tcl/Tk Tutorial Part II: Tcl ScriptingDecember 12, 1995, slide 2 Language Overview Two parts to learning Tcl: 1. Syntax and substitution rules: –Substitutions simple, but may be confusing at first. 2. Built-in commands: –Can learn individually as needed. –Control structures are commands, not language syntax.

Tcl/Tk Tutorial Part II: Tcl ScriptingDecember 12, 1995, slide 3 Basics u Tcl script = –Sequence of commands. –Commands separated by newlines, semi-colons. u Tcl command = –One or more words separated by white space. –First word is command name, others are arguments. –Returns string result. u Examples: – set a 22; set b 33 – set a 22 set b 33

Tcl/Tk Tutorial Part II: Tcl ScriptingDecember 12, 1995, slide 4 Division Of Responsibility Tcl Parser Command Procedure Command Words Result u Interprets words. u Produces string result. u Chops commands into words. u Makes substitutions. u Does not interpret values of words.

Tcl/Tk Tutorial Part II: Tcl ScriptingDecember 12, 1995, slide 5 Arguments u Parser assigns no meaning to arguments (quoting by default, evaluation is special): C: x = 4; y = x+10 y is 14 Tcl: set x 4; set y x+10 y is "x+10" u Different commands assign different meanings to their arguments: set a 122 expr 24/3.2 eval "set a 122" button.b -text Hello -fg red string length Abracadabra

Tcl/Tk Tutorial Part II: Tcl ScriptingDecember 12, 1995, slide 6 Variable Substitution  Syntax: $ varName u Variable name is letters, digits, underscores. u May occur anywhere in a word. Sample commandResult set b 6666 set a bb set a $b66 set a $b+$b+$b set a $b set a $b4 no such variable

Tcl/Tk Tutorial Part II: Tcl ScriptingDecember 12, 1995, slide 7 Command Substitution u Syntax: [script] u Evaluate script, substitute result. u May occur anywhere within a word. Sample commandResult set b 88 set a [expr $b+2]10 set a "b-3 is [expr $b-3]"b-3 is 5

Tcl/Tk Tutorial Part II: Tcl ScriptingDecember 12, 1995, slide 8 Controlling Word Structure u Words break at white space and semi-colons, except: –Double-quotes prevent breaks: set a "x is $x; y is $y" –Curly braces prevent breaks and substitutions: set a {[expr $b*$c]} –Backslashes quote special characters: set a word\ with\ \$\ and\ space u Substitutions don't change word structure: set a "two words" set b $a

Tcl/Tk Tutorial Part II: Tcl ScriptingDecember 12, 1995, slide 9 Expressions u C-like (int and double), extra support for string operations. u Command, variable substitution occurs within expressions.  Used in expr, other commands. Sample commandResult set b 55 expr ($b*4) expr $b <= 20 expr $a * cos(2*$b) expr {$b * [fac 4]}120 set a BillBill expr {$a < "Anne"}0

Tcl/Tk Tutorial Part II: Tcl ScriptingDecember 12, 1995, slide 10 Lists u Zero or more elements separated by white space: red green blue u Braces and backslashes for grouping: a b {c d e} f one\ word two three u List-related commands: concatlindexllengthlsearch foreachlinsertlrangelsort lappendlistlreplace u Examples: lindex {a b {c d e} f} 2 н c d e lsort {red green blue} н blue green red

Tcl/Tk Tutorial Part II: Tcl ScriptingDecember 12, 1995, slide 11 Control Structures u C-like appearance. u Just commands that take Tcl scripts as arguments. u Example: list reversal. set b "" set i [expr [llength $a] - 1] while {$i >= 0} { lappend b [lindex $a $i] incr i -1 } u Commands: ifforswitchbreak foreachwhileevalcontinue

Tcl/Tk Tutorial Part II: Tcl ScriptingDecember 12, 1995, slide 12 Procedures  proc command defines a procedure: proc sub1 x {expr $x-1} u Procedures behave just like built-in commands: sub1 3н 2 u Arguments can have defaults: proc decr {x {y 1}} { expr $x-$y } u Scoping: local and global variables. name list of argument names body

Tcl/Tk Tutorial Part II: Tcl ScriptingDecember 12, 1995, slide 13 Procedures, cont'd u Variable-length argument lists: proc sum args { set s 0 foreach i $args { incr s $i } return $s } sum н 15 sum н 0

Tcl/Tk Tutorial Part II: Tcl ScriptingDecember 12, 1995, slide 14 Errors u Errors normally abort commands in progress, application displays error message: set n 0 foreach i { } { set n [expr {$n + i*i}] } н syntax error in expression "$n + i*i"  Global variable errorInfo provides stack trace: set errorInfo н syntax error in expression "$n + i*i" while executing "expr {$n + i*i}" invoked from within "set n [expr {$n + i*i}]..." ("foreach" body line 2)...

Tcl/Tk Tutorial Part II: Tcl ScriptingDecember 12, 1995, slide 15 Advanced Error Handling u Can intercept errors: catch {expr {2 +}} msg н 1 set msg н syntax error in expression "2 +" u Can generate errors: error "bad argument"  Global variable errorCode holds machine-readable information about errors (e.g. UNIX errno value).

Tcl/Tk Tutorial Part II: Tcl ScriptingDecember 12, 1995, slide 16 Additional Tcl Features: u String manipulation commands: regexpformatsplitstring regsubscanjoin u File I/O commands: opengetsseekflushglob closereadtellcd putssourceeofpwd  Subprocesses with exec command: exec grep foo << $input | wc

Tcl/Tk Tutorial Part II: Tcl ScriptingDecember 12, 1995, slide 17 Additional Tcl Features, cont'd u Associative arrays: set x(fred) 44 set x(2) [expr $x(fred) + 6] array names x н fred 2 u Variable scoping: globaluplevelupvar u Access to Tcl internals: inforenametrace

Tcl/Tk Tutorial Part II: Tcl ScriptingDecember 12, 1995, slide 18 Additional Tcl Features, cont'd u Autoloading: – unknown procedure invoked when command doesn't exist. –Loads procedures on demand from libraries. –Uses search path of directories. u Coming soon (Tcl 7.5): –Dynamic loading of binaries: load command. –Security: Safe-Tcl. –Event-driven I/O. –Socket support.

Tcl/Tk Tutorial Part II: Tcl ScriptingDecember 12, 1995, slide 19 More On Substitutions  Keep substitutions simple: use commands like format for complex arguments.  Use eval for another level of expansion: exec rm *.o н *.o: No such file or directory glob *.o н a.o b.o exec rm [glob *.o] н a.o b.o: No such file or directory eval exec rm [glob *.o]

Tcl/Tk Tutorial Part II: Tcl ScriptingDecember 12, 1995, slide 20 Commands And Lists: Quoting Hell u Lists parse cleanly as commands: each element becomes one word. u To create commands safely, use list commands: button.b -text Reset -command {set x $initValue} ( initValue read when button invoked)... -command "set x $initValue" (fails if initValue is " New York ": command is " set x New York ")... -command "set x {$initValue}" (fails if initValue is " { ": command is " set x {{} ")... -command [list set x $initValue] (always works: if initValue is " { " command is " set x \{ ")

Tcl/Tk Tutorial Part II: Tcl ScriptingDecember 12, 1995, slide 21 Tcl Syntax Summary u Script = commands separated by newlines or semicolons. u Command = words separated by white space.  $ causes variable substitution.  [] causes command substitution.  "" quotes white space and semi-colons.  {} quotes all special characters.  \ quotes next character, provides C-like substitutions.  # for comments (must be at beginning of command).