Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS 330 Programming Languages 09 / 25 / 2008 Instructor: Michael Eckmann.

Similar presentations


Presentation on theme: "CS 330 Programming Languages 09 / 25 / 2008 Instructor: Michael Eckmann."— Presentation transcript:

1 CS 330 Programming Languages 09 / 25 / 2008 Instructor: Michael Eckmann

2 Michael Eckmann - Skidmore College - CS 330 - Fall 2008 Today’s Topics Questions/comments? Perl –Practical Extraction and Report Language

3 Perl Michael Eckmann - Skidmore College - CS 330 - Fall 2008 You have two options Get Perl on your own machine. Download Perl (version 5.003 or higher.)‏ –recommend ActivePerl from ActiveState for Mac OS and Windows –Perl is installed by default under most linuxes AND/OR Use the Linux machines in the new Linux lab.

4 Perl Michael Eckmann - Skidmore College - CS 330 - Fall 2008 Perl (developed by Larry Wall and many others) is a language that has as it's ancestors C, sed, AWK and others. Sed is short for stream editor, AWK is named for it's authors (of which K is for Kernighan famous for his work on C.) Sed and AWK are good for pattern matching, editing and reporting on text files. Perl has these capabilities too. Some of Perl's syntax is C-like.

5 Perl Michael Eckmann - Skidmore College - CS 330 - Fall 2008 How it's going to be “taught” in this course. –We're going to dive in and see a lot of stuff so you get a general overview of the language quickly. –We will go in more depth later, on several topics. –I suggest you go through a tutorial with and learn from your peers. –We will not learn all there is to know about Perl.

6 Perl Michael Eckmann - Skidmore College - CS 330 - Fall 2008 Resources on the web www.perl.com A Perl tutorial http://www.comp.leeds.ac.uk/Perl/start.html Programming Perl book online http://www.unix.org.ua/orelly/perl/prog3/index.htm If that's not enough, you know how to search the internet. I'll try to post some useful links on our course webpage.

7 Perl Michael Eckmann - Skidmore College - CS 330 - Fall 2008 Variables need not be declared, they exist upon first use. If you happen to use a variable before it is assigned a value, then it is 0,“”, or false depending on its use. You can though, if you want to, declare variables with my or our (to be explained later.)‏ You can also do use strict; which provides error checking including disallowing the use of undeclared variables.

8 Perl Michael Eckmann - Skidmore College - CS 330 - Fall 2008 –Scalars (hold a single value) Names start with a $ –Arrays Names start with an @ –Hashes (keyed lists aka associative arrays)‏ Names start with a % –Subroutines Names start with an &

9 Perl Michael Eckmann - Skidmore College - CS 330 - Fall 2008 More about hashes –They consist of pairs of data. The first in each pair is the key and the second is the value. –Values can be returned based on their keys. –We'll see an example in a couple of slides

10 Perl Michael Eckmann - Skidmore College - CS 330 - Fall 2008 Example scalar assignment statements (and declarations) --- can hold strings or numerics (integers and floating point.)‏ $age = 50; $name = “Mike”; Example array assignment statements (and declarations)‏ @grades_list = (100, 98, 43, 87, 92); @people = (“Jerry Garcia”, “Bobby Weir”, “Phil Lesh”); Example hash assignment statement (and declaration)‏ %course_names = (“CS106” => “Intro to CS 1”, “CS206” => “Intro to CS 2”, “CS330” => “Programming Languages”);

11 Perl Michael Eckmann - Skidmore College - CS 330 - Fall 2008 @people = (“Jerry Garcia”, “Bobby Weir”, “Phil Lesh”); When referring to one element of an array, use $ because each element is a scalar and use the typical square brackets with index. Indices start at 0. e.g. $people[0] would refer to the first element of the array. Oddly, scalars can be assigned from the whole array: ($lead, $rhythm, $bass) = @people;

