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.

Slides:



Advertisements
Similar presentations
JavaScript I. JavaScript is an object oriented programming language used to add interactivity to web pages. Different from Java, even though bears some.
Advertisements

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.
IntroductionIntroduction  Computer program: an ordered sequence of statements whose objective is to accomplish a task.  Programming: process of planning.
ISBN Regular expressions Mastering Regular Expressions by Jeffrey E. F. Friedl –(on reserve.
Bellevue University CIS 205: Introduction to Programming Using C++ Lecture 3: Primitive Data Types.
CS Lecture 03 Outline Sed and awk from previous lecture Writing simple bash script Assignment 1 discussion 1CS 311 Operating SystemsLecture 03.
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.
Tcl/TK Tutorial Fan Yang Kristy Hollingshead
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie June 27, 2005.
Writing Tcl Scripts Outline Goal Reading Syntax Data Types
Tcl/Tk 2 CS 414, Software Engineering I Mark Ardis Rose-Hulman Institute December 5, 2002.
Guide To UNIX Using Linux Third Edition
JavaScript, Third Edition
Java™ How to Program, 9/e © Copyright by Pearson Education, Inc. All Rights Reserved.
 2004 Prentice Hall, Inc. All rights reserved. Chapter 25 – Perl and CGI (Common Gateway Interface) Outline 25.1 Introduction 25.2 Perl 25.3 String Processing.
Innovation Intelligence ® 1 Chapter 1: Introduction to TCL.
Introduction to Python
SYST Web Technologies SYST Web Technologies Lesson 6 – Intro to JavaScript.
An Introduction to Unix Shell Scripting
Chap 3 – PHP Quick Start COMP RL Professor Mattos.
2440: 211 Interactive Web Programming Expressions & Operators.
JAVA Tokens. Introduction A token is an individual element in a program. More than one token can appear in a single line separated by white spaces.
20-753: Fundamentals of Web Programming 1 Lecture 12: Javascript I Fundamentals of Web Programming Lecture 12: Introduction to Javascript.
Tcl/TK Tutorial. 2 Learning Tcl/TK What is Tcl/TK? –An interpreted programming language Build on-the-fly commands, procedures Platform-independent Easy.
Perl Language Yize Chen CS354. History Perl was designed by Larry Wall in 1987 as a text processing language Perl has revised several times and becomes.
SE-1010 Dr. Mark L. Hornick 1 Variables & Datatypes.
_______________________________________________________________________________________________________________ PHP Bible, 2 nd Edition1  Wiley and the.
Basic Java Syntax COMP 401, Spring 2014 Lecture 2 1/14/2014.
JavaScript Scripting language What is Scripting ? A scripting language, script language, or extension language is a programming language.
Copyright © 2003 ProsoftTraining. All rights reserved. Perl Fundamentals.
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.
Introduction to Python Dr. José M. Reyes Álamo. 2 Three Rules of Programming Rule 1: Think before you program Rule 2: A program is a human-readable set.
Programming Fundamentals. Overview of Previous Lecture Phases of C++ Environment Program statement Vs Preprocessor directive Whitespaces Comments.
Dongsoo S. Kim  Tcl: Tool Command Language Interpreted programming (scripting) language Build on-the-fly commands and procedures Embeddable.
C++ for Engineers and Scientists Second Edition
Open Source Server Side Scripting ECA 236 Open Source Server Side Scripting PHP Basics.
JavaScript. JavaScript Introduction JavaScript is the world's most popular programming language. It is the language for HTML and the web, for servers,
Tokens in C  Keywords  These are reserved words of the C language. For example int, float, if, else, for, while etc.  Identifiers  An Identifier is.
Introduction to Programming with Tcl. Introduction Using two languages  C++ for “ data ”  Otcl for control Tcl has been widely used as a scripting language.
2: Basics Basics Programming C# © 2003 DevelopMentor, Inc. 12/1/2003.
Scripting.
PHP Tutorial. What is PHP PHP is a server scripting language, and a powerful tool for making dynamic and interactive Web pages.
1 Agenda  Unit 7: Introduction to Programming Using JavaScript T. Jumana Abu Shmais – AOU - Riyadh.
An Introduction To Tcl Scripting John Ousterhout Sun Microsystems Laboratories Tcl/Tk Tutorial, Part II.
Perl & TCL Vijay Subramanian, Modified from : perl_basics_06.ppt.
TCL Training Joshi Sravan Kumar K 21-Nov-2011.
Definition of the Programming Language CPRL
CSC 352– Unix Programming, Spring 2016, Final Exam Guide
Data types Data types Basic types
Server-Side Application and Data Management IT IS 3105 (Spring 2010)
PHP Introduction.
Programming Using Tcl Maxwell Created By: Quan Nguyen Created Date: 12/12/2016 TCL Training1.
C++ for Engineers and Scientists Second Edition
TCL/TK Tool Command Language/Tool Kit.
CET 3640 – Lecture 2 Java Syntax Chapters 2, 4, 5
Character Set The character set of C represents alphabet, digit or any symbol used to represent information. Types Character Set Uppercase Alphabets A,
NS-2 LAB: TCL Miriam Allalouf Semester A.
Introduction to C++ Programming
Lecture 2 Python Programming & Data Types
PHP.
T. Jumana Abu Shmais – AOU - Riyadh
Lecture 2 Python Programming & Data Types
Python Primer 1: Types and Operators
C Programming Getting started Variables Basic C operators Conditionals
The C Language: Intro.
Presentation transcript:

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  Intended to be embedded with application.  But used in rapid prototyping, scripting, GUI, testing of HW and SW. u Platform-independent u All operations are commands, including language structures.commands u Everything can be dynamically redefined and overridden. u All data types can be manipulated as strings, including code.data typesstringscode u Need little experience with programming –Easy –Programs are short, efficient u Be willing to learn something new

What is Tcl/Tk? u Tcl –a scripting language –can be extended in C (but this is harder) –ugly but simple u Tk –a simple but powerful widget set –Hello World: a complete program that exits when a person presses the button  grid [ button.myButton -text "Hello World" -command exit ] u Simple things are simple, hard things are possible

Tcl Language Programming There are two parts to learning Tcl: 1. Syntax and substitution rules: –Substitutions simple (?). 2. Built-in commands: –Can learn individually as needed. –Control structures are commands, not language syntax.

Scripts and Commands 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 myName Saul puts "My Name is $myName” set class CPSC-481; puts -nonewline $class

Arguments u Parser assigns no meaning to arguments (quoting by default, evaluation is special): set x 4 x is "4 " set y x+10 y is "x+10” set z $x+10 z is "4+10” u Different commands assign different meanings to their arguments. “Type-checking” must be done by commands themselves. expr 24/3 arg is math expresson -> 8 eval "set a 122" evaluate argument as a command button.b -text Hello -fg red some args are options (the -) string length Abracadabra some args are qualifiers (length)

Operators u Unary (- + ~ !) : –Unary minus, unary plus, bit-wise NOT, logical NOT u + - * / % : –Add, Subtract, Multiply, divide, remainder u ** : –Exponentiation u > : –Left and right (bit) shift u Comparison operators –Numbers : = –String : eq ne in ni (string compare str1 str2) u Bitwise (& | ^ ~): –Bitwise and, or, exclusive or, not u Logical (&& || !): –Logical and, or, not

Variable Substitution  Syntax: $ varName u Variable name is letters, digits, underscores. –This is a little white lie, actually. 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

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

Controlling Word Structure u Words break at white space and semi-colons, except: –Double-quotes prevent breaks: set a 4; set y 5 set a "x is $x; y is $y" -> x is 4; y is 5 –Curly braces prevent breaks and substitutions: set a {[expr $b*$c]} -> [expr $b*$c] –Backslashes quote special characters: set a word\ with\ \$\ and\ space -> word with $ and space

Controlling Word Structure (continued) –Backslashes can escape newline (continuation)  set aLongVariableNameIsUnusual \ “This is a string” -> This is a string –Substitutions don't change word structure:  set a "two words" set b $a -> two words

Comments u The # is the comment command u Tcl parsing rules apply to comments as well set a 22; set b 33 <- OK # this is a comment <- OK set a 22 # same thing? <- Wrong! set a 22 ;# same thing <- OK

Summary of Tcl Command Syntax u Command: words separated by whitespace u First word is a function, others are arguments u Only functions apply meanings to arguments u Single-pass tokenizing and substitution u $ causes variable interpolation u [ ] causes command interpolation u “” prevents word breaks u { } prevents all interpolation u \ escapes special characters u TCL HAS NO GRAMMAR!

14 Special characters # : single-line comments, similar to "//" in C ;# : in-line comments, just like "//" in C \ : escape character, same function as in C : also used to break a long line of code to two lines $ : get the value of a variable u var : name of variable u $var : value of variable [] : evaluate command inside brackets

Tcl Expressions u Arguments are interpretted as expressions in some commands: expr, if,... Sample commandResult set b 55 expr ($b*4) expr $b <= 20 expr {$b * cos(4)}-3.268… u Some Tcl operators work on strings too (but safer to use the string compare command) set a BillBill expr {$a < "Anne"}0 expr {$a < "Fred"}1

Tcl Arrays u Tcl arrays are 'associative arrays': index is any string –set foo(fred) 44 ;# 44 –set foo(2) [expr $foo(fred) + 6] ;# 50 –array names foo ;# fred 2 u You can 'fake' 2-D arrays: set A(1,1) 10 set A(1,2) 11 array names A => 1,1 1,2 (commas included in names!)

Lists u Zero or more elements separated by white space: set colors {red green blue} u Braces and backslashes for grouping: set hierarchy {a b {c d e} f}) set two_item_list {one two\ two} u List-related commands: concatlindexllengthlsearch foreachlinsertlrangelsort lappendlistlreplace  Note: all indices start with 0. end means last element u Examples: lindex {a b {c d e} f} 2 í c d e lsort {red green blue} í blue green red

