Unformatted text preview:

Perl: Programming Freedom Fundamentals of Engineering For Honors – H192Perl: Programming FreedomPractical Extraction and Report LanguagePractical? Report? This doesn’t have to do with labs, does it?Perl vs. C: Major DistinctionsPerl vs. C: Syntax and OperatorsPerl Data Types: Scalars ($)Scalar Example With Some TwistsPerl Regular Expressionsexample: $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)dd/ sets $1 to “123” (first set of parentheses) sets $2 to “2” (second set of parentheses)Perl Regular Expression ExamplePerl I/OPerl File I/OPerl File I/O ExampleLearn more than one language? That’s dumb even for a geek.Please End; Really, Leave!Lecture 10APerl: Programming FreedomFundamentals of Engineering For Honors – H192By Robert Mohr,Ted Pavlic, andJoe RyanLecture 10APerl: Programming FreedomWhat is Perl?Why should I use Perl?What does Perl look like? Show me some examples.ConclusionLecture 10APractical Extraction and Report LanguageDebuted on December 18, 1987 (Ted was 6)Invented by Larry Wall–Linguist and Computer ScientistProgramming Perl: 3rd Edition–The essential book on Perl–http://www.ora.com/ (O’Reilly)CS&E 459.51–1 Credit Hour Spring S/U Course on Perl at OSUhttp://www.perl.org/http://www.cpan.org/Lecture 10APractical? 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 10APerl 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 prototypingLecture 10APerl 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: http://www.cpan.org/)Easy object-oriented programmingLecture 10APerl 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 == 25Lecture 10AScalar 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 & constantprint “x = y = $x\t” if ($x == $y); # Compare two scalars# Notice placement of ifwhile($z > $y) { $z--; } # while / decrement scalarprint( “z = $z\n” ); # Print scalar valueprint “Of course $z isn’t the same as $s!\n”if ($z ne $s); # String “not-equal” compareOutputw = 1 x = y = 2 z = 1.5Of course 1.5 isn’t the same as text!Lecture 10APerl 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 variableLecture 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 UsageLecture 10APerl Regular Expression Example# Declare and initialize some arrays of famous people’s namesfor my $prof (“Dr. Demel”, “Dr. Freuler”, “Mr. Clingan”){if ( $prof =~ m/Dr\. (.*)/){print $1,” has a PhD\n”;}}Lecture 10APerl I/OPrint to standard output (screen)–print “text”;print $scalar;print @list; print STDOUT “text”;Read from standard input (keyboard)–$line = <STDIN>;chomp $line;The angle operator (<>) (used very frequently!)–chomp( $line = <> );Lecture 10APerl File I/OOpen a file–open(IN, “filename”); # Open for readingopen(OUT, “>filename”); # Open for writingopen(OUT, “>>filename”); # Open for appendingInput from file referenced by the handle “IN”–$line = <IN>;Print to the file referenced by the handle “OUT”–print OUT “text”;Close a file–close IN;close OUT;Lecture 10APerl File I/O Exampleprint “Enter file to read: “; # Ask user for a filename$filename = <STDIN>; # Retrieve entire linechomp $filename; # Get rid of ‘\n’ if thereopen(IN, $filename); # Open the files (notice fileopen(OUT, “>example.out”); # descriptors IN and


View Full Document

OSU ENGR H192 - Programming Freedom

Documents in this Course
Strings

Strings

22 pages

Load more
Download Programming Freedom
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Programming Freedom and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Programming Freedom 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?