Perl • Perl basics • Perl Elements • Arrays and Hashes • Control statements • Operators • OOP in Perl
Scripting languages • Scripting language • control an application • typically not strongly typed • typically interpreted (or compiled into byte-code) • scripts can be created, modified, executed at run-time • Perl is a prime example of a scripting language • Others • Javascript, ActionScript, • Visual Basic for Applications • Web: ASP, PHP, JSP, Python
Perl Basics • Scripting language • Strengths in text processing • Descendant of • C, Lisp, shell scripting (sh), … • Created by Larry Wall • Released in 1987 • Links • www.perl.org • documentation: perldoc.perl.org • software download: www.activestate.com
Perl Applications • Used for • text handling • parsing • data management • Applications • system administration • client-side scripting in web applications • CGI scripts (Common Gateway Interface) • network programming • GUI development
Design Principles • Stated goals • practical (easy to use, efficient, complete), • rather than beautiful (tiny, elegant, minimal) • To make easy tasks easy and difficult tasks possible • Things that are different should look different • Many features • "There's more than one way to do it" • "The Swiss Army Chainsaw of Programming Languages" • "No unnecessary limits"
Language Specification • There is no written specification or standard • the implementation of the interpreter is the de facto specification of the language • Tolerates exceptions to its rules • Heuristics are used to resolve syntax ambiguities • "In general, built-in functions do what you want, unless you want consistency." [perlfunc(1) manual] • Consequence • bugs are sometimes hard to find
Perl Features • Procedural programming • Object-Oriented (OO) programming • Powerful built-in text processing • Very large collection of third-party modules
Imperative Style Perl • Procedural / Imperative style • variables • expressions • assignment • code blocks {} • control statements • subprograms
Variables • Leading "sigils" identify the data type of variables • $ scalar my $grade = 'A+'; • @ array my @courses = (313, 415); • % hash my %grades = (313 => "A-", 415 => "A+"); my %feelings = ("313" => "hard", "415" => "easy"); • my declares a lexically scoped variable • without my, a variable is global • Variables "interpolate" into strings print "$grade"; #prints the value of $grade
Data Structures • Data structures • Arrays (as Llists in Lisp) • Hashes (associative arrays – as in AWK) • Regular expressions (as in sed) • Different braces • Arrays: [] my @nrs = (1, 2, 3); $nr[0] = $nr[$#nrs]; # $nr[$#nrs] is the last element • Hashes : {} my %grades = (313 => "A+", 415 => "A-"); $grades{313} = $grades{"415"}; • Regular expressions: /// /foo/ # pattern matches "foo" $a =~ s/foo/bar/; # replaces "foo" with "bar" in $a
Operations on Arrays and Hashes • Subarrays @nrs[0,2,1]; # =($nrs[0],nrs[2],nrs[1]) @nrs[1..$#nrs-1]; # all but 1st and last element • Array operations my @sorted = sort @courses; my @backwards = reverse @grades; • Keys and values of hashes my @courses = keys %course_grades; my @grade = values %course_grades;
Operators • Arithmetic and relational • Like Java: +, -, *, /, ==,!=, <, >, <=, >= • Boolean • Like Java: !, &&, || • Also: not, and, or • String • Comparisons: eq, ne, lt, gt, le, ge • Concatenation: . • Compound assignments • E.g.: $a .= "\n"; # same as $a = $a."\n";
Selections • If-clause if (condition) {... } elsif (another_condition ) {... } else {...} • If-not-clause unless (condition) {... #same as if (! condition) } • Also post-condition print "A" if $ics; print "No beach" unless $in_hawaii;
Loops • while and until loops while (condition) {... } until (condition) {... } print "HI from HI\n" while 1; # endless loop • for loop for ($i=0; $i <= $max; $i++) {... } • foreach loop foreach (@array) { # default variable $_ contains an element print "This element is $_\n"; } foreach my $key (keys %hash) { # instead of $_ print "The value of $key is $hash{$key}\n"; }
Hello World #!/usr/bin/perl use strict; #stop on error use warnings; #issue a warning # This is just a comment print "Hello, "; #a string print 'ICS'; #also a string print 313; #a number my $dept = "ics"; #'my' declares a variable my @nrs = {313, 415}; #@ denotes an array print "Hi,$dept$nrs[$#nrs]"; #: Hi,ics415
Beyond Imperative Style • Functional style • First-class functions • Closures as values • eval function • OOP model • References • Packages • Class-based method dispatch • Lexically scoped variables • Automatic garbage collection • Reference counting