DOC PREVIEW
Penn CIT 597 - Perl Lecture

This preview shows page 1-2-3-24-25-26-27-48-49-50 out of 50 pages.

Save
View full document
Premium Document
Do you want full access? Go Premium and unlock all 50 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

Perl Major parts of this lecture adapted from http www scs leeds ac uk Perl start html Jan 13 2019 Why Perl Perl is built around regular expressions REs are good for string processing Therefore Perl is a good scripting language Perl is especially popular for CGI scripts Perl makes full use of the power of UNIX Short Perl programs can be very short Perl is designed to make the easy jobs easy without making the difficult jobs impossible Larry Wall Programming Perl 2 Why not Perl Perl is very UNIX oriented Perl does not scale well to large programs Perl is available on other platforms but isn t always fully implemented there However Perl is often the best way to get some UNIX capabilities on less capable platforms Weak subroutines heavy use of global variables Perl s syntax is not particularly appealing 3 What is a scripting language Operating systems can do many things copy move create delete compare files execute programs including compilers schedule activities monitor processes etc A command line interface gives you access to these functions but only one at a time A scripting language is a wrapper language that integrates OS functions 4 Major scripting languages UNIX has sh Perl Macintosh has AppleScript Frontier Windows has no major scripting languages probably due to the weaknesses of DOS Generic scripting languages include Perl most popular Tcl easiest for beginners Python new Java like best for large programs 5 Perl Example 1 usr local bin perl Program to do the obvious print Hello world Print a message 6 Comments on Hello World Comments are to end of line But the first line usr local bin perl tells where to find the Perl compiler on your system Perl statements end with semicolons Perl is case sensitive Perl is compiled and run in a single operation 7 Perl Example 2 ex2 usr bin perl Remove blank lines from a file Usage singlespace oldfile newfile while line STDIN if line eq n next print line 8 More Perl notes On the UNIX command line In Perl STDIN is the input file STDOUT is the output file Scalar variables start with Scalar variables hold strings or numbers and they are interchangeable Examples filename means to get input from this file filename means to send output to this file priority 9 priority 9 Array variables start with 9 Perl Example 3 usr local bin perl Usage fixm filenames Replace r with n replaces input files foreach file ARGV print Processing file n if e fixm temp die File fixm temp already exists n if e file die No such file file n open DOIT tr 015 012 file fixm temp or die Can t tr 015 012 infile outfile n close DOIT open DOIT mv f fixm temp file or die Can t mv f fixm temp file n close DOIT 10 Comments on example 3 In Usage fixm filenames the angle brackets just mean to supply a list of file names here In UNIX text editors the r carriage return character usually shows up as M hence the name fixm temp The UNIX command tr 015 012 replaces all 015 characters r with 012 n characters The format of the open and close commands is open fileHandle fileName close fileHandle fileName says Take input from file pipe it to the tr command put the output on tr 015 012 file fixm temp fixm temp 11 Arithmetic in Perl a 1 2 Add 1 and 2 and store in a a 3 4 Subtract 4 from 3 and store in a a 5 6 Multiply 5 and 6 a 7 8 Divide 7 by 8 to give 0 875 a 9 10 Nine to the power of 10 that is 910 a 5 2 Remainder of 5 divided by 2 a Increment a and then return it a Return a and then increment it 12 String and assignment operators a b c Concatenate b and c a b x c b repeated c times a a a a b b b b Assign b to a Add b to a Subtract b from a Append b onto a 13 Single and double quotes a apples b bananas print a and b print a and b prints apples and bananas prints a and b print a and b prints apples and bananas 14 Arrays food apples bananas cherries But print food 1 morefood meat food prints bananas morefood meat apples bananas cherries a b c 5 10 20 15 push and pop push adds one or more things to the end of a list pop removes and returns the last element push food eggs bread push returns the new length of the list sandwich pop food len food len gets length of food food returns index of last element 16 foreach Visit each item in turn and call it morsel foreach morsel food print morsel n print Yum yum n 17 Tests Zero is false This includes 0 0 0 Anything not false is true Use and for numbers eq and ne for strings and are and or and not respectively 18 for loops for loops are just as in C or Java for i 0 i 10 i print i n 19 while loops usr local bin perl print Password a STDIN chop a Remove the last character n while a ne fred print sorry Again a STDIN chop a 20 do while and do until loops usr local bin perl do print Password a STDIN chop a while a ne fred 21 if statements if a print The string is not empty n else print The string is empty n 22 if elsif statements if a print The string elsif length a print The string elsif length a print The string characters n else print The string characters n is empty n 1 has one character n 2 has two has many 23 Why Perl Two factors make Perl important Pattern matching string manipulation Based on regular expressions REs REs are similar in power to those in Formal Languages but have many convenience features Ability to execute UNIX commands The Perl interpreter emulates these commands on nonUNIX platforms Often Perl is used simply for its UNIX emulation 24 Basic pattern matching sentence the sentence The dog bites if sentence the is false True if sentence contains the because Perl is case sensitive is does not contain 25 RE special characters Any single character except a newline The beginning of the line or string The end of the line or string Zero or more of the last character One or more of the last character 26 RE examples matches the entire string hi bye matches from hi to bye inclusive x y and y matches x one or more blanks Dear matches Dear only at beginning bags matches bag or bags 27 Square brackets qjk Either q or j or k qjk Neither q nor j nor k a z Anything from a to z inclusive a z No lower case letters a zA Z Any letter a z Any non zero sequence of lower case letters 28 More examples aeiou vowels …


View Full Document

Penn CIT 597 - Perl Lecture

Documents in this Course
DOM

DOM

21 pages

More DOM

More DOM

11 pages

Rails

Rails

33 pages

DOM

DOM

21 pages

RELAX NG

RELAX NG

31 pages

RELAX NG

RELAX NG

31 pages

RELAX NG

RELAX NG

31 pages

RELAX NG

RELAX NG

31 pages

Rake

Rake

12 pages

Ruby

Ruby

58 pages

DOM

DOM

21 pages

Tomcat

Tomcat

16 pages

DOM

DOM

21 pages

Servlets

Servlets

29 pages

Logging

Logging

17 pages

Html

Html

27 pages

DOM

DOM

22 pages

RELAX NG

RELAX NG

30 pages

Servlets

Servlets

28 pages

XHTML

XHTML

13 pages

DOM

DOM

21 pages

DOM

DOM

21 pages

Servlets

Servlets

26 pages

More CSS

More CSS

18 pages

Servlets

Servlets

29 pages

Logging

Logging

17 pages

Load more
Download Perl Lecture
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Perl Lecture and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Perl Lecture and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?