1 Perl Perl basics Perl Elements Arrays and Hashes Control statements Operators OOP in Perl
2 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
3 Perl Basics Scripting language Strengths in text processing Descendant of –C, Lisp, shell scripting (sh), … Created by Larry Wall Released in 1987 Links – –documentation: perldoc.perl.orgperldoc.perl.org –software download:
4 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
5 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"
6 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
7 Perl Features Procedural programming Object-Oriented (OO) programming Powerful built-in text processing Very large collection of third-party modules
8 Imperative Style Perl Procedural / Imperative style –variables –expressions –assignment –code blocks {} –control statements –subprograms
9 Variables Leading "sigils" identify the data type of variables – $ scalar my $grade = 'A+'; array = (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
10 Data Structures Data structures –Arrays (as Llists in Lisp) –Hashes (associative arrays – as in AWK) –Regular expressions (as in sed) Different braces –Arrays: [] = (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
11 Operations on Arrays and Hashes # # all but 1st and last element Array operations = = Keys and values of hashes = keys %course_grades; = values %course_grades;
12 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";
13 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;
14 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 { # 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"; }
15 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 = {313, 415}; denotes an array print "Hi,$dept$nrs[$#nrs]"; #: Hi,ics415
16 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