Perl PowerPoint PPT Presentation

  • Uploaded on
  • Presentation posted in: General

2. Why Perl?. Perl is built around regular expressionsREs are good for string processingTherefore Perl is a good scripting languagePerl is especially popular for CGI scriptsPerl makes full use of the power of UNIXShort Perl programs can be very short?Perl is designed to make the easy jobs easy

Download Presentation


An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.

- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -

Presentation Transcript

1. 27-Sep-12 Perl Major parts of this lecture adapted from

2. 2 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

3. 3 Why not Perl? Perl is very UNIX-oriented Perl is available on other platforms... ...but isnt always fully implemented there However, Perl is often the best way to get some UNIX capabilities on less capable platforms Perl does not scale well to large programs Weak subroutines, heavy use of global variables Perls syntax is not particularly appealing

4. 4 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

5. 5 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)

6. 6 Perl Example 1

7. 7 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

8. 8 Perl Example 2

9. 9 More Perl notes On the UNIX command line; < filename means to get input from this file > filename means to send output to this file 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: $priority = 9; $priority = '9'; Array variables start with @

10. 10 Perl Example 3

11. 11 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 "| tr \'\\015' \'\\012' < $file > fixm_temp" says: Take input from $file, pipe it to the tr command, put the output on fixm_temp

12. 12 Arithmetic in Perl

13. 13 String and assignment operators

14. 14 Single and double quotes $a = 'apples'; $b = 'bananas'; print $a . ' and ' . $b; prints: apples and bananas print '$a and $b'; prints: $a and $b print "$a and $b"; prints: apples and bananas

15. 15 Arrays @food = ("apples", "bananas", "cherries"); But print $food[1]; prints "bananas" @morefood = ("meat", @food); @morefood == ("meat", "apples", "bananas", "cherries"); ($a, $b, $c) = (5, 10, 20);

16. 16 push and pop push adds one or more things to the end of a list push (@food, "eggs", "bread"); push returns the new length of the list pop removes and returns the last element $sandwich = pop(@food); $len = @food; # $len gets length of @food $#food # returns index of last element

17. 17 foreach

18. 18 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.

19. 19 for loops for loops are just as in C or Java for ($i = 0; $i < 10; ++$i) { print "$i\n"; }

20. 20 while loops

21. 21 do..while and do..until loops

22. 22 if statements

23. 23 if - elsif statements

24. 24 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 Less useful outside a UNIX environment

25. 25 Basic pattern matching $sentence =~ /the/ True if $sentence contains "the" $sentence = "The dog bites."; if ($sentence =~ /the/) # is false because Perl is case-sensitive !~ is "does not contain"

26. 26 RE special characters

27. 27 RE examples

28. 28 Square brackets

29. 29 More examples

30. 30 More special characters

31. 31 Quoting special characters

32. 32 Alternatives and parentheses

33. 33 Substitution =~ is a test, as in: $sentence =~ /the/ !~ is the negated test, as in: $sentence !~ /the/ =~ is also used for replacement, as in: $sentence =~ /london/London/ This is an expression, whose value is the number of substitutions made (0 or 1)

34. 34 The $_ variable Often we want to process one string repeatedly The $_ variable holds the current string If a subject is omitted, $_ is assumed Hence, the following are equivalent: if ($sentence =~ /under/) $_ = $sentence; if (/under/) ...

35. 35 Global substitutions s/london/London/ substitutes London for the first occurrence of london in $_ s/london/London/g substitutes London for each occurrence of london in $_ The value of a substitution expression is the number of substitutions actually made

36. 36 Case-insensitive substitutions s/london/London/i case-insensitive substitution; will replace london, LONDON, London, LoNDoN, etc. You can combine global substitution with case-insensitive substitution s/london/London/gi

37. 37 Remembering patterns Any part of the pattern enclosed in parentheses is assigned to the special variables $1, $2, $3, , $9 Numbers are assigned according to the left (opening) parentheses "The moon is high" =~ /The (.*) is (.*)/ Afterwards, $1 = "moon" and $2 = "high"

38. 38 Dynamic matching During the match, an early part of the match that is tentatively assigned to $1, $2, etc. can be referred to by \1, \2, etc. Example: \b.+\b matches a single word /(\b.+\b) \1/ matches repeated words "Now is the the time" =~ /(\b.+\b) \1/ Afterwards, $1 = "the"

39. 39 tr tr does character-by-character translation tr returns the number of substitutions made $sentence =~ tr/abc/edf/; replaces a with e, b with d, c with f $count = ($sentence =~ tr/*/*/); counts asterisks tr/a-z/A-Z/; converts to all uppercase

40. 40 split split breaks a string into parts $info = "Caine:Michael:Actor:14, Leafy Drive"; @personal = split(/:/, $info); @personal = ("Caine", "Michael", "Actor", "14, Leafy Drive");

41. 41 Associative arrays Associative arrays allow lookup by name rather than by index Associative array names begin with % Example: %fruit = ("apples", "red", "bananas", "yellow", "cherries", "red"); Now, $fruit{"bananas"} returns "yellow" Note: braces, not parentheses

42. 42 Associative Arrays II Can be converted to normal arrays: @food = %fruit; You cannot index an associative array, but you can use the keys and values functions: foreach $f (keys %fruit) { print ("The color of $f is " . $fruit{$f} . "\n"); }

43. 43 Associative Arrays III The function each gets key-value pairs while (($f, $c) = each(%fruit)) { print "$f is $c\n"; }

44. 44 Calling subroutines Assume you have a subroutine printargs that just prints out its arguments Subroutine calls: &printargs("perly", "king"); Prints: "perly king" &printargs("frog", "and", "toad"); Prints: "frog and toad"

45. 45 Defining subroutines Here's the definition of printargs: sub printargs { print "@_\n"; } Where are the parameters? Parameters are put in the array @_ which has nothing to do with $_

46. 46 Returning a result The value of a subroutine is the value of the last expression that was evaluated

47. 47 Local variables @_ is local to the subroutine, and so are $_[0], $_[1], $_[2], local creates local variables

48. 48 Example subroutine

49. 49 Perl V There are only a few differences between Perl 4 and Perl 5 Perl 5 has modules Perl 5 modules can be treated as classes Perl 5 has auto variables

50. 50 The End

  • Login