Presentation is loading. Please wait.

Presentation is loading. Please wait.

PERL Part 3 1.Subroutines 2.Pattern matching and regular expressions.

Similar presentations


Presentation on theme: "PERL Part 3 1.Subroutines 2.Pattern matching and regular expressions."— Presentation transcript:

1 PERL Part 3 1.Subroutines 2.Pattern matching and regular expressions

2 (1) Subroutines Subroutines provide a way for programmers to group a set of statements, set them aside, and turn them into mini-programs within a larger program. These mini-programs can be executed several times from different places in the overall program

3 Working with Subroutines You can create a subroutine by placing a group of statements into the following format: sub subroutine_name { set of statements } For example a outputTableRow subroutine sub outputTableRow { print ‘ One Two ’; } Execute the statements in this subroutine, by preceding the name by an ampersand: &outputTableRow;

4 Subroutine Example Program 1. #!/usr/bin/perl 2. use CGI ':standard'; 3. print header, start_html( 'Subroutine Example' ); 4. print 'Here is simple table '; 5. &outputTableRow; 6. &outputTableRow; 7. &outputTableRow; 8. print ' ', end_html; 9. sub outputTableRow { 10. print ' One Two '; 11. }

5 Would Output The Following …

6 (2) Pattern matching and regular expressions Use Perl pattern matching and regular expressions to filter input data Work with files to enable a program to store and retrieve data

7 Patterns in String Variables Many programming problems require matching, changing, or manipulating patterns in string variables. –An important use is verifying input fields of a form helps provide security against accidental or malicious attacks. For example, if expecting a form field to provide a telephone number as input, your program needs a way to verify that the input comprises a string of seven digits.

8 Four Different Constructs –The match operator enables your program to look for patterns in strings. –The substitute operator enables your program to change patterns in strings. –The split function enables your program to split strings into separate variables based on a pattern. –Regular expressions provide a pattern matching language that can work with these operators and functions to work on string variables.

9 The Match Operator The match operator is used to test if a pattern appears in a string. –It is used with the binding operator (“ =~ ”) to see whether a variable contains a particular pattern.