12 Perl Michael Eckmann - Skidmore College - CS 330 - Fall 2008 Note: in hashes, the comma can be used instead of the => which is less readable, but valid syntax. %course_names = (“CS106” => “Intro to CS 1”, “CS206” => “Intro to CS 2”, “CS330” => “Programming Languages”); Also, hashes can be used as lists (ignoring the key / value pair meaning.) The first key is the first element of the list, the first value is the second element in the list, the 2 nd key is the third element, and so on. e.g. @course_info = %course_names; Here, $course_info[0] contains “CS106” $course_info[1] contains “Intro to CS 1” etc.

13 Perl Michael Eckmann - Skidmore College - CS 330 - Fall 2008 %course_names = (“CS106” => “Intro to CS 1”, “CS206” => “Intro to CS 2”, “CS330” => “Programming Languages”); To get a value out of a hash, you can use its key inside { } e.g. $name = $course_names{“CS330”}; $name would contain the string “Programming Languages”.

14 Perl Michael Eckmann - Skidmore College - CS 330 - Fall 2008 Output print “Hello, World!\n”; $output_text = “Hello, World”; print $output_text. “\n”; print “$output_text\n”; #same as above because “” causes interpolation print '$output_text \n'; #would print literally --- not value of var and no new line either print “\$output_text\n”; #would print literally --- b/c \ forces $ to be printed so no var

15 Perl Michael Eckmann - Skidmore College - CS 330 - Fall 2008 Operators For numerics: +, -, *, /, %, ** (exponentiation)‏ For strings:. (concatentation), x (multiplies the string by an integer)‏ e.g. $text = “Hey”; $text = $text x 3; # result is HeyHeyHey

16 Perl Michael Eckmann - Skidmore College - CS 330 - Fall 2008 Assignment operators (more than just the =)‏ Here are some examples *=, +=, ||=, x=, etc... e.g. $text = “Hey”; $text x= 3; # same as if we did $text = $text x 3;

17 Perl Michael Eckmann - Skidmore College - CS 330 - Fall 2008 Conditional operators include:, =, ==, != (for numeric comparison)‏ lt, gt, le, ge, eq, ne (for string comparisons)‏ (numeric compare) --- it is a less than followed by equals followed by a greater than symbol without spaces. cmp (string compare)‏

18 Perl Michael Eckmann - Skidmore College - CS 330 - Fall 2008 These probably don’t need much discussion:, =, ==, != (for numeric comparison)‏ lt, gt, le, ge, eq, ne (for string comparisons)‏ But these two compare operators are interesting: (numeric compare)‏ cmp (string compare)‏ The two compare operators above Return –1 if left operand is less than right operand Return 0 if left operand is equal to right operand Return +1 if left operand is greater than right operand

19 Perl Michael Eckmann - Skidmore College - CS 330 - Fall 2008 We covered: –Printing Interpolation “ “ vs. literal text ' ' \ reference operator within “ “ to force literal –Operators (numeric and string)., x, +, *, etc. –Assignment operators *=,.=, ||=, etc. –conditional operators (numeric and string)‏ Next we'll cover: –true and false –Logical &&, ||, !, and, or, not, xor (words vs. symbols different precedence)‏ –for, while, until, do-while, if, else, elsif –File handling –Assignments (chaining them --- b/c they return lvalues)‏ –chop, chomp –Class exercise –Regular expressions

20 Perl Michael Eckmann - Skidmore College - CS 330 - Fall 2008 True and false –Perl treats any string as true except “0” and “” which are false. –Perl treats any number as true except 0 which is false. Comments –Single line comments are specified starting with the # character and go until a newline.

21 Perl Michael Eckmann - Skidmore College - CS 330 - Fall 2008 Logical operators Both symbols and words &&, || ! and, or, not (also allowed but have lower precedence than their related symbols above.)‏ Both forms of AND (&& and and) and OR (|| and or) are short circuit operators. That means that if the left operand determines the outcome of the whole condition, then the right operand is not evaluated.

22 Perl Michael Eckmann - Skidmore College - CS 330 - Fall 2008 Short circuiting When using or or ||, if the left operand evaluates to true then the whole thing is true (because true or anything is true) so, the right side is not evaluated. When using and or &&, if the left operand evaluates to false then the whole thing is false (because false and anything is false) so, the right side is not evaluated. E.g. if ($x == 1 && $y < 0) # suppose $x had the value 0, it is unnecessary to evaluate the $y < 0 at all, because the condition will be false regardless.

