Perl Tutorial-Part II CSCI 3136 Principles of Programming and Languages Slides mainly taken from Perl Programming, Quentin Smith 1 http://stuff.mit.edu/iap/perl/
Outline • Run a Perl Program • Data Types • Context • Operators • Flow Control • Subroutines • References • Files • Regular Expressions Slides mainly taken from Perl Programming, Quentin Smith 2 http://stuff.mit.edu/iap/perl/
Run a Perl Program • on bluenose: – perl hello.pl – Add “#!/ usr/bin/perl ” to the first line of hello.pl – chmod a+x hello.pl – ./hello.pl Slides mainly taken from Perl Programming, Quentin Smith 3 http://stuff.mit.edu/iap/perl/
Data Types • Scalar – Singles values – Name preceded by a $ character • Arrays – Multiple ordered values – Name preceded by a @ character • Hashes – Multiple unordered values – Name preceded by a % character Slides mainly taken from Perl Programming, Quentin Smith 4 http://stuff.mit.edu/iap/perl/
Context • Context determines how variables and values are evaluated • Numeric context • String context • Boolean context • Determined by operators Slides mainly taken from Perl Programming, Quentin Smith 5 http://stuff.mit.edu/iap/perl/
Operators • Numeric – ++, --, +, -, *, /, +=, *=, >>, << • String – Concatenation (.), repetition (x), assignment (.=, x=) • Quoting – qq (“), q (‘), qx(`), qw, pattern matching • Boolean – <, >, ==, !=, =>, <= – lt, gt, eq, ne, le, ge – &&, ||, !, and, or, not – ? : • List – sort, reverse, push/pop, unshift/shift, split/join, grep, map Slides mainly taken from Perl Programming, Quentin Smith 6 http://stuff.mit.edu/iap/perl/
Flow Control • Flow control is expressive in Perl • Conditional statements – if – unless • Loop statements – while – until – for – foreach • Modifiers – Simple statements may be modified • See perldoc perlsyn for complete syntax Slides mainly taken from Perl Programming, Quentin Smith 7 http://stuff.mit.edu/iap/perl/
Conditional Statements • Conditional statements provide boolean context • if statement controls the following block – if, elsif, else – Yes, that does say elsif not else if • unless is opposite of if – Equivalent to if (not $boolean) – unless, elsif, else – There is no elsunless , thankfully. Slides mainly taken from Perl Programming, Quentin Smith 8 http://stuff.mit.edu/iap/perl/
Conditional Statements • Example: my ($a, $b) = (0, 1); if (!$a && !$b) {print "Neither\n";} # Conventional if (not $a and not $b) {print "Neither\n";} # Same, but in English if (not ($a or $b)) {print "Neither\n";} # Same, but parentheses unless ($a or $b) {print "Neither\n";} # Same, but clearest ($a,$b) = (1,0); unless($a) { print “a is not true”; }elsif($b) { print “b is true”; }else{ print “a is true but b is not true”; } Slides mainly taken from Perl Programming, Quentin Smith 9 http://stuff.mit.edu/iap/perl/
Loop Statements • Loop statements provide a boolean context • while – Loops while boolean is true • until – Loops until boolean is true – Opposite of while • do – At least one loop, then depends on while or until Slides mainly taken from Perl Programming, Quentin Smith 10 http://stuff.mit.edu/iap/perl/
Loop Statements my $counter = 10; while ($counter >= 0) { • Example: print $counter.”,”; $counter--; } Output: 10,9,8,7,6,5,4,3,2,1,0, $counter = 5; until ($counter < 0) { print $counter.”,” $counter--; } Output: 5,4,3,2,1,0, $counter = 0; do { print $counter.”,” $counter--; } while ($counter >= 0); Output: 0, Slides mainly taken from Perl Programming, Quentin Smith 11 http://stuff.mit.edu/iap/perl/
Loop Statements • for – Like C, C++, Java: for (initialization; condition; increment) • foreach – Iterates over a list or array • Good to localize loop variables with my Slides mainly taken from Perl Programming, Quentin Smith 12 http://stuff.mit.edu/iap/perl/
Loop Statements • Example for (my $i = 10; $i >= 0; $i--) { print "$i,"; # Countdown } Output: 10,9,8,7,6,5,4,3,2,1,0, foreach my $i (reverse 0..10) { print "$i,\n"; # Same } Output: 10,9,8,7,6,5,4,3,2,1,0, %hash = (dog => "lazy", fox => "quick"); foreach my $key (keys %hash) { print "The $key is $hash{$key}.\n"; # Print out hash pairs } Output: The fox is quick. The dog is lazy. Slides mainly taken from Perl Programming, Quentin Smith 13 http://stuff.mit.edu/iap/perl/
Modifiers • Simple statements can take single modifiers – Places emphasis on the statement, not the control – Can make programs more legible – Parentheses usually not needed – Good for setting default values – Valid modifiers are if, unless, while, until, foreach Slides mainly taken from Perl Programming, Quentin Smith 14 http://stuff.mit.edu/iap/perl/
Modifiers my $default = 0; my $a = $default unless defined $a; • Example: my $b = 3; $b = $default unless defined $b; print “$ a,$b\ n”; Output: 0,3 my $balance = 5000; $balance += $deposit if $deposit; print “$balance \ n”; Output: 5000 my withdrawal = 300; $balance -= $withdrawal if $withdrawal and $withdrawal <= $balance; Print “$balance \ n”; Output: 4700 Slides mainly taken from Perl Programming, Quentin Smith 15 http://stuff.mit.edu/iap/perl/
Subroutines • Subs group related statements into a single task • Perl allows both declared and anonymous subs • Perl allows various ways of handling arguments • Perl allows various ways of calling subs • perldoc perlsub gives complete description Slides mainly taken from Perl Programming, Quentin Smith 16 http://stuff.mit.edu/iap/perl/
Declaring Subroutines • Subroutines are declared with the sub keyword • Subroutines return values – Explicitly with the return command – Implicitly as the value of the last executed statement • Return values can be a scalar or a flat list – wantarray describes what context was used – Unused values are just lost Slides mainly taken from Perl Programming, Quentin Smith 17 http://stuff.mit.edu/iap/perl/
Declaring Subroutines sub ten { • Example: @array = (1..10); return wantarray() ? @array : 100; } @ten = ten(); # (1, 2, 3, 4, 5, 6, 7, 8, 9, 10) $ten = ten(); # 100 ($ten) = ten(); # (1) ($one, $two) = ten(); # (1, 2) ---------------------------------------------------------------------- sub ten{ @array = (1..10); return @array; } @ten = ten(); # (1, 2, 3, 4, 5, 6, 7, 8, 9, 10) $ten = ten(); # 10 Slides mainly taken from Perl Programming, Quentin Smith 18 http://stuff.mit.edu/iap/perl/
Handling Arguments • Two common means of passing arguments to subs – Pass by value – Pass by reference – Perl allows either • Arguments are passed into the @_ array – @_ is the "fill in the blanks" array – Usually should copy @_ into local variables Slides mainly taken from Perl Programming, Quentin Smith 19 http://stuff.mit.edu/iap/perl/
Handling Arguments • Example: sub add_one { # Like pass by value my ($n) = @_; # Copy first argument return ($n + 1); # Return 1 more than argument } sub plus_plus { # Like pass by reference $_[0] = $_[0] + 1; # Modify first argument } # no return statement my ($a, $b) = (10, 0); add_one($a); # Return value is lost, nothing changes $b = add_one($a); # $a is 10, $b is 11 plus_plus($a); # Return value lost, but a now is 11 $b = plus_plus($a); # $a and $b are 12 Slides mainly taken from Perl Programming, Quentin Smith 20 http://stuff.mit.edu/iap/perl/
Calling Subroutines • Subroutine calls usually have arguments in parentheses – Parentheses are not needed if sub is declared first – But using parentheses is often good style • Subroutine calls may be recursive • Subroutines are another data type – Name may be preceded by an & character – & is not needed when calling subs Slides mainly taken from Perl Programming, Quentin Smith 21 http://stuff.mit.edu/iap/perl/
Calling Subroutines • Example: print factorial(5) . "\n"; # Parentheses required sub factorial { my ($n) = @_; return $n if $n <= 2; $n * factorial($n - 1); } print ((factorial 5) . "\n"); # Parentheses around argument not required, # but need to ensure there are no extra arguments print &factorial(5) . "\n"; # Neither () nor & required Slides mainly taken from Perl Programming, Quentin Smith 22 http://stuff.mit.edu/iap/perl/
Subroutines • Example: – Declare subroutine – Copy arguments – Check arguments – Perform computation – Return results Slides mainly taken from Perl Programming, Quentin Smith 23 http://stuff.mit.edu/iap/perl/
Recommend
More recommend