Slide 1

Slide 1 text

Subroutines Basic Perl Saturday, October 1, 2011

Slide 2

Slide 2 text

Spot The Bug use strict; use warnings; my $a = <>; my $b = <>; my $sum_a = 0; while ($a) { my $digit = $a % 10; $sum_a += $digit; $a = int($a/10); } my $sum_b = 0; while($b) { my $digit = $a % 10; $sum_b += $digit; $b = int($b/10); } print "gap is: ", $sum_a - $sum_b, "\n"; Saturday, October 1, 2011

Slide 3

Slide 3 text

Spot The Bug use strict; use warnings; my $a = <>; my $b = <>; my $sum_a = 0; while ($a) { my $digit = $a % 10; $sum_a += $digit; $a = int($a/10); } my $sum_b = 0; while($b) { my $digit = $a % 10; $sum_b += $digit; $b = int($b/10); } print "gap is: ", $sum_a - $sum_b, "\n"; Saturday, October 1, 2011

Slide 4

Slide 4 text

Don’t Repeat Yourself Saturday, October 1, 2011

Slide 5

Slide 5 text

Subroutines ✦ A named piece of code is called a subroutine ✦ Calling a subroutine executes the code ✦ Subroutines save us from breaking DRY Saturday, October 1, 2011

Slide 6

Slide 6 text

Now, Where’s That Bug use strict; use warnings; my $a = <>; my $b = <>; my $sum_a = sum_of_digits($a); my $sum_b = sum_of_digits($b); print "gap is: ", $sum_a - $sum_b, "\n"; Saturday, October 1, 2011

Slide 7

Slide 7 text

