Presentation is loading. Please wait.

Presentation is loading. Please wait.

Practical Extraction & Report Language PERL Joseph Beltran.

Similar presentations


Presentation on theme: "Practical Extraction & Report Language PERL Joseph Beltran."— Presentation transcript:

1 Practical Extraction & Report Language PERL Joseph Beltran

2 What is PERL? An interpreted language that is optimized for I/O, system tasks and string manipulation An interpreted language that is optimized for I/O, system tasks and string manipulation Larry Wall originally created PERL because he sought the need for a language that combines the best features of other scripting languages Larry Wall originally created PERL because he sought the need for a language that combines the best features of other scripting languages

3 Uses of PERL Text Processing Text Processing can manipulate textual data, email, news articles, log files, or just about any kind of text, with great ease can manipulate textual data, email, news articles, log files, or just about any kind of text, with great ease System Administration System Administration particularly useful for tying together lots of smaller scripts, working with file systems, networking, & so on particularly useful for tying together lots of smaller scripts, working with file systems, networking, & so on CGI and Web Programming CGI and Web Programming can be used to process and generate HTML can be used to process and generate HTML Other Uses Other Uses DNA sequencing for The Human Genome Project DNA sequencing for The Human Genome Project NASA’s Satellite Systems Control NASA’s Satellite Systems Control Perl Data Language for number-crunching Perl Data Language for number-crunching Perl Object Environment for event-driven machines Perl Object Environment for event-driven machines

4 Variables PERL provides three kinds of variables: PERL provides three kinds of variables: Scalars, Arrays, and Associative Arrays The initial character of the name identifies the particular type of variable and, hence, its functionality. The initial character of the name identifies the particular type of variable and, hence, its functionality.

5 Variables Scalar Variables Scalar Variables$name Strings and numbers whether integers or decimals are treated in the same way Strings and numbers whether integers or decimals are treated in the same way $aVar = 4; $aVar = 4; $bVar = 4.5; # a decimal number $bVar = 4.5; # a decimal number $cVar = 3.14e10; # a floating point number $cVar = 3.14e10; # a floating point number $dVar = "a string of words“; $dVar = "a string of words“; $eVar = $aVar. $bVar; # note use of. to concatenate strings $eVar = $aVar. $bVar; # note use of. to concatenate strings

6 Variables Arrays Arrays@name() Single dimension list of scalars Single dimension list of scalars @aList = (2, 4, 6, 8); # explicit values @aList = (2, 4, 6, 8); # explicit values @aList = (1..4); # range of values\ @aList = (1..4); # range of values\ @aList = (1, "two", 3, "four"); # mixed values @aList = (1, "two", 3, "four"); # mixed values @aList = (); # empty list @aList = (); # empty list $#aList; # index of last item $#aList; # index of last item $aList[0]; # first item in @aList $aList[0]; # first item in @aList

7 Variables Associative Arrays Associative Arrays%name{} A two-dimensional array, for use with attribute/value pairs. A two-dimensional array, for use with attribute/value pairs. The first element in each row is a key and the second element is a value associated with that key. The first element in each row is a key and the second element is a value associated with that key. $aAA{"A"} = 1; # creates first row of associative array $aAA{"A"} = 1; # creates first row of associative array $aAA{"B"} = 2; # creates second row of associative array $aAA{"B"} = 2; # creates second row of associative array %aAA = ("A", 1, "B", 2); # same as first two statements %aAA = ("A", 1, "B", 2); # same as first two statements

8 Operators If variables are the nouns, PERL provides operators, which are the verbs. If variables are the nouns, PERL provides operators, which are the verbs. Operators access and change the values of variables. Operators access and change the values of variables. Some assignments apply to all three kinds of variables. However, most are specialized with respect to their types. Some assignments apply to all three kinds of variables. However, most are specialized with respect to their types.