23 Perl Michael Eckmann - Skidmore College - CS 330 - Fall 2008 Short circuiting E.g. if ($z == 0 || $y < 0) # suppose $z had the value 0, it is unnecessary to evaluate the $y < 0 at all, because the condition will be true regardless.

24 Perl Michael Eckmann - Skidmore College - CS 330 - Fall 2008 Example of how Perl programmers use this short circuit feature to their advantage to make concise readable code. open(FILEHAND, “<“, $fname) or die “can’t open file.\n”; Because of the short circuit, the above works in the following way, the open function returns false if the file can’t be opened. If that happens the die function is called (see the or operator) which prints the error to STDERR and kills the program. If the file can be opened, then true is returned and the or part is not evaluated (executed.)‏

25 Perl Michael Eckmann - Skidmore College - CS 330 - Fall 2008 Open and die are two of Perl’s built-in functions. STDIN, STDOUT, and STDERR are file handles that are automatically available and open in Perl programs. STDIN is the keyboard and the other 2 are the console. We’ll come back to more about opening files later, let’s instead continue with our discussion of more operators.

26 Perl Michael Eckmann - Skidmore College - CS 330 - Fall 2008 if, elsif, else structures (notice the odd spelling of elsif --- there is no “e” in it.)‏ Why do you think? As expected, the elsif and else portions of the if structure are optional. You can have an if, followed by zero or more elsif’s, followed by zero or one else’s. Also, there’s an unless that can be used instead of the if (but still can use the elsif’s and else portions.)‏ unless reverses the test if you had used if

