perl, beyond the basics: regular expressions, subroutines, and objects in perl

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

Upload: cedric-grimes

Post on 30-Dec-2015

32 views

Category:

Documents


2 download

DESCRIPTION

Perl, Beyond the Basics: Regular Expressions, Subroutines, and Objects in Perl. CSCI 431 Programming Languages Fall 2003. Regular Expressions. Perl provides a great deal of built-in text-processing functions. We will cover only some of the most popular such functions. - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Perl, Beyond the Basics: Regular Expressions, Subroutines, and Objects in Perl

11

Perl, Beyond the Basics: Regular Perl, Beyond the Basics: Regular Expressions, Subroutines, and Expressions, Subroutines, and

Objects in PerlObjects in Perl

CSCI 431 Programming LanguagesCSCI 431 Programming Languages

Fall 2003Fall 2003

Page 2: Perl, Beyond the Basics: Regular Expressions, Subroutines, and Objects in Perl

22

Regular ExpressionsRegular Expressions

• Perl provides a great deal of built-in text-processing Perl provides a great deal of built-in text-processing functions. We will cover only some of the most functions. We will cover only some of the most popular such functions. popular such functions.

• Perl uses forward slashes to delimit regular Perl uses forward slashes to delimit regular expressions for pattern matching and substitution. expressions for pattern matching and substitution.

• Strings are evaluated to true of false via the =~ Strings are evaluated to true of false via the =~ operator. operator. $a = "Mary had a little lamb"; $a = "Mary had a little lamb"; $a =~/little/ $a =~/little/ # evaluates to true # evaluates to true $a =~/brittle/ $a =~/brittle/ # evaluates to false# evaluates to false

Page 3: Perl, Beyond the Basics: Regular Expressions, Subroutines, and Objects in Perl

33

Patten MatchingPatten Matching

• Perl provides a set of modifying Perl provides a set of modifying characters for string matching, some of characters for string matching, some of these are shown below: these are shown below:

ModifierModifier MeaningMeaning i i matches characters regardless of case matches characters regardless of case s s treats string as a single linetreats string as a single line gg globally find all matchesglobally find all matches

e.g.: e.g.: $a =~/little/i$a =~/little/i

Page 4: Perl, Beyond the Basics: Regular Expressions, Subroutines, and Objects in Perl

44

ExampleExample

while ($line = <FILE>) {while ($line = <FILE>) {if ($line =~ /http:/) {if ($line =~ /http:/) {

print $line;print $line;}}

}}

oror

while(<FILE>) {while(<FILE>) {

print if /http:/;print if /http:/;

}}

Page 5: Perl, Beyond the Basics: Regular Expressions, Subroutines, and Objects in Perl

55

Pattern MatchingPattern Matching

• Perl uses a set of Perl uses a set of meta-charactersmeta-characters to extend to extend the functionality of pattern matching. Below is the functionality of pattern matching. Below is a table of commonly used meta characters. a table of commonly used meta characters.

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

Page 6: Perl, Beyond the Basics: Regular Expressions, Subroutines, and Objects in Perl

66

Pattern MatchingPattern Matching

• Perl also has a set of special characters Perl also has a set of special characters proceeded with a backslash, some of proceeded with a backslash, some of which are listed below.which are listed below.

Special CharacterSpecial Character MeaningMeaning \s \s any whitespace any whitespace \S \S any non whitespace any non whitespace \d \d any digit i.e. [0-9] any digit i.e. [0-9] \w \w any alphanumeric i.e [a-zA-Z0-9] any alphanumeric i.e [a-zA-Z0-9] \n \n newline newline \t \t tab tab

Page 7: Perl, Beyond the Basics: Regular Expressions, Subroutines, and Objects in Perl

77

Example from 3Example from 3rdrd Edition of Perl Programming Edition of Perl Programming (finding duplicate words)(finding duplicate words)