10 Other Delimiters? Slash (“/”) is most common match pattern –Others are possible, For example, both use valid match operator syntax: –if ( $name =~ m!Dave! ) { – if ( $name =~ m ) { The reverse binding operator test if pattern is NOT found: if ( $color !~ m/blue/ ) {

11 Substitutes Substitutes the first occurrence of the search pattern for the change pattern in the string variable. For example, the following changes the first occurrence of t to T : $name = “tom turtle”; $name =~ s/t/T/; print “Name=$name”; The output of this code would be Name=Tom turtle

12 Changing All Occurrences You can place a g (for global substitution) at the end of the substitution expression to change all occurrences of the target pattern string in the search string. For example, –$name = “tom turtle”; –$name =~ s/t/T/g; –print “Name=$name”; The output of this code would be –Name= Tom TurTle

13 Using Translate A similar function is called tr (for “translate”). Useful for translating characters from uppercase to lowercase, and vice versa. –The tr function allows you to specify a range of characters to translate from and a range of characters to translate to. : $name="smokeY"; $name =~ tr/[a-z]/[A-Z]/; print "name=$name"; Would output the following Name=SMOKEY

14 The Alternation Operator Alternation operator looks for alternative strings for matching within a pattern. –(That is, you use it to indicate that the program should match one pattern OR the other). The following shows a match statement using the alternation operator (left) and some possible matches based on the contents of $address (right); this pattern matches either com or edu.

15 Parenthesis For Groupings You use parentheses within regular expressions to specify groupings. For example, the following matches a $name value of Dave or David. Match Statement: if ( $name =~ /Dav(e|id)/) { print “$name came home from school\n”; }

16 Example Alternation Operator

17 Using Regular Expressions regular expressions to enable programs to more completely match patterns. –They actually make up a small language of special matching operators that can be employed to enhance the Perl string pattern matching.

18 Special Character Classes Perl has a special set of character classes for short hand pattern matching For example consider these two statements if ( $name =~ m/ / ) { if ($name =~ m/\s/ ) {

19 Special Character Classes

20 Special Character Classes - II

21 Special Character Classes - III

22 Setting Specific Patterns w/ Quantifiers Character quantifiers let you look for very specific patterns For example, use the dollar sign (“ $ ”) to to match if a string ends with a specified pattern. if ($ Name =~ /Jones$/ ) { Matches “John Jones” but not “Jones is here” would not. Also, “The guilty party is Jones” would matches.

23 Selected Perl Character Quantifiers

24

25 Building Regular Expressions that Work 1. Determine the precise field rules. 2. Get form and form-handling programs working 3. Start with the most specific term possible. 4. Anchor and refine. (Use ^ and $ when possible) – if ( $date =~ m{^\d\d/\d\d/\d\d\d\d$} ) { Starts with 2 digits in middle Ends with 4 digits

26 Regular Expression Special Variables Perl regexs set several special scalar variables: –$& will be equal to the first matching text –$` will be the text before the match, and –$’ will be the text after the first match. $name='*****Marty'; if ( $name =~ m/\w/ ) { print “match at=$& "; print "B4=$` after=$'"; } else { print "Not match"; } Output: match at=M B4=***** after=arty

27 Drivedate4.pl Example Program 1. #!/usr/bin/perl 2. use CGI ':standard'; 3. print header, start_html('Date Check'); 4. $date=param('udate'); 5. if ( $date =~ m{^\d\d/[0-3]\d/2\d\d\d$} ) { 6. print 'Valid date=', $date; 7. } else { 8. print 'Invalid date=', $date; 9. } 10. print end_html;

28 Output...

29 A Pattern Matching Example 1. #!/usr/bin/perl 2. use CGI ':standard'; 3. print header, start_html('Command Search'); 4. @PartNums=( 'XX1234', 'XX1892', 'XX9510'); 5. $com=param('command'); 6. $prod=param('uprod'); 7. if ($com eq "ORDER" || $com eq "RETURN") { 8. $prod =~ s/xx/XX/g; # switch xx to XX 9. if ($prod =~ /XX/ ) { 10. foreach $item ( @PartNums ) { 11. if ( $item eq $prod ) { 12. print "VALIDATED command=$com prodnum=$prod"; 13. $found = 1; 14. } 15. } 16. if ( $found != 1 ) { 17. print br,"Sorry Prod Num=$prod NOT FOUND"; 18. } 19. } else { 20. print br, "Sorry that prod num prodnum=$prod looks wrong"; 21. } 22. } else { 23. print br, "Invalid command=$com did not receive ORDER or RETURN"; 24. } 25. print end_html;

30 Output...

31 The Split Function split() breaks a string into different pieces based on a field separator. 2 arguments: – a pattern to match (which can contain regular expressions) – and a string variable to split. (into as many pieces as there are matches for the pattern)

32 split() Example $line = “Please, pass thepepper”; @result = split( /\s+/, $line ); Sets list variable $result with the following: $result[0] = “Please”; $result[1] = “,” $result[2] = “pass”; $result[3] = “thepepper”; 1 or more spaces Variable to split Results into a list

33 Another split() Example Another split() example: $ line = “Baseball, hot dogs, apple pie”; @newline = split( /,/, $line ); print “newline= @newline”; These lines will have the following output: –newline= Baseball hot dogs apple pie

34 The Split Function When you know how many matches to expect: $line = “AA1234:Hammer:122:12”; ($partno, $name, $id, $cost) = split( /:/, $line ); print “Part#: $partno; Name: $part; ID: $num; Cost: $cost”; Would output the following: P art#: AA1234; Name: Hammer; ID: 122; Cost: 12

35 Summary –Perl supports a set of operators and functions that are useful for working with string variables and verifying input data. The match operator, the substitute operator, the translate operator, the split function. –Perl uses regular expressions to to enable a program to look for specific characters (such as numbers, words, or letters) in specific places in any string. You can use them to verify form input, thereby providing a first line of defense against accidental or malicious input.

36 The End


Download ppt "PERL Part 3 1.Subroutines 2.Pattern matching and regular expressions."

Similar presentations


Ads by Google