27 Perl Michael Eckmann - Skidmore College - CS 330 - Fall 2008 if ($count < 10) { # do something here } Is the same as: unless ($count >= 10) { # do something here }

28 Perl (example of if/elsif/else)‏ Michael Eckmann - Skidmore College - CS 330 - Fall 2008 if ($count < 10) { # do something here } elsif ($count >100)‏ { # do something here } else { # do something here }

29 Perl Michael Eckmann - Skidmore College - CS 330 - Fall 2008 Previous slides showed how to use if and unless on blocks of code. Interestingly, if, unless, while, until and foreach can all be used as modifiers to a simple statement. Examples: print “Hello” unless $printing_is_off; $total++ if $increase_total;

30 Perl Michael Eckmann - Skidmore College - CS 330 - Fall 2008 while, until, foreach and for are looping structures in Perl. while and for act as you’d expect from knowing C++ or Java. until executes its loop until the condition becomes true, whereas while executes its loop until the condition becomes false. Yeah it’s redundant. So much of Perl is.

31 Perl Michael Eckmann - Skidmore College - CS 330 - Fall 2008 foreach works on list data (e.g. arrays.)‏ Example: foreach $element (@people) { print “$element is a person in the array\n”; } # foreach iterates through all values of the array in the parens and uses the variable just after the word foreach to temporarily store the value. Then the code in the { }’s executes once for every element of the array. Note: $element and @people are user-defined names (not special to Perl)‏

32 Perl Michael Eckmann - Skidmore College - CS 330 - Fall 2008 open( INDATA, “< “, “datafile.txt”) or die “can’t open datafile.txt”; Alternatively, one can combine the mode with the file name in one string: e.g. “<datafile.txt” Modes: < is input, > is output (writing), >> is append, +< is read-write (assumes file exists) and >+ is read-write (file might not exist).

33 Perl Michael Eckmann - Skidmore College - CS 330 - Fall 2008 Another thing to notice about Perl is that for calling the built-in functions we can use parentheses around the arguments or not use the parentheses. e.g. open(FH, “<data.txt”); open FH, “<data.txt”; # both are valid and do the same thing. But be careful with something like: print ( 7 + 3 ) * 2; # this will cause the parens to enclose the args # and therefore it would print 10 not 20. We would want this instead: print (( 7 + 3 ) * 2);

34 Perl Michael Eckmann - Skidmore College - CS 330 - Fall 2008 Use the angle brackets to read a line from a file handle. Use print to write to a filehandle. e.g. print FH “A line to be written to file\n”;

35 Perl Michael Eckmann - Skidmore College - CS 330 - Fall 2008 When reading lines from STDIN or a file, the line will contain a \n at the end. It is often the case that you wish to get rid of it. Use chomp function to do this. $inline = ; chomp($inline); Or chomp($inline = ); Chomp removes the end of record marker and returns the # of chars removed. chop is also a function. It removes the last character regardless if it is \n or not and returns the character.

36 Perl Michael Eckmann - Skidmore College - CS 330 - Fall 2008 An interesting thing about return values: The assignment operators are interesting in that they return the variable on the LHS of the assignment as an lvalue. An lvalue is something that can have a value assigned to it. This allows chaining of assignments like: $num1 = $num2 = $num3 = 0; # 0 is assigned to num3, then num3 to num2 … And ($temp -= 32) *= 5/9; # the -= in parens returns the $temp as an lvalue which is assigned # a new value with the *= assignment.

37 Perl Michael Eckmann - Skidmore College - CS 330 - Fall 2008 Let's go to the Linux lab and do an exercise and some more lecture.

38 Perl Michael Eckmann - Skidmore College - CS 330 - Fall 2008 First, everyone log in, open a terminal window type the following: –mkdir cs330 –cd cs330 Save the input_file_for_perl.txt into your cs330 directory. and bring up a text editor (e.g. either emacs or gedit) by typing in the terminal window: –gedit & –enter this as the first line in your file: #!/usr/bin/perl

39 Perl Michael Eckmann - Skidmore College - CS 330 - Fall 2008 Continue and add this line below the shebang line: print “Hello World\n”; save the file as –helloWorld.pl at the terminal window do the following: chmod 755 helloWorld.pl (makes your script executable)‏./helloWorld.pl (executes your script using shebang line)‏ OR perl helloWorld.pl (executes your script explicitly using perl)

40 Perl Michael Eckmann - Skidmore College - CS 330 - Fall 2008 Let's try the following as an exercise: –read each line of input from the file on the notes page input_file_for_perl.txt until the line that has “eof” is read –store each line in an element of an array (with the exception of the last line “eof”.)‏ –store the array into a hash (can be done with one line of code)‏ –Then, in a loop, ask the user for input (from STDIN) for a product and output its price by looking it up in the hash. Do this until user enters some sentinel that you make him/her aware of at the beginning. Note: the format of the file consists of a product name on one line and its price on the next line, then another product name on the next line and it's price on the next line and so on until the last line of the file which contains the word: eof

41 Perl Michael Eckmann - Skidmore College - CS 330 - Fall 2008 Matching string basics =~ (matches)‏ m/ / (this is the format of match regular expression)‏ Note: you put the regular expression in between the / / The format of a complete expression which will evaluate to true or false depending on whether the string matched or not (anywhere within the string) is as follows: ($some_string =~ m/RegExprToMatch/)‏ Examples: (“CS330 Programming Languages” =~ m/Prog/) # true (“CS330 Programming Languages” =~ m/CS106/) # false

42 Perl Michael Eckmann - Skidmore College - CS 330 - Fall 2008 Matching string basics !~ (doesn't match)‏ Examples: (“CS330 Programming Languages” !~ m/Prog/) # false (“CS330 Programming Languages” !~ m/CS106/) # true You may use any other character as a delimiter other than the /'s if you wish. e.g. m#Prog# could be used instead of m/Prog/ The m for the m/ / matching operator is optional if we use the /'s as the delimiters. To match a / you have two choices, either use a different delimiter, then just put the slash in there, or backslash the slash to make it an escape sequence.

43 Perl Michael Eckmann - Skidmore College - CS 330 - Fall 2008 More information: Variables can be put inside the / / search pattern and they are interpolated. =~ can be omitted if matching the $_ special default variable Metacharacters {}[]()^$.|*+?\ have special meaning inside the m/ /. If you wish to match those characters you need to backslash them. Also, regardless of the delimiter, if you which to match what you used as the delimiter, guess what you need to do? ^ forces the match to be required to be at the very beginning of the string $ forces the match to be required to be at the very end of the string Examples: (“CS330 Programming Languages” =~ m/^Prog/) # false, not at beginning (“CS330 Programming Languages” =~ m/ges$/) # true, ges is at the end (“CS330 Programming Languages” =~ m/^ges$/) # false, ges not the whole string (“CS330” =~ m/^CS330$/) # true, CS330 is the complete string

44 Perl Michael Eckmann - Skidmore College - CS 330 - Fall 2008 More information: Character classes are put between [ ] square brackets within the pattern --- this means any one character in the [ ] is able to match. May use ^ (not) to not match a char inside the character class May use – (hyphen) to specify a range of characters for the class “Hay is for horses” =~ m/[A-Z]ay/; # matches if the string contains any capital letter followed by ay “The boardgame Payday is lame” =~ m/[A-Z]ay/; # this would be true too, it matches Pay “The boardgame Payday is lame” =~ m/[^A-Z]ay/; # this would be true too, why?

45 Perl Michael Eckmann - Skidmore College - CS 330 - Fall 2008 More information: Character classes are put between [ ] square brackets within the pattern --- this means any one character in the [ ] is able to match. May use ^ (not) to not match a char inside the character class May use – (hyphen) to specify a range of characters for the class “Hay is for horses” =~ m/[A-Z]ay/; # matches if the string contains any capital letter followed by ay “The boardgame Payday is lame” =~ m/[A-Z]ay/; # this would be true too, it matches Pay “The boardgame Payday is lame” =~ m/[^A-Z]ay/; # this would be true too, why? It matches day These are special variables that are set for a match: $` $& and $' (left, matched, right)‏ Let's put this code in a program and print out the variable values to see what matches.

46 Perl Michael Eckmann - Skidmore College - CS 330 - Fall 2008 There are ways to specify common character classes \d (any digit)‏ \s (any whitespace \ \t\r\n\f \w (any “word” character (a digit, letter or underscore))‏ \D (any non-digit)‏ \S (any non-whitespace)‏ \W (any non-word character)‏. (any character other than newline \n)‏ These can be used within the square brackets or without.

47 Perl Michael Eckmann - Skidmore College - CS 330 - Fall 2008 Modifiers are characters that go after the second forward slash i is a modifier for ignore case. The behaviour for no modifier (the default) is that. Matches any non-newline character ^ matches at beginning of string $ matches at end of string (or before a newline at end)‏ s modifier: treats the string as a single long line, so. matches any character including newline m modifier: treats string as multiple lines so, ^ and $ match the beginning or end of any line But now, \A matches the beginning of the whole string, \Z matches the end of the whole string. Let’s look at this webpage’s examples under Using Character Classes for some more examples: http://www.cs.rit.edu/~afb/20013/plc/perl5/doc/perlretut.html

48 Perl Michael Eckmann - Skidmore College - CS 330 - Fall 2008 | alternation character (acts sort of like a logical or)‏ Grouping characters using the parentheses Getting the “submatches” by using the $1, $2, $3, etc. variables which are set via the parentheses. Repetitions ? - 0 or 1 time * - 0 or more times + - 1 or more times { } – min and max, at least or exactly Let’s continue looking at this site for examples: http://www.cs.rit.edu/~afb/20013/plc/perl5/doc/perlretut.html

49 Perl Michael Eckmann - Skidmore College - CS 330 - Fall 2008 Recap on the special variables we learned $_ $` $& and $' (left, match, right)‏ $0 (program name)‏ $1, $2, $3,... (the submatches)‏

50 Perl Michael Eckmann - Skidmore College - CS 330 - Fall 2008 Let’s look at a larger parsing example using many of the features we just learned: The “doing string selections” section of: http://www.troubleshooters.com/codecorn/littperl/perlreg.htm The following page is a good page for reference. It is a nice summary of the different characters and their meanings with succinct examples: http://www.cs.tut.fi/~jkorpela/perl/regexp.html


Download ppt "CS 330 Programming Languages 09 / 25 / 2008 Instructor: Michael Eckmann."

Similar presentations


Ads by Google