CptS 355 Programming Language Design - PowerPoint PPT Presentation

1 / 12
About This Presentation
Title:

CptS 355 Programming Language Design

Description:

Page 3 Roger Ray, CptS 355, WSU Vancouver. Summary. Text manipulation ... idiom for reading stdin lines: while ( STDIN variable $_ has the line just read ... – PowerPoint PPT presentation

Number of Views:90
Avg rating:3.0/5.0
Slides: 13
Provided by: roge2
Category:

less

Transcript and Presenter's Notes

Title: CptS 355 Programming Language Design


1
CptS 355Programming Language Design
Instant Perl
  • Roger Ray
  • WSU Vancouver, Spring 2001

instant-perl.ppt
2
Contents
  • Summary
  • Examples
  • Reference booklet

3
Summary
  • Text manipulation language
  • C-like syntax
  • Interpretive semantics
  • variables implicitly declared
  • scoping is dynamic
  • implicit compilation
  • Ad-hoc widely used
  • Use Perl in lab, or download it for Windows
  • e.g. http//www.perl.com/CPAN/src/stable.zip

a hammer!
4
Contents
  • Summary
  • Examples
  • Reference booklet

5
Hello, World
  • Version 1
  • Version 2
  • Version 3
  • Operation
  • source file interpreted by "perl"
  • ".pl" suffix is common convention
  • "perl c hello.pl" checks w/o execution
  • "perl w hello.pl" runs with warnings on
  • Syntax
  • C-like
  • all statements end with ""
  • "" for comments
  • Variables
  • mark with ""
  • implicitly declared dynamically scoped
  • value is either text or number
  • interpolated into strings (like Unix Shell)

hello.pl
print "Hello, world!\n"
perl hello.pl Hello, world!
msg "Hello, world!\n" print msg
hello.pl ... msg "Hello, world!" print
"msg\n"
6
Print Temperatures
perl c2f.pl 10 110 C -10 F 14 C 0 F
32 freezing! C 10 F 50 C 20 F 68 C
30 F 86 C 40 F 104 C 50 F 122 C 60
F 140 C 70 F 158 C 80 F 176 C 90
F 194 C 100 F 212 boiling! C 110 F 230
Print C to F conversions
c2f.pl
if (ARGV ! 1) print "usage perl c2f.pl
min-C max-C\n" exit (1) min
ARGV0 max ARGV1 c min while (c
lt max) f 180c/100 32 print "C
c\tF f" if (c 100) print "\t
boiling!\n" elsif (c 0) print "\t
freezing!\n" else print "\n" c
c 10
  • Command line
  • command line args follow script name
  • first arg ARGV0
  • last arg ARGVARGV
  • ARGV is 1, if no args at all
  • Tests
  • numeric , !, lt, etc.
  • textual eq, ne, lt, le, ...
  • and short-circuit
  • note the elsif

7
Select Lines
perl mary.pl lt poem Mary had a little lamb, And
everywhere that Mary went
Print stdin lines that contain "Mary" or "mary"
poem
Mary had a little lamb, Its fleece was white as
snow And everywhere that Mary went The lamb was
sure to go.
mary.pl
  • I/O
  • idiom for reading stdin lines while (ltSTDINgt
  • variable _ has the line just read
  • includes trailing newline
  • Chop
  • removes trailing newline from _
  • Match
  • matches string against pattern
  • pattern is Unix-like regular expressions

while (ltSTDINgt) chop line _ if
(line /mMary/) print "line\n"
8
Construct Web Page, 1
export DATE"(date)" export
WHERE"WSU-V" perl web1.pl gt now.htm
Build a web page from environment variables
now.htm
lthtmlgt ltbodygt lth1gtThe time at WSU-V is Tue Apr 17
162438 PDT 2001lt/h1gt lt/bodygt lt/htmlgt
web1.pl
dateENV"DATE" whereENV"WHERE" print
ltltEOF lthtmlgt ltbodygt lth1gtThe time at where is
datelt/h1gt lt/bodygt lt/htmlgt EOF
  • Environment variables
  • ENV"DATE" returns env var DATE value
  • just like C's getenv ("DATE")
  • Print
  • ltltXYZ means take all following lines upto XYZ
  • treat the lines as a single string
  • interpolation happens as usual
  • note the semicolon on the print

9
Construct Web Page, 2
excel who.xls perl web2.pl lt who.txt
Build web pages from Excel file
page1.htm
lthtmlgt ltbodygt To John Smith ltbrgt Your bill is
overdue! lt/bodygt lt/htmlgt
who.xls
save as tab delimited
who.txt
John Smith Jane Doe Charlie Brown
web2.pl
page2.htm
... see next slide ...
lthtmlgt ltbodygt To Jane Doe ltbrgt Your bill is
overdue! lt/bodygt lt/htmlgt
page3.htm
lthtmlgt ltbodygt To Charlie Brown

10
Construct Web Page, 2
excel who.xls perl web2.pl lt who.txt
Build web pages from Excel file
web2.pl
page1.htm
n 0 while (ltSTDINgt) chop (first,
last) split(/\t/, _) if (n gt 0)
close (HTM) n open(HTM,
"gtpagen.htm") print HTM ltltEOF lthtmlgt ltbodygt T
o first last ltbrgt Your bill is
overdue! lt/bodygt lt/htmlgt EOF
lthtmlgt ltbodygt To John Smith ltbrgt Your bill is
overdue! lt/bodygt lt/htmlgt
who.txt
John Smith Jane Doe Charlie Brown
  • Split
  • breaks string into an array per a pattern
  • (var0, var1, var2) array is shorthand for
    var0 array0 var1 array1 ....
  • Open
  • set file handle XYZ open (XYZ, "gtname")
  • "gtname" means open "name" for output
  • Print
  • print "text" goes to stdout
  • print XZY "text" uses XYZ file handle

11
Use a Library
perl table.pl 10 30 C -10 F 14 C 0 F
32 freezing! C 10 F 50 C 20 F 68 C
30 F 86
Print C to F conversions, again
require "lib.pl" load library if (ARGV !
1) print "usage table min-C max-C\n" exit
(1) (min, max) _at_ARGV _at_ARGV is whole
array c min while (c lt max) f
fahrenheit (c) call sub print (c, f)
call sub c c 10
table.pl
lib.pl
sub fahrenheit local (c) _at__ get
parms 180c/100 32 final result sub
print local (c, f) _at__ get parms print
"C c\tF f" if (c 100) print "\t
boiling!\n" elsif (c 0) print "\t
freezing!\n" else print "\n" 1
make require happy
  • _at_ marks variable as a whole array
  • whole array assignment _at_v (10, 12, 13)
  • first element v0
  • get elements (x, y, z) _at_v
  • marks subroutine calls
  • local introduces new names in a scope
  • array of actual parameters _at__
  • create new names local (x, y, z)
  • array assignment local(x, y, z) _at__

12
Contents
  • Summary
  • Examples
  • Reference booklet

The booklet is from "Programming Perl", Larry
Wall Randall Schwartz, O'Reilly Associates,
Inc.
Write a Comment
User Comments (0)
About PowerShow.com