Unit vii wp ppt

616 views

Published on

web Programming UNIT VII notes

0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
616
On SlideShare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
16
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Unit vii wp ppt

  1. 1. Programming Using Tcl/TkThese slides are based upon several Tcl/Tk text books material byDr. Ernest J. Friedman-Hill
  2. 2. What you’ll needPCs in the Computer Science Lab have it installed – Start / Tcl / Wish– Start / Widget tourOr install it on your own computer– Windows & Macintosh: free binaries available– Most Unix: source availableDocumentation • books can be bought (bookstore, etc) • books in the PC lab– up-to-date man pages on-line • Start / Help
  3. 3. What is Tcl/Tk?Tcl – a scripting language– can be extended in C (but this is harder)– ugly but simpleTk – 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 ]Simple things are simple, hard things are possible
  4. 4. Tcl Language ProgrammingThere are 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.
  5. 5. Scripts and CommandsTcl script = – Sequence of commands. – Commands separated by newlines, semi-colons.Tcl command = – One or more words separated by white space. – First word is command name, others are arguments. – Returns string result.Examples:set myName Saulputs "My Name is $myName”set class CPSC-481; puts -nonewline $class
  6. 6. ArgumentsParser assigns no meaning to arguments (quoting bydefault, 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”Different commands assign different meanings to theirarguments. “Type-checking” must be done by commandsthemselves.expr 24/3 arg is math expresson -> 8eval "set a 122" evaluate argument as a commandbutton .b -text Hello -fg red some args are options (the -)string length Abracadabra some args are qualifiers (length)
  7. 7. Variable SubstitutionSyntax: $varNameVariable name is letters, digits, underscores. – This is a little white lie, actually.May occur anywhere in 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
  8. 8. Command SubstitutionSyntax: [script]Evaluate 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
  9. 9. Controlling Word StructureWords 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
  10. 10. Controlling Word Structure (continued)– Backslashes can escape newline (continuation) • set aLongVariableNameIsUnusual “This is a string” -> This is a string– Substitutions dont change word structure: • set a "two words" set b $a -> two words
  11. 11. CommentsThe # is the comment commandTcl 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
  12. 12. Summary of Tcl Command SyntaxCommand: words separated by whitespaceFirst word is a function, others are argumentsOnly functions apply meanings to argumentsSingle-pass tokenizing and substitution$ causes variable interpolation[ ] causes command interpolation“” prevents word breaks{ } prevents all interpolation escapes special charactersTCL HAS NO GRAMMAR!
  13. 13. Tcl ExpressionsArguments are interpretted as expressions in somecommands: expr, if, ... Sample command Result set b 5 5 expr ($b*4) - 3 17 expr $b <= 2 0 expr {$b * cos(4)} -3.268…Some Tcl operators work on strings too(but safer to use the string compare command) set a Bill Bill expr {$a < "Anne"} 0 expr {$a < "Fred"} 1
  14. 14. Tcl ArraysTcl 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 2You can fake 2-D arrays:set A(1,1) 10set A(1,2) 11array names A=> 1,1 1,2 (commas included in names!)
  15. 15. ListsZero or more elements separated by white space: set colors {red green blue}Braces and backslashes for grouping: set hierarchy {a b {c d e} f}) set two_item_list {one two two}List-related commands: concat lindex llength lsearch foreach linsert lrange lsort lappend list lreplaceNote: all indices start with 0. end means last elementExamples:lindex {a b {c d e} f} 2 c d elsort {red green blue} blue green red
  16. 16. String ManipulationString manipulation commands:regexp format split stringregsub scan joinstring subcommandscompare first last index lengthmatch range toupper tolower trimtrimleft trimrightNote: all indexes start with 0. end means last char • string tolower "THIS" ;# this • string trimleft “XXXXHello” ;# Hello • string index “abcde” 2 ;# c
  17. 17. Control StructuresC-like in appearance.Just commands that take Tcl scripts as arguments.Commands: if for switch break foreach while eval continue
  18. 18. if elseset x 2if {$x < 3} { puts "x is less than 3"} else { puts "x is 3 or more"}
  19. 19. while#list reversalset 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
  20. 20. for and foreachfor {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) zforeach index [array names A] { puts $A($index)}
  21. 21. switchset pete_count 0set bob_count 0set other_count 0foreach 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"
  22. 22. Procedures proc command defines a procedure: proc decrement {x} {name expr $x-1 body } list of argument namesProcedures behave just like built-in commands: decrement 3 2Arguments can have default values: proc decrement {x {y 1}} { expr $x-$y } decrement 100 5 ;# 95 decrement 100 ;# 99
  23. 23. ProceduresProcedures 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 15sum 0
  24. 24. Procedures and ScopeScoping: local and global variables. – Interpreter knows variables by their name and scope– Each procedure introduces a new scopeglobal procedure makes a global variable localset outside "Im outside"set inside "Im really outside"proc whereAmI {inside} { global outside puts $outside puts $inside}whereAmI "I wonder where I will be"-> Im outside I wonder where I will be
  25. 25. Tcl File I/OTcl file I/O commands:open gets seek flush globclose read tell cdfconfigure fblocked fileeventputs source eof pwd filenameFile commands use tokens to refer to filesset f [open "myfile.txt" "r"]=> file4puts $f "Write this text into file"close $f
  26. 26. Tcl File I/Ogets and puts are line orientedset x [gets $f] reads one line of $f into xread can read specific numbers of bytesread $f 100=> (up to 100 bytes of file $f)seek, tell, and read can do random-access I/Oset f [open "database" "r"]seek $f 1024read $f 100=> (bytes 1024-1123 of file $f)
  27. 27. Tcl Network I/Osocket creates a network connectionset f [socket www.sun.com 80]fconfigure $f -buffering lineputs $f "GET /"puts [read $f 100]close $f=> The 1st 100 characters from Suns home pageNetwork looks just like a file!To create a server socket, just usesocket -server accept portno

×