Scripti ng LanguagesStephen A. EdwardsColumbia UniversityFall 2008Scripti ng LanguagesHigher-level languages a.k.a. dynamic languages.More compact for small programs.Often not suitable for large systems.The more popular ones:◮Awk◮Perl◮Python◮PHP◮Tcl◮Bourne ShellAwkNamed for its three developers:Alfred AhoPeter WeinbergerBrian KernighanGood for data file manipulation. Iuse it for computing grades a ndother 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, runseach action whose pattern matches.Patterns:BEGIN, END True before and after the file.expression Condition/regular expression/ String pattern ma tchpattern && patternpattern || pattern Boolean operators! patternAwk One-LinersPrint every line{ print }Print the first a nd third fields of e ach line{ print $1, $3 }Print every line with three fieldsNF == 3 { print }Print a line number before every line{ print NR, $0 }Statistics in Aw k#!/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) / nprint 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 a random language reference manual produces103 the58 of51 is49 and49 a35 expression32 The29 =PerlLarr y Wall’sPractical Extraction and Report LanguageorPathologically Eclectic Rubbish ListerLarger, more flexible language thanAwk. Good for text processing andother tasks. Strange semantics.Henious syntax.Excellent regular-expression support.More complicated data structurespossible (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 m any. 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 m ost popular scripting language.Despite its flaws, it’s very powerful.Almost has a good type system.Ve ry few things can’t be done in Perl.Fast, flexible interpreter.Ability to make virtually every Unix system call. Binary datamanipulation.Ported everywhere.Ve ry, very extensive collection of libraries. Database access.CGI/HTML for the web. Math. IPC. Time.PythonPerl designed by a sane man(Guido van Rossum).Ve ry clean syntax andsemantics.Large collection of libraries.Regular expression support(but not as integrated as Perl’s.)Wordcount in P ython#!/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; Classes◮Persistent 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, m ail/Mime, HTML);◮Tk;◮Cryptography;◮System-specific (Windows, Mac, SGI, POSIX)Python vs. PerlPython can be the more verbose language, but Perl can becryptic.Regular expression support more integrated with language inPerl.Perl better-known.Probably comparable e xe cution speeds.More “tricks” possible in Perl; Python more disciplined.Python has the much cleaner syntax and semantics; I knowwhich language’s programs I’d rather maintain.Larr y Wall and Guido van Rossum are not who you think theyshould be. See Lee Gomes, Two Men, Two Ways To SpeakComputerese And Two Big Successes, The Wall Street Journal,July 21, 2003, page B1.PHPA left-recursive acronym: “PHP: Hypertext Processor”Designed for generating dynamic web pages and applications,especially those connected to a database (e.g., MySQL).“Server-side scripting.”Really widespread and popular. Huge number of libraries(interfaces to every database in the universe, PDF generation,graphics, etc.)The PHP ElephpantHello World i n PHP<?phpecho "Hello World!\n";?>Text outside <?php ?> pairs are copied verbatim to the output(typically HTML).Agnostic comment syntax:/*C-stylemultiline*/// C++-style single-line# shell-style single-lineWordcount in PHP<?phpwhile (!feof(STDIN)) {$line = fgets(STDIN);$line = preg_replace("/[.;:;!(){}\n]/","",$line);$words = explode(" ", $line);foreach ($words as $word)if ($word != "")$count[$word] += 1;}arsort($count);foreach ($count as $word => $count)echo "$count $word\n";?>TclJohn Ousterhout’s Tool CommandLanguage was originally intended to begrafted on to an application to ma ke itcontrollable.It has become a general-purpose scriptinglanguage. Its syntax is simple, althoughrather atypical for a programminglanguage.Tk, a Tcl package, provide graphical user interface widgets.Tcl/Tk m ay be the easiest way to write a GUI.Tk has been connected to
View Full Document