100 likes | 288 Views
Perl Subroutines. Monday, November 14, 2011. Subroutine Basics. To call a subroutine, use the & @_ returns the number of parameters $num_params = @_; @_ contains the actual parameters $_[0], $_[1], … A function can return a value with the return statement
E N D
Perl Subroutines Monday, November 14, 2011
Subroutine Basics • To call a subroutine, use the & • @_ returns the number of parameters • $num_params = @_; • @_ contains the actual parameters • $_[0], $_[1], … • A function can return a value with the return statement • If no return statement is executed, the subroutine will return the value of the final statement that was performed
Subroutine Example #!/usr/local/bin/perl ($len, $wid, $price) = (75, 80, 3.25); $cost=&fenceCost($len, $wid, $price); print "The total cost is $cost\n"; sub fenceCost { ($l,$w,$p) = @_; my $perimeter = 2 * $l + 2 * $w; my $total = $perimeter * $p; return $total; }
Review: Scope • Global scope refers to a variable that is available to all parts of a script and exists as long as the script is running • Local scope refers to a variable that has limited scope and might pop in and out of existence depending on what part of the script is currently executing • Global variables are easy to create and easy to use in Perl • Any variable not explicitly declared with local or my is automatically a global variable.
Why global variables are evil • Large scripts are more difficult to debug when only globals are used • Where did that variable come from? • When does it get updated? • What is it doing? • Variable names may accidentally be used multiple times • Creating reusable Perl libraries is a nightmare
Perl: Local vs. My • Local followed by some variables specifies that those variables will be local to the currently executing block, loop, or subroutine. The variable is also available to all nested subroutines. • My followed by some variables specifies that those variables are local to the current block, loop, or subroutine. It is not available to nested subroutines. • If more than one variable is supplied, they must be placed within parenthesis: • local ($x, $y);
Example: Local vs. My #!/usr/local/bin/perl &yippee (5, 6); exit; sub yippee { local ($first) = $_[0]; my ($second) = $_[1]; print "1: First is $first Second is $second\n"; &yahoo(); print "2: First is $first Second is $second\n"; } sub yahoo { print "3: First is $first Second is $second\n"; &yuk($first, $second); print "4: First is $first Second is $second\n"; &yuk($first, $second); print "5: First is $first Second is $second\n"; } sub yuk { local ($dog) = $_[0]; my ($cat) = $_[1]; print "6: Cat is $cat Dog is $dog\n"; $first *= 2; $second *= 3; } $first $second X 20 X 10 5 6 1: First is 5 Second is 6 3: First is 5 Second is 6: Cat is Dog is 5 4: First is 10 Second is 0 6: Cat is 0 Dog is 10 5: First is 20 Second is 0 2: First is 20 Second is 6 $second 0 $dog $cat X 10 5 0
Reference Parameters • The argument list that you get inside your subroutine via @_ are implicit references to the values that were passed in from outside. • If you modify the contents of the @_ array itself inside the body of the subroutine, variables passed as parameters will have their values modified, respectively.
Reference Parameters #!/usr/local/bin/perl @prices = (24.95, 35.75, 55.69, 88.59); print "Prices before the sale\n"; &show(@prices); &sale(@prices); print "Prices after the sale\n"; &show(@prices); sub show { for (my $i=0; $i < @_; $i++) { printf("\$%4.2f\n", $prices[$i]); } } sub sale { for (my $i=0; $i < @_; $i++) { $_[$i] *= 0.95; } }