Download presentation
Presentation is loading. Please wait.
Published byQuan Nguyen Modified over 6 years ago
1
Programming Using Tcl Maxwell Created By: Quan Nguyen Created Date: 12/12/2016 TCL Training1
2
What you’ll need u TCL is a scripting language u Start Maxwell and open the Console TCL Training2
3
Basic TCL 3
4
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 TCL Training4
5
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+$b66+66+66 set a $b.366.3 set a $b4 no such variable TCL Training5
6
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 Training6
7
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 TCL Training7
8
Summary of Tcl Command Syntax u Command: words separated by whitespace u First word is a function, others are arguments u $ causes variable interpolation u [ ] causes command interpolation u “” prevents word breaks u \ escapes special characters u TCL HAS NO GRAMMAR! TCL Training8
9
Tcl Expressions u Arguments are interpretted as expressions in some commands: expr, if,... Sample commandResult set b 55 expr ($b*4) - 317 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 Training9
10
Lists u Zero or more elements separated by white space: set colors {red green blue} set list1 {} Set list2 [list a b c] Set list3 {1 2 {3 4}} Set list4 [list 1 2 [list 3 4]] Index 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 TCL Training10
11
11 List Methods Function NameExample lappend lappend {a b} c a b c lindex lindex {a b c} 1 b llength llength {a b c} 3 lrange lrange {a b c d} 0 2 a b c lsearch lsort lsort –decreasing {a b c} c b a lreplace TCL Training
12
String Manipulation u String manipulation commands: split, compare, first, last, index, length, match, 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 TCL Training12
13
TCL Training13 regexp regexp exp string ?matchVar? ?sub? ?sub?.any single character ^matches null string at start of string $matches null string at end of string \xmatches the character x [c1-c2]matches any single character (regexp)matches regexp – used for grouping *matches 0 or more of previous atom +matches 1 or more of previous atom ?matches null string or previous atom r1|r2matches r1 or r2
14
Control Structures u Just commands that take Tcl scripts as arguments. u Commands: ifforswitchbreak foreachwhileevalcontinue TCL Training14
15
if else set x 2 if {$x < 3} { puts "x is less than 3" } else { puts "x is 3 or more" } TCL Training15
16
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 TCL Training16
17
for and foreach for {set i 0} {$i<10} {incr i} { puts $i } foreach color {red green blue} { puts “I like $color” } TCL Training17
18
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" TCL Training18
19
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 100 5 ;# 95 decrement 100 ;# 99 name list of argument names body TCL Training19
20
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 1 2 3 4 5 15 sum 0 TCL Training20
21
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 Training21
22
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 Training22
23
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 1024-1123 of file $f) TCL Training23
24
Error handling u Similar to C++ and Java u catch {command} varName if {[catch {open foo.txt} msg]} { puts $msg } TCL Training24
25
Time and Date u clock seconds –time in seconds (usu. since 1/1/70) u clock format –convert to string –e.g., clock format $t –format "%a, %B %e %Y %H:%M:%S" Thu, April 4 2002 15:00:56 u clock scan dateString –convert date string to integer TCL Training25
26
namespace u namespace eval/import/export namespace eval ::savarti::tcl { namespace import ::savarti::example::* proc proc1 {} { puts “proc in namespace” } ::savarti::tcl::proc1 TCL Training26
27
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!) TCL Training27
28
Maxwell Tcl Reference Manual 28
29
Overview TCL Training29 namespaceUse db::Database Tcl dm::Data Management Tcl de::Design Editing Tcl gi::Graphical Infrastructure Tcl le::Layout Editing Tcl se::Schematic Editing Tcl
30
db:: namespace (1) TCL Training30 procUse db::foreach Provides “foreach” loop control for iterating over dbCollections db::addAttr Adds the specified attribute to the given object. db::setAttr Sets the value of the specified attribute of the given objects db::getAttr Returns the value of the specified attribute of the given object. db::listAttrs Returns the attribute names for the given objec db::getCount Returns the number of objects in the specified collection db::getNext Returns the next object in a collection db::destroy Deletes objects
31
db:: namespace (2) TCL Training31 proc db::getShapesdb::getPrefs db::getTermsdb::createPref db::getViasdb::copyParams db::getInstsdb::createParamDef db::getInstTermsdb::getCallbacks db::getMarkersdb::getParams db::getNetsdb::getParamValue db::getPinsdb::setParamValue db::setInstNamedb::getPrefs db::setMasterdb::createPref
32
dm:: namespace TCL Training32 proc dm::createCelldm::createLib dm::copyCellsdm::copyLib dm::getCellsdm::getLibs dm::moveCellsdm::moveLib dm::createCellView dm::copyCellViews dm::getCellViews dm::moveCellViews
33
de:: namespace TCL Training33 proc de::opende::getFigures de::savede::getLayers de::closede::getLPPs de::copyde::setCursor de::createCommand de::startTransaction de::endTransaction de::getActiveContext de::getActiveEditorWindow
34
gi:: namespace TCL Training34 proc gi::addActionsgi::createRow gi::addMenugi::createTabGroup gi::createActiongi::createTable gi::createBooleanInputgi::createTextInput gi::createColumngi::execDialog gi::createDialoggi::executeAction gi::createFileInputgi::findChild gi::createGroupgi::getCells gi::createLabelgi::getColumns gi::createListInputgi::getDialogs gi::createMenugi::layout gi::createMutexInputgi::prompt gi::createNumberInputgi::setActiveTab
35
le:: namespace TCL Training35 proc le::alignle::createTerm le::convertToPolygonle::createVia le::copyle::flatten le::createInstle::generateShapes le::createPinle::move le::rotate
36
se:: namespace TCL Training36 proc se::alignse::createPolygon se::attachTextse::createRectangle se::checkse::createText se::connectWiresse::createWire se::copyse::createWireName se::createInstse::delete se::createLinese::generateSymbol se::createPinse::move
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.