5IF (PPE Saturday, October 1, 2011

Slide 8

Slide 8 text

Shorter Code ✦ Shorter code is easier to read and edit ✦ Shorter code is fun Saturday, October 1, 2011

Slide 9

Slide 9 text

Less Buggy ✦ No repetition means less code - less to get wrong ✦ No more copy-paste bugs Saturday, October 1, 2011

Slide 10

Slide 10 text

Helping Friends ✦ Sharing is caring ✦ Subroutines make our code easier to share ✦ Team members don’t need to understand every line Saturday, October 1, 2011

Slide 11

Slide 11 text

Define A Sub ✦ Subroutines are defined with the keyword sub followed by a block ✦ Subroutines can be defined anywhere in the program file. ✦ No formal parameter checking Saturday, October 1, 2011

Slide 12

Slide 12 text

Define A Sub sub greet { for ( 1..10 ) { print “Take me to your leader\n”; } } Saturday, October 1, 2011

Slide 13

Slide 13 text

Call A Sub ✦ Call a sub by specifying its name ✦ Use of parens is recommended for user defined subroutines ✦ Old code uses & before the sub name Saturday, October 1, 2011

Slide 14

Slide 14 text

Call A Sub greet(); greet; chomp($text); print “Hello\n”; Saturday, October 1, 2011

Slide 15

Slide 15 text

Input Parameters ✦ A subroutine can take a list of input parameters that affect its execution ✦ print takes “what to print” ✦ chomp takes some text to chomp ✦ length takes the text to count Saturday, October 1, 2011

Slide 16

Slide 16 text

Input Parameters ✦ Inside a subroutine the special list @_ contains all parameters that were passed to it ✦ Copy it to local variables for safety Saturday, October 1, 2011

Slide 17

Slide 17 text

Input Parameters sub sum_of_digits { my ($number) = @_; my $sum = 0; while ($number) { $sum += chop($number); } print “sum is: $sum\n”; } Saturday, October 1, 2011

Slide 18

Slide 18 text

Input Parameters ✦ Perl makes it easy to use all input parameters as an array ✦ Iterate over the array to implement generic functions such as: sum, max, min, etc. ✦ List::Util already includes them Saturday, October 1, 2011

Slide 19

Slide 19 text

Q & A Saturday, October 1, 2011

Slide 20

Slide 20 text

Exercise ✦ Write a function that takes a string as input and prints how many numbers it has ✦ Write a function that takes many strings, and prints the total number of characters ✦ Write a function that takes a list of numbers and prints the sum of all even numbers Saturday, October 1, 2011

Slide 21

Slide 21 text

Output Parameter ✦ Every function has a return value ✦ The return value is used on the caller’s perspective ✦ Default return value is the last execution line ✦ Use return to set return value Saturday, October 1, 2011

Slide 22

Slide 22 text

Output Parameter sub sum_of_digits { my ($number) = @_; my $sum = 0; while ($number) { $sum += chop($number); } return $sum; } Saturday, October 1, 2011

Slide 23

Slide 23 text

Output Parameter my $x = sum_of_digits(2343); my $y = sum_of_digits(1234); print “gap is: “, $x - $y, “\n”; Saturday, October 1, 2011

Slide 24

Slide 24 text

Default Parameters ✦ Assigning a default value to a parameter happens inside the function ✦ If @_ is shorter than you expect, you should assign defaults (or leave) Saturday, October 1, 2011

Slide 25

Slide 25 text

Default Parameters sub print_times { my ($text, $times) = @_; $times = 5 if ! defined($times); print $text x $times; } Saturday, October 1, 2011

Slide 26

Slide 26 text

Bend Or Break Saturday, October 1, 2011

Slide 27

Slide 27 text

Bend || Break ✦ Bend means if you can handle a surprise, handle it well using default variables. ✦ Break means if you can’t handle a surprise, you better make an issue Saturday, October 1, 2011

Slide 28

Slide 28 text

Break ✦ When things go really wrong - die or croak ✦ die leaves the subroutine immediately and throws an error. ✦ If the error is not handled, program will end ✦ croak is the more informative version of die. Saturday, October 1, 2011

Slide 29

Slide 29 text

Break Example sub print_times { my ($text, $times) = @_; die “Don’t know what to say” if ! defined($text); $times = 5 if ! defined($times); print $text x $times; } Saturday, October 1, 2011

Slide 30

Slide 30 text

Which Is True ✦ First print works, second fails ✦ Both prints work ✦ Second print works, first fails sub sum { my ($a, $b) = @_; return $a + $b; } print sum(2, 3), “\n”; print $a; Saturday, October 1, 2011

Slide 31

Slide 31 text

Which Is True ✦ First print works, second fails ✦ Both prints work ✦ Second print works, first fails sub sum { my ($a, $b) = @_; return $a + $b; } print sum(2, 3), “\n”; print $a; Saturday, October 1, 2011

Slide 32

Slide 32 text

Which Is True ✦ Both lines print 5 ✦ Both lines print 2 ✦ First line prints 5, Second line prints 3 ✦ First line prints 3, Seconds line prints 2 sub count { return scalar @_; } print count 2, 3, “\n”; print count(2, 3), “\n”; Saturday, October 1, 2011

Slide 33

Slide 33 text

Which Is True ✦ Both lines print 5 ✦ Both lines print 2 ✦ First line prints 5, Second line prints 3 ✦ First line prints 3, Seconds line prints 2 sub count { return scalar @_; } print count 2, 3, “\n”; print count(2, 3), “\n”; Saturday, October 1, 2011

Slide 34

Slide 34 text

Which Is True ✦ Both lines print 5.14 ✦ Both lines print 3.14 ✦ First line prints 5.14, Second line prints 3.14 ✦ First line prints 3.14, Seconds line prints 5.14 sub PI { 3.14 } print PI + 2, “\n”; print PI() + 2, “\n”; Saturday, October 1, 2011

Slide 35

Slide 35 text

Which Is True ✦ Both lines print 5.14 ✦ Both lines print 3.14 ✦ First line prints 5.14, Second line prints 3.14 ✦ First line prints 3.14, Seconds line prints 5.14 sub PI { 3.14 } print PI + 2, “\n”; print PI() + 2, “\n”; Saturday, October 1, 2011

Slide 36

Slide 36 text

Subroutine Tips ✦ Call your own subroutines with parens ✦ Assign @_ to local named variables ✦ Assign defaults where they make sense ✦ Keep argument list short. Use hashes instead of anonymous long argument list Saturday, October 1, 2011

Slide 37

Slide 37 text

Q & A Saturday, October 1, 2011

Slide 38

Slide 38 text

Thank You ✦ Ynon Perek ✦ [email protected] ✦ ynonperek.com Saturday, October 1, 2011