9 Operators Numeric Operators Numeric Operators + plus + plus - minus - minus * multiply * multiply / divide / divide ** exponentiation ** exponentiation % modulus % modulus == equal == equal != not equal != not equal < less than < less than > greater than > greater than <= less than or equal to <= less than or equal to >= greater than or equal to >= greater than or equal to += binary assignment += binary assignment -= same, subtraction -= same, subtraction *= same, multiplication *= same, multiplication ++ auto increment ++ auto increment -- auto decrement -- auto decrement

10 Literal Operators Literal Operators. concatenate. concatenate x n repetition # e.g., "A" x 3 => "AAA" x n repetition # e.g., "A" x 3 => "AAA" eq equal eq equal ne not equal ne not equal lt less than lt less than gt greater than gt greater than le less than or equal to le less than or equal to ge greater than or equal to ge greater than or equal to Operators

11 Control Structures PERL is an iterative language in which control flows from the first statement in the program to the last statement unless something interrupts. PERL is an iterative language in which control flows from the first statement in the program to the last statement unless something interrupts. Some of the things that can interrupt this linear flow are conditional branches and loop structures. Some of the things that can interrupt this linear flow are conditional branches and loop structures.

12 Control Structures If Conditional Statement If Conditional Statement if (expression_A) if (expression_A){A_true_stmt_1;A_true_stmt_2;} elseif (expression_B) {B_true_stmt_1;B_true_stmt_2;}elsefalse_stmt_1;

13 Control Structures While Loop Statement While Loop Statement LABEL: while (expression) {stmt_1;stmt_2;} Until Loop Statement Until Loop Statement LABEL: until (expression) {stmt_1;stmt_2;}

14 Control Structures For Loop Statement For Loop Statement LABEL: for (initial exp; test exp; increment exp) {stmt_1;stmt_2;} For Each Loop Statement For Each Loop Statement LABEL: foreach $i (@aList) {stmt_1;stmt_2;}

15 Input / Output PERL uses filehandles to control input & output PERL uses filehandles to control input & output These are STDIN for accessing input, STDOUT for printing output, and STDERR for writing error messages These are STDIN for accessing input, STDOUT for printing output, and STDERR for writing error messages Additional filehandles are created by the open command Additional filehandles are created by the open command

16 Input / Output Opening Files Opening Files Syntax: open (FILEHANDLE, "filename"); Examples: open (INPUT, "index.html"); # for reading open (OUTPUT, "> index.html"); # for writing open (OUTPUT, ">> index.html"); # for appending Closing Files Closing Files Syntax: close (FILEHANDLE); Example: close (INPUT); close (INPUT);

17 Regular Expressions Regular expressions give us extreme power to do pattern matching on text documents. Regular expressions give us extreme power to do pattern matching on text documents. Patterns Patterns Literal String Pattern Literal String Pattern if (/cat/) { print "cat found in $a"; } Single-Character Pattern Single-Character Pattern /.at/ # matches "cat,“ and "bat“ /[0-9]/ # matches 0 to 9 /[0123456789]/ # matches 0 to 9

18 Regular Expressions Operators: Operators: Substitution Substitution s/cat/dog/ # replaces "cat" with "dog“ s/cat/dog/ # replaces "cat" with "dog“ s/cat/dog/gi # same but ignores case s/cat/dog/gi # same but ignores case Splitting Splitting @a = split(/cat/, $a); # removes “cat” from $a @a = split(/cat/, $a); # removes “cat” from $a Joining Joining $a = join (“cat", "dog", "bird"); # returns "catdogbird" $a = join (“cat", "dog", "bird"); # returns "catdogbird"

19 Examples Example 1 Example 1 STDIN and STDOUT, Looping and Conditions STDIN and STDOUT, Looping and Conditions Example 2 Example 2 SEARCH and REPLACE strings SEARCH and REPLACE strings Example 3 Example 3 FILE READING and WRITING FILE READING and WRITING Sample scripts are run using Active Perl 5.6 Sample scripts are run using Active Perl 5.6 from www.activestate.com from www.activestate.com


Download ppt "Practical Extraction & Report Language PERL Joseph Beltran."

Similar presentations


Ads by Google