Scripting LanguagesCOMS W4115Prof. Stephen A. EdwardsFall 2004Columbia UniversityDepartment of Computer ScienceScripting LanguagesHigher-level languages.More compact for small programs.Often not suitable for large systems.The more popular ones:AwkPerlPythonTclBourne ShellAwkNamed for its three developers:Alfred AhoPeter WeinbergerBrian KernighanGood for data file manipulation. I use it for computinggrades and other simple tasks.Simple Awk ProgramInput file. Each line a record. Space-separated fields:employee, pay rate, hours workedBeth 10.0 0Dan 9.75 0Kathy 14.0 10Mark 10.0 20Susie 8.25 18Run on the awk program$3 > 0 { print $1, $2 * $3 }producesKathy 140Mark 200Susie 148.5Simple Awk ProgramBeth 10.0 0Kathy 14.0 103rd fieldz}|{$3 > 0| {z }pattern{ print $1, $2 * $3 }| {z }actionKathy 140Awk Program Structurepattern { action }pattern { action }...awk scans an input file one line at a time and, in order,runs each action whose pattern matches.Patterns:BEGIN, END True before and after the file.expression Condition/regular expression/ String pattern matchpattern && patternpattern || pattern Boolean operators! patternAwk One-LinersPrint every line{ print }Print the first and third fields of each line{ print $1, $3 }Print every line with three fieldsNF == 3 { print }Print a line number before every line{ print NR, $0 }Statistics in Awk#!/bin/awk -fBEGIN { n = 0; s = 0; ss = 0;}NF == 1 { n++; s += $1; ss += $1 * $1; }END {print n " data points"m = (s+0.0) / n; print m " average"sd = sqrt( (ss - n * m * m) / ( n - 1.0))print sd " standard deviation"}Run on15103711gives6 data points6.16667 average3.92003 standard deviationAssociative Arrays: Word Counter{ gsub(/[.,:;!(){}]/, "") # remove punctuationfor ( i = 1 ; i <= NF ; i++ )count[$i]++}END { for (w in count)print count[w], w | "sort -rn"}Run on the Tiger reference manual produces103 the58 of51 is49 and49 a35 expression32 The29 =PerlLarry Wall’sPractical Extraction and Report LanguageorPathologically Eclectic Rubbish ListerLarger, more flexible language than Awk. Good for textprocessing and other tasks. Strange semantics. Henioussyntax.Excellent regular-expression support. More complicateddata structures possible (even classes).Wordcount in Perl#!/usr/bin/perlwhile(<>) {chop;s/[.,:;!(){}]//g;@words = split;foreach (@words) {$count{$_}++;}}open(SORTER, "| sort -nr");foreach (keys %count) {print SORTER$count{$_}, " ", $_,"\n";}Understandable wordcount in Perl#!/usr/bin/perlwhile($line = <>) {chop($line);$line =˜ s/[.,:;!(){}]//g;@words = split(/\s+/, $line);foreach $word (@words) {$count{$word}++;}}open(SORTER, "| sort -nr");foreach $word (keys %count) {print SORTER$count{$word}, " ", $word,"\n";}“There’s more than one way to do it”Perhaps too many. Equivalent ways to print STDIN:while (<STDIN>) { print; }print while <STDIN>print while <>while (defined(($_ = <STDIN>)) { print $_; }for (;<STDIN>;) { print; }print $_ while defined($_ = <STDIN>);Many Perl statements come in prefix and postfix formwhile (...) ...... while ...if (...) ...... if ...... unless ...So Why Perl?Perhaps the most popular scripting language.Despite its flaws, it’s very powerful.Almost has a good type system.Very few things can’t be done in Perl.Fast, flexible interpreter.Ability to make virtually every Unix system call. Binarydata manipulation.Ported everywhere.Very, very extensive collection of libraries. Databaseaccess. CGI/HTML for the web. Math. IPC. Time.PythonPerl designed by a sane man.Very clean syntax and semantics.Large collection of libraries (but not as big as Perl’s).Regular expression support (but not as integrated asPerl’s.)Wordcount in Python#!/usr/bin/env pythonimport fileinput, re, string, oscount = {}for line in fileinput.input():line = re.sub(r’[.,:;!(){}]’,"",line)for word in string.split(line):if not count.has_key(word):count[word] = 1else:count[word] = count[word] + 1f = os.popen("sort -nr",’w’)for word in count.keys():f.write(’%d %s\n’ % (count[word], word) )Python Classesclass Complex:def __init__(self, realpart, imagpart):self.r = realpartself.i = imagpartdef add(self, a):self.r = self.r + a.rself.i = self.i + a.idef p(self):print "%g + %gi" % (self.r,self.i)x = Complex(1,2)y = Complex(2,3)x.p()x.add(y)x.p()Prints1 + 2i3 + 5iPython’s MeritsGood support for programming-in-the-large:Packages with separate namespaces; Exceptions;ClassesPersistent datastructures (pickling)High-level: lists, strings, associative arrays, iteratorsGood collection of libraries:Operating-system access (files, directories, etc.);String manipulation; Curses; Databases; Networking(CGI, HTTP, URL, mail/Mime, HTML); Tk;Cryptography; System-specific (Windows, Mac, SGI,POSIX)Python vs. PerlPython can be the more verbose language, but Perl canbe cryptic.Regular expression support more integrated withlanguage in Perl.Perl better-known.Probably comparable execution speeds.More “tricks” possible in Perl; Python more disciplined.Python has the much cleaner syntax and semantics; Iknow which language’s programs I’d rather maintain.TclJohn Ousterhout’s Tool Command Language wasoriginally intended to be grafted on to an application tomake it controllable.Since become a general-purpose scripting language. Itssyntax is quite simple, although rather atypical for aprogramming language.Tk, a Tcl package, provide graphical user interfacewidgets. Tcl/Tk may be the easiest way to write a GUI.Tk has been connected to Perl and Python as well.Tcl SyntaxShell-like command syntax:command argument argument ...All data is strings (incl. numbers and lists)Macro-like variable substitution:set foo "123 abc"bar 1 $foo 3Command substitution:set foo 1set bar 2puts [eval $foo + $bar]; # Print 3Wordcount in Tcl#!/usr/bin/env tclshwhile {[gets stdin line] >= 0} {regsub -all {[.,:;!(){}]} $line "" lineforeach word $line {if {![info exists count($word)]} {set count($word) 1} else {incr count($word)}}}set f [open "| sort -rn" w]foreach word [array names count] {puts $f "$count($word) $word"}Nifty Tcl FeaturesAssociative arraysset count(Stephen) 1Listslappend foo 1lappend foo 2foreach i $foo { puts $i } ; # print 1 then 2Proceduresproc sum3 {a b c} {return [expr $a + $b + $c]}Tk“Hello World” in Tk.button .b -text "Hello World" -command "exit"pack .bAn Editable GraphAn Editable Graph# Set up the main windowset w .plotcatch destroy $wtoplevel $wwm title $w "Plot Demonstration"wm iconname $w "Plot"positionWindow $wset c $w.c# Text description at toplabel $w.msg -font
View Full Document