Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Perl Subroutines

ynonperek
November 13, 2011

Perl Subroutines

Syntax & best practices for using subroutines in perl

ynonperek

November 13, 2011
Tweet

More Decks by ynonperek

Other Decks in Programming

Transcript

  1. 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
  2. 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
  3. 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
  4. 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
  5. Shorter Code ✦ Shorter code is easier to read and

    edit ✦ Shorter code is fun Saturday, October 1, 2011
  6. Less Buggy ✦ No repetition means less code - less

    to get wrong ✦ No more copy-paste bugs Saturday, October 1, 2011
  7. 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
  8. 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
  9. Define A Sub sub greet { for ( 1..10 )

    { print “Take me to your leader\n”; } } Saturday, October 1, 2011
  10. 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
  11. 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
  12. 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
  13. 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
  14. 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
  15. 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
  16. 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
  17. Output Parameter sub sum_of_digits { my ($number) = @_; my

    $sum = 0; while ($number) { $sum += chop($number); } return $sum; } Saturday, October 1, 2011
  18. Output Parameter my $x = sum_of_digits(2343); my $y = sum_of_digits(1234);

    print “gap is: “, $x - $y, “\n”; Saturday, October 1, 2011
  19. 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
  20. Default Parameters sub print_times { my ($text, $times) = @_;

    $times = 5 if ! defined($times); print $text x $times; } Saturday, October 1, 2011
  21. 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
  22. 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
  23. 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
  24. 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
  25. 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
  26. 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
  27. 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
  28. 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
  29. 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
  30. 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