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
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
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
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
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
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
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
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
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
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
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
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