1 / 13

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

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.

Download Presentation

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

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


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

  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: ModifierMeaning 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 /http:/; }

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

  6. 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

  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(); &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.

  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 variable @_ &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

More Related