Writing Tcl Scripts Outline Goal Reading Syntax Data Types

Slides:



Advertisements
Similar presentations
PL/SQL.
Advertisements

1 Chapter 2 Introduction to Java Applications Introduction Java application programming Display ____________________ Obtain information from the.
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.
Scalar Variables Start the file with: #! /usr/bin/perl –w No spaces or newlines before the the #! “#!” is sometimes called a “shebang”. It is a signal.
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.
Bash, part 2 Prof. Chris GauthierDickey COMP Unix Tools.
Introduction to Computers and Programming - Class 2 1 Introduction to Computers and Programming Class 2 Introduction to C Professor Avi Rosenfeld.
1 Problem 2 A Scanner / Parser for Simple C. 2 Outline l Language syntax for SC l Requirements for the scanner l Requirement for the parser l companion.
Tcl/TK Tutorial Fan Yang Kristy Hollingshead
Tcl/Tk 2 CS 414, Software Engineering I Mark Ardis Rose-Hulman Institute December 5, 2002.
Shell Script Examples.
Innovation Intelligence ® 1 Chapter 1: Introduction to TCL.
1 Operating Systems Lecture 3 Shell Scripts. 2 Shell Programming 1.Shell scripts must be marked as executable: chmod a+x myScript 2. Use # to start a.
1 Operating Systems Lecture 3 Shell Scripts. 2 Brief review of unix1.txt n Glob Construct (metacharacters) and other special characters F ?, *, [] F Ex.
CSC 330 E-Commerce Teacher Ahmed Mumtaz Mustehsan Ahmed Mumtaz Mustehsan GM-IT CIIT Islamabad GM-IT CIIT Islamabad CIIT Virtual Campus, CIIT COMSATS Institute.
Chap 3 – PHP Quick Start COMP RL Professor Mattos.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 2 Input, Processing, and Output.
Tcl and Otcl Tutorial Part II Internet Computing KUT Youn-Hee Han.
ASP.NET Programming with C# and SQL Server First Edition Chapter 3 Using Functions, Methods, and Control Structures.
Input, Output, and Processing
Introduction to Bash Programming Ellen Zhang. Previous three classes What have we learnt so far ?
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.
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.
PHP Constructs Advance Database Management Systems Lab no.3.
Data TypestMyn1 Data Types The type of a variable is not set by the programmer; rather, it is decided at runtime by PHP depending on the context in which.
Revision Lecture Mauro Jaskelioff. AWK Program Structure AWK programs consists of patterns and procedures Pattern_1 { Procedure_1} Pattern_2 { Procedure_2}
_______________________________________________________________________________________________________________ PHP Bible, 2 nd Edition1  Wiley and the.
Introducing Python CS 4320, SPRING Lexical Structure Two aspects of Python syntax may be challenging to Java programmers Indenting ◦Indenting is.
Python Primer 1: Types and Operators © 2013 Goodrich, Tamassia, Goldwasser1Python Primer.
JavaScript, Fourth Edition
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.
Shell Scripting Lab 2 Advanced Operating System Spring Advanced Operating System - Spring Lab 2.
Programming Using Tcl/Tk Week 2 Seree Chinodom
1 Lecture 9 Shell Programming – Command substitution Regular expressions and grep Use of exit, for loop and expr commands COP 3353 Introduction to UNIX.
A Few More Functions. One more quoting operator qw// Takes a space separated sequence of words, and returns a list of single-quoted words. –no interpolation.
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.
General Computer Science for Engineers CISC 106 Lecture 12 James Atlas Computer and Information Sciences 08/03/2009.
Open Source Server Side Scripting ECA 236 Open Source Server Side Scripting PHP Basics.
Introduction to Programming with Tcl. Introduction Using two languages  C++ for “ data ”  Otcl for control Tcl has been widely used as a scripting language.
Interpolation Variable Interpolation, Backslash Interpolation.
Introduction to Bash Shell. What is Shell? The shell is a command interpreter. It is the layer between the operating system kernel and the user.
MIT-AITI: Functions Defining and Invoking Functions Functions as Data Function Scope: The call Object Function Arguments: The arguments objects Function.
Scripting.
Shell Scripting September 27, 2004 Class Meeting 6, Part II * Notes adapted by Lenwood Heath from previous work by other members of the CS faculty at Virginia.
Chapter 4.  Variables – named memory location that stores a value.  Variables allows the use of meaningful names which makes the code easier to read.
1 Agenda  Unit 7: Introduction to Programming Using JavaScript T. Jumana Abu Shmais – AOU - Riyadh.
Radoslav Georgiev Telerik Corporation
Nested Loops CS303E: Elements of Computers and Programming.
An Introduction To Tcl Scripting John Ousterhout Sun Microsystems Laboratories Tcl/Tk Tutorial, Part II.
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.
TCL Training Joshi Sravan Kumar K 21-Nov-2011.
Shell Scripting March 1st, 2004 Class Meeting 7.
Programming Using Tcl Maxwell Created By: Quan Nguyen Created Date: 12/12/2016 TCL Training1.
TCL/TK Tool Command Language/Tool Kit.
CHAPTER 8 (skip , ) CHAPTER 10
Statements, Comments & Simple Arithmetic
Perl Variables: Array Web Programming.
NS-2 LAB: TCL Miriam Allalouf Semester A.
Introduction to C++ Programming
Python Primer 1: Types and Operators
C Programming Getting started Variables Basic C operators Conditionals
Comments Any string of symbols placed between the delimiters /* and */. Can span multiple lines Can’t be nested! Be careful. /* /* /* Hi */ is an example.
Presentation transcript:

