Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 Perl, Beyond the Basics: Regular Expressions, Subroutines, and Objects in Perl CSCI 431 Programming Languages Fall 2003.

Similar presentations


Presentation on theme: "1 Perl, Beyond the Basics: Regular Expressions, Subroutines, and Objects in Perl CSCI 431 Programming Languages Fall 2003."— Presentation transcript:

1 1 Perl, Beyond the Basics: Regular Expressions, Subroutines, and Objects in Perl CSCI 431 Programming Languages Fall 2003

2 2 Regular Expressions Perl provides a great deal of built-in text-processing functions. We will cover only some of the most popular such functions.Perl provides a great deal of built-in text-processing functions. We will cover only some of the most popular such functions. Perl uses forward slashes to delimit regular expressions for pattern matching and substitution.Perl uses forward slashes to delimit regular expressions for pattern matching and substitution. Strings are evaluated to true of false via the =~ operator.Strings are evaluated to true of false via the =~ operator. $a = "Mary had a little lamb"; $a =~/little/ # evaluates to true $a =~/brittle/ # evaluates to false

3 3 Patten Matching Perl provides a set of modifying characters for string matching, some of these are shown below:Perl provides a set of modifying characters for string matching, some of these are shown below: Modifier Meaning Modifier Meaning i matches characters regardless of case s treats string as a single line gglobally find all matches e.g.: $a =~/little/i

4 4 Example while ($line = ) { if ($line =~ /http:/) { print $line; }}or while( ) { print if /http:/; }

5 5 Pattern Matching Perl uses a set of meta-characters to extend the functionality of pattern matching. Below is a table of commonly used meta characters.Perl uses a set of meta-characters to extend the functionality of pattern matching. Below is a table of commonly used meta characters. Metacharacter Meaning. matches any single character except for \n. matches any single character except for \n ^ matches the front of a line ^ matches the front of a line $ matches the end of a line $ matches the end of a line *matches proceeded character 0 or more times *matches proceeded character 0 or more times + matches proceeded character 1 or more times + matches proceeded character 1 or more times ? matches proceeding character 0 or 1 times ? matches proceeding character 0 or 1 times [...] matches any of the class of characters [...] matches any of the class of characters

6 6 Pattern Matching Perl also has a set of special characters proceeded with a backslash, some of which are listed below.Perl also has a set of special characters proceeded with a backslash, some of which are listed below. Special Character Meaning \s any whitespace \S any non whitespace \d any digit i.e. [0-9] \w any alphanumeric i.e [a-zA-Z0-9] \n newline \t tab

7 7 Example from 3 rd Edition of Perl Programming (finding duplicate words) while (<>) { while (<>) { while ( m{ while ( m{ \b#start at a word boundary (\w\S+)#find a wordish chunk ( \s+#separated by whitespace \1#and that chunk again )+# repeat ad lib \b }xig }xig ){ print “dup word ‘$1’ at paragraph $.\n”; }}

8 8 Substitution Perl provides a simple way of searching for patterns and substituting new patterns in their place.Perl provides a simple way of searching for patterns and substituting new patterns in their place. This accomplished by using a s before a slash delimited regular expression.This accomplished by using a s before a slash delimited regular expression. s/string1/string2/i # replaces the first instance of string1 with # with string 2 the /i forces a case sensitive # search

9 9 Functions A subprogram in Perl is a function.A subprogram in Perl is a function. An ampersand is placed before the function name to denote invocation.An ampersand is placed before the function name to denote invocation. If the function takes arguments, they are placed within parentheses following the name of the function (the parentheses may be omitted).If the function takes arguments, they are placed within parentheses following the name of the function (the parentheses may be omitted).&aFunction(); &aFunction($arg1, @arg2); Control is transferred to the code of the function and transferred back at the end of the code or when an explicit return operator is reached.Control is transferred to the code of the function and transferred back at the end of the code or when an explicit return operator is reached.

10 10 Function Definition The function definition is marked by the keyword sub followed by the name of the function (without an ampersand prefix). The function body is enclosed in curly brackets.The function definition is marked by the keyword sub followed by the name of the function (without an ampersand prefix). The function body is enclosed in curly brackets. sub aFunction { stmt_1;stmt_2;…stmt_n;} The value returned by a function is the value of the last expression evaluated.The value returned by a function is the value of the last expression evaluated. Functions can not be nested.Functions can not be nested.

11 11 Arguments The arguments to a function constitute a list. They are available within the function definition through the predefined list variable @_The arguments to a function constitute a list. They are available within the function definition through the predefined list variable @_ &aFunction ($a, “hello”, $b); sub aFunction { foreach $temp (@_) { print “$temp \n”; }}

12 12 Variable Scope Variables defined within the body of a Perl program are a available inside a Perl function as global variables.Variables defined within the body of a Perl program are a available inside a Perl function as global variables. Perl offers 3 scoping declarations:Perl offers 3 scoping declarations: –mye.g., my nose; –oure.g., our $house –local$local $Tvchannel my and local can both be used (although they work in different ways) to limit the scope of variables that are intended to be local to a function.my and local can both be used (although they work in different ways) to limit the scope of variables that are intended to be local to a function. Sub aFunction { my ($local1, $local2); …}

13 13 Object Orientated Programming in Perl Part 1: References and PackagesPart 1: References and PackagesPart 1: References and PackagesPart 1: References and Packages Part 2: Object Properties and Object MethodsPart 2: Object Properties and Object MethodsPart 2: Object Properties and Object MethodsPart 2: Object Properties and Object Methods


Download ppt "1 Perl, Beyond the Basics: Regular Expressions, Subroutines, and Objects in Perl CSCI 431 Programming Languages Fall 2003."

Similar presentations


Ads by Google