Presentation is loading. Please wait.

Presentation is loading. Please wait.

CSCI 431 Programming Languages Fall 2003

Similar presentations


Presentation on theme: "CSCI 431 Programming Languages Fall 2003"— Presentation transcript:

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

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 uses forward slashes to delimit regular expressions for pattern matching and substitution. 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 Patten Matching Perl provides a set of modifying characters for string matching, some of these are shown below: Modifier Meaning i matches characters regardless of case s treats string as a single line g globally find all matches e.g.: $a =~/little/i

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

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. Metacharacter Meaning . matches any single character except for \n ^ matches the front of a line $ matches the end of a line * matches proceeded character 0 or more times + matches proceeded character 1 or more times ? matches proceeding character 0 or 1 times [...] matches any of the class of characters

6 Pattern Matching 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 Example from 3rd Edition of Perl Programming (finding duplicate words)
while (<>) { 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 ) { print “dup word ‘$1’ at paragraph $.\n”; }

8 Substitution 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. s/string1/string2/i # replaces the first instance of string1 with # with string 2 the /i forces a case sensitive # search

9 Functions A subprogram in Perl is a function.
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). &aFunction(); 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 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. sub aFunction { stmt_1; stmt_2; stmt_n; } The value returned by a function is the value of the last expression evaluated. Functions can not be nested.

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

12 Variable Scope Variables defined within the body of a Perl program are a available inside a Perl function as global variables. Perl offers 3 scoping declarations: my e.g., my nose; our e.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. Sub aFunction { my ($local1, $local2); }

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


Download ppt "CSCI 431 Programming Languages Fall 2003"

Similar presentations


Ads by Google