Writing Tcl Scripts Outline Goal Reading Syntax Data Types Control Structures Variable Scoping Goal Understand Tcl syntax, data types, control structures, scoping Reading Ch. 10-13, Practical Programming in Tcl and Tk

Basics Tcl script = Tcl command = Examples sequence of commands commands separated by newlines, semicolons # in place of command is comment line Tcl command = one or more words separated by spaces first word is command name, others are arguments cmd arg1 arg2 arg3 . . . Examples set a 22; set b 33 set a 22 set b 33

Division of Responsibility Command Chops command into words, makes substitutions. Does not interpret values of words. Tcl Parser Words Command Procedure Interprets words, produces string result. Result

Arguments 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” 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

Variable Substitution Syntax: $varName Variable name is letters, digits, underscores May occur anywhere within a word Sample command Result set b 66 66 set a b b set a $b 66 set a $b+$b+$b 66+66+66 set a $b.3 66.3 set a $b4 no such variable

Command Substitution Syntax: [script] Execute script, substitute result May occur anywhere within a word Sample Command Result set b 8 8 set a [expr $b+2] 10 set a “b-3 is [expr $b-3]” b-3 is 5

Controlling Word Structure Words break at white space and semicolons except: double-quotes prevent breaks set a “This is one big word with spaces” curly braces prevent breaks and substitutions set a {nested {} braces} backslashes quote special characters set a word\ with\ \$\ and\ space Substitutions do not change word structure set a “two words” - a is one word set b $a - b is still one word

Syntax Summary Script = commands separated by newlines, semicolons Command = words separated by white space # at beginning of command is comment $ causes variable substitution [ ] causes command substitution Only one level of substitution done by parser “ “ quotes white space and semicolons still do substitutions \ quotes next character also provides some special characters like \n, \t, etc. { } quotes all characters - no substitutions

Expressions C-like (int and double) plus strings Used in expr, conditional commands Commands perform substitutions within expression arguments can and should put braces around arguments Sample command Result set b 5 5 expr ($b*4)-3 17 expr $b<=2 0 expr {$b * [fac 4]} 120 set a Bill Bill expr {$a < “Anne”} 0

Lists Zero or more elements separated by white space red green blue Braces and backslashes for grouping zero one {twoA twoB twoC} three one\ word two three List-related commands concat, foreach, lappend, lindex, linsert, list, llength, lrange, lreplace, lsearch, lsort Example lindex {a b {c d e} f} 2 returns: c d e Lists parse as commands: each element becomes one word

Associative Arrays Syntax Strings as array indices arrayname(index) Strings as array indices set x(fred) 44 set x(2) [expr $x(fred)+6] result: 50 Array command - searching, size queries array names x returns: fred 2 foreach i [array names x] { set x($i) 0 }

Control Structures C-like appearance Just commands that take Tcl scripts as arguments Example if {$x < 3} { puts stdout “x is too small!” set x 3 } Commands if, for, foreach, while, case, break, continue, eval

Control Structures (cont.) Should usually put braces around control command arguments set i 0 for {set i 0} {$i < 10} {incr i 3} { lappend aList $i } set aList => 0 3 6 9 If no braces, then $i < 10 --> 0 < 10, always true, infinite loop

Procedures proc command defines procedure proc sub1 x {expr $x-1} Procedures behave just like built-in commands sub1 3 returns: 2 Arguments can have defaults proc decr {x {y 1}} {expr $x-$y} Can have variable number of arguments proc foo {a b args} { . . . } name body list of argument names gets list of extra args

Variable Scope Variables in proc are local by default Must explicitly reference global variables global command makes global variables locally accessible only in procedures where it appears proc foo a { global x set x $a } proc bar a { puts stdout “x is $x” foo 5 bar 3 => x is 5