String Manipulation u String manipulation commands: regexpformatsplitstring regsubscanjoin  string subcommands compare first last index length match range toupper tolower trim trimleft trimright  Note: all indexes start with 0. end means last char  string tolower "THIS" ;# this  string trimleft “XXXXHello” ;# Hello  string index “abcde” 2 ;# c

Control Structures u C-like in appearance. u Just commands that take Tcl scripts as arguments. u Commands: ifforswitchbreak foreachwhileevalcontinue

if else set x 2 if {$x < 3} { puts "x is less than 3" } else { puts "x is 3 or more" }

while #list reversal set a {a b c d e} set b "” set i [expr [llength $a] - 1] while {$i >= 0} { lappend b [lindex $a $i] incr i -1 } puts $b

for and foreach for {set i 0} {$i<10} {incr i} { puts $I } foreach color {red green blue} { puts “I like $color” } set A(1) a; set A(2) b; set A(26) z foreach index [array names A] { puts $A($index) }

switch set pete_count 0 set bob_count 0 set other_count 0 foreach name {Peter Peteee Bobus Me Bobor Bob} { switch -regexp $name { ^Pete* {incr pete_count} ^Bob|^Robert {incr bob_count} default {incr other_count} } puts "$pete_count $bob_count $other_count"

Procedures  proc command defines a procedure: proc decrement {x} { expr $x-1 } u Procedures behave just like built-in commands: decrement 3 í 2 u Arguments can have default values: proc decrement {x {y 1}} { expr $x-$y } decrement ;# 95 decrement 100 ;# 99 name list of argument names body

Procedures u Procedures can have a variable number of arguments proc sum args { set s 0 foreach i $args { incr s $i } return $s } sum í 15 sum í 0

Procedures u procedure calls (embedded commands) set b [expr $a + 5] puts "The value of b is $b" u create your own procedure (called by value only) proc foo {a b c} { return [expr $a * $b - $c] } puts [expr [foo 2 3 4] + 5] proc bar { } { puts "I'm in the bar procedure" } bar

Procedures and Scope u Scoping: local and global variables. –Interpreter knows variables by their name and scope –Each procedure introduces a new scope  global procedure makes a global variable local set outside "I'm outside" set inside "I'm really outside" proc whereAmI {inside} { global outside puts $outside puts $inside } whereAmI "I wonder where I will be " -> I'm outside I wonder where I will be

Tcl File I/O u Tcl file I/O commands: opengetsseekflushglob closereadtellcd fconfigure fblocked fileevent putssourceeofpwdfilename u File commands use 'tokens' to refer to files set f [open "myfile.txt" "r"] => file4 puts $f "Write this text into file" close $f

Tcl File I/O u gets and puts are line oriented set x [gets $f] reads one line of $f into x  read can read specific numbers of bytes read $f 100 => (up to 100 bytes of file $f)  seek, tell, and read can do random-access I/O set f [open "database" "r"] seek $f 1024 read $f 100 => (bytes of file $f)

Libraries&packages u What is tcllib? u Tcllib is a collection of utility modules for Tcl. These modules provide a wide variety of functionality, from implementations of standard data structures to implementations of common networking protocols. The intent is to collect commonly used function into a single library, which users can rely on to be available and stable.

u A Tcl library is simply a directory containing Tcl scripts that are sourced at startup by a virtual server u Each Tcl file in a library often contains one or more calls to ns_register_proc, ns_schedule_proc, or ns_register_filter to bind a script to a specific URL

packages u A package in Tcl can be a Tcl script (*.tcl), a loadable binary (*.so), or a combination of the two. Examples of the first are xml.tcl and sgml.tcl u Tcl provides a command called pkg_mkIndex, which can build an index file to enable the automatic loading of packages via the "package require" command.