Lecture 10A Perl: Programming Freedom Fundamentals of Engineering For Honors – H192 By Robert Mohr, Ted Pavlic, and Joe Ryan
Lecture 10A Perl: Programming Freedom What is Perl? Why should I use Perl? What does Perl look like? Show me some examples. Conclusion
Lecture 10A Practical Extraction and Report Language Debuted on December 18, 1987 (Ted was 6) Invented by Larry Wall – Linguist and Computer Scientist Programming Perl: 3 rd Edition – The essential book on Perl – (O’Reilly) CS&E – 1 Credit Hour Spring S/U Course on Perl at OSU
Lecture 10A Practical? Report? This doesn’t have to do with labs, does it? Originally meant to be a glue language – Many applications, many platforms, many files, much power... With no way to communicate Need for a general purpose tool with a strong ability for text processing and reporting Text Processing? World Wide Web? How could those two possibly relate?! The embodiment of computational synergy?
Lecture 10A Perl vs. C: Major Distinctions Perl is interpreted, C is compiled. Negatives: – Perl requires additional overhead (CPU time, memory, etc.) – Perl code is slower than C, all other things being equal Positives: – Perl is easier to extend & upgrade without recompiling – Perl is not platform-specific, but C is – Perl implements powerful features without needing new architecture – Perl is constantly growing; powerful modules can be easily added – Perl has major security features as part of the language – Perl is great for rapid prototyping
Lecture 10A Perl vs. C: Syntax and Operators Borrows syntax from C, awk, BASIC, Python, Pascal, English, Greek – Extremely familiar, comfortable, and unconventional Flexible operators and constructs – do, for, while, ==, =,, ++, --, +, -, *, /, !, &&, || – foreach, unless, until, =~, eq, ne, and, or, not (and more…) Loosely-typed variables and automatic conversions Built-in text string comparison, parsing, pattern-matching Simple I/O library Hundreds of pre-written modules extend Perl for many tasks – (CPAN: Easy object-oriented programming
Lecture 10A Perl Data Types: Scalars ($) Scalars store numeric and string data (and more!) Syntax: $scalar_name Automatic conversion between numeric and text data types when appropriate $x = 5; $y = -6.7; $z = “foo”; $a = $x + $y;# $a == -1.7 $b = $z. $x;# $b eq “foo5” $t = “hello” x 5;# $t eq “hellohellohellohellohello” $g = $x ** 2;# exponentiation – $g == 25
Lecture 10A Scalar Example With Some Twists # Declare and initialize some scalars to play with $w = 1; $x = 2; $y = “2”; $z = “4.5”; $s = “text”; If (1 == $w) { print “w = 1\t”; }# Compare scalar & constant print “x = y = $x\t” if ($x == $y); # Compare two scalars # Notice placement of if while($z > $y) { $z--; }# while / decrement scalar print( “z = $z\n” );# Print scalar value print “Of course $z isn’t the same as $s!\n” if ($z ne $s); # String “not-equal” compare Output w = 1x = y = 2z = 1.5 Of course 1.5 isn’t the same as text!
Lecture 10A Perl Regular Expressions allow for matching strings against patterns special characters –. matches any character – * matches 0 or more of the preceding character – ? matches 0 or 1 of the preceding character – + matches 1 or more of the preceding character escape sequences – \d matches any (d)igit – \s matches any white(s)pace (tab “\t”, space “ ”, newline “\n”) – \w matches any alphanumeric “(w)ord” character capturing – parentheses in the pattern (e.g., m/(\d)\d/) cause the portion of the string in the parentheses to be captured into a temporary variable
Lecture 10A example: $str =~ m/a*b?\+\d/ matches – 0 or more “a”s (e.g., “”, “a”, “aa”, etc.) followed by – 0 or 1 “b”s (e.g., “”, “b”) followed by – a literal “+” (the backslash escapes the “+”) followed by – 1 digit 0-9 (\d – (d)igit) example: “12345” =~ m/\d(\d)\d(\d)\d/ – sets $1 to “2” (first set of parentheses) – sets $2 to “4” (second set of parentheses) example: “12345” =~ m/(\d(\d)\d)\d\d/ – sets $1 to “123” (first set of parentheses) – sets $2 to “2” (second set of parentheses) Perl Regular Expression Usage
Lecture 10A Perl Regular Expression Example # Declare and initialize some arrays of famous people’s names for my $prof (“Dr. Demel”, “Dr. Freuler”, “Mr. Clingan”) { if ( $prof =~ m/Dr\. (.*)/) { print $1,” has a PhD\n”; }
Lecture 10A Perl I/O Print to standard output (screen) – print “text”; print $scalar; print STDOUT “text”; Read from standard input (keyboard) – $line = ; chomp $line; The angle operator (<>) (used very frequently!) – chomp( $line = <> );
Lecture 10A Perl File I/O Open a file – open(IN, “filename”);# Open for reading open(OUT, “>filename”);# Open for writing open(OUT, “>>filename”);# Open for appending Input from file referenced by the handle “IN” – $line = ; Print to the file referenced by the handle “OUT” – print OUT “text”; Close a file – close IN; close OUT;
Lecture 10A Perl File I/O Example print “Enter file to read: “;# Ask user for a filename $filename = ;# Retrieve entire line chomp $filename;# Get rid of ‘\n’ if there open(IN, $filename);# Open the files (notice file open(OUT, “>example.out”);# descriptors IN and OUT) while($line = ) {# Print out to screen/file print $line; print OUT $line; } close OUT;# Close file handles close IN;
Lecture 10A Learn more than one language? That’s dumb even for a geek. Perl motto: TMTOWTDI There’s More Than One Way To Do It
Lecture 10A Please End; Really, Leave! Any questions?