130 likes | 283 Views
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.
E N D
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. • 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
Patten Matching • Perl provides a set of modifying characters for string matching, some of these are shown below: ModifierMeaning i matches characters regardless of case s treats string as a single line g globally find all matches e.g.: $a =~/little/i
Example while ($line = <FILE>) { if ($line =~ /http:/) { print $line; } } or while(<FILE>) { print if /http:/; }
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. MetacharacterMeaning . 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
Pattern Matching • Perl also has a set of special characters proceeded with a backslash, some of which are listed below. Special CharacterMeaning \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
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”; } }
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
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(); &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.
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.
Arguments • 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”; } }
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); … }
Pointers (referenes) in Perl $String = 'CSCI' ; $StrPntr = \$String ; print $$StrPntr ; $ArrayPntr = [ 'ATMS', 5, 'CSCI' ] ; print $$ArrayPntr[2] ; print $ArrayPntr->[2] ; $HashPntr = { red => 255, green => 255, blue => 0 ) ; print $HashPntr->{red} ;