wwhile (<>) {hile (<>) { while ( m{while ( m{

\b\b #start at a word boundary#start at a word boundary(\w\S+)(\w\S+) #find a wordish chunk#find a wordish chunk((

\s+\s+ #separated by #separated by whitespacewhitespace

\1\1 #and that chunk again#and that chunk again)+)+ # repeat ad lib# repeat ad lib\b\b

}xig}xig ))

{{print “dup word ‘$1’ at paragraph $.\n”;print “dup word ‘$1’ at paragraph $.\n”;

}}}}

Page 8: Perl, Beyond the Basics: Regular Expressions, Subroutines, and Objects in Perl

88

SubstitutionSubstitution

• Perl provides a simple way of searching Perl provides a simple way of searching for patterns and substituting new patterns for patterns and substituting new patterns in their place. in their place.

• This accomplished by using a This accomplished by using a ss before a before a slash delimited regular expression. slash delimited regular expression.

s/string1/string2/i s/string1/string2/i # replaces the first instance of string1 with# replaces the first instance of string1 with# with string 2 the /i forces a case sensitive # with string 2 the /i forces a case sensitive # search# search

Page 9: Perl, Beyond the Basics: Regular Expressions, Subroutines, and Objects in Perl

99

FunctionsFunctions

• A subprogram in Perl is a A subprogram in Perl is a function.function.

• An ampersand is placed before the function name to An ampersand is placed before the function name to denote invocation.denote invocation.

• If the function takes arguments, they are placed If the function takes arguments, they are placed within parentheses following the name of the within parentheses following the name of the function (the parentheses may be omitted).function (the parentheses may be omitted).

&aFunction();&aFunction();&aFunction($arg1, @arg2);&aFunction($arg1, @arg2);

• Control is transferred to the code of the function and Control is transferred to the code of the function and transferred back at the end of the code or when an transferred back at the end of the code or when an explicit explicit returnreturn operator is reached operator is reached..

Page 10: Perl, Beyond the Basics: Regular Expressions, Subroutines, and Objects in Perl

1010

Function DefinitionFunction Definition

• The function definition is marked by the keyword The function definition is marked by the keyword sub followed by the name of the function (without an sub followed by the name of the function (without an ampersand prefix). The function body is enclosed in ampersand prefix). The function body is enclosed in curly brackets.curly brackets.

sub aFunction {sub aFunction {stmt_1;stmt_1;stmt_2;stmt_2;……stmt_n;stmt_n;

}}

• The value returned by a function is the value of the last The value returned by a function is the value of the last expression evaluated.expression evaluated.

• Functions can not be nested.Functions can not be nested.

Page 11: Perl, Beyond the Basics: Regular Expressions, Subroutines, and Objects in Perl

1111

ArgumentsArguments

• The arguments to a function constitute a list. They The arguments to a function constitute a list. They are available within the function definition through are available within the function definition through the predefined list variable the predefined list variable @_@_

&aFunction ($a, “hello”, $b);&aFunction ($a, “hello”, $b);

sub aFunction {sub aFunction {foreach $temp (@_) {foreach $temp (@_) {

print “$temp \n”;print “$temp \n”; }}}}

Page 12: Perl, Beyond the Basics: Regular Expressions, Subroutines, and Objects in Perl

1212

Variable ScopeVariable Scope

• Variables defined within the body of a Perl program Variables defined within the body of a Perl program are a available inside a Perl function as global are a available inside a Perl function as global variables.variables.

• Perl offers 3 scoping declarations:Perl offers 3 scoping declarations:– mymy e.g., my nose;e.g., my nose;– ourour e.g., our $housee.g., our $house– locallocal $local $Tvchannel$local $Tvchannel

• mymy and and locallocal can both be used (although they work in can both be used (although they work in different ways) to limit the scope of variables that are different ways) to limit the scope of variables that are intended to be local to a function.intended to be local to a function.

Sub aFunction {Sub aFunction {my ($local1, $local2);my ($local1, $local2);……

}}

Page 13: Perl, Beyond the Basics: Regular Expressions, Subroutines, and Objects in Perl

1313

Object Orientated Programming Object Orientated Programming in Perlin Perl

• Part 1: References and PackagesPart 1: References and Packages

• Part 2: Object Properties and Object MethodsPart 2: Object Properties and Object Methods