1>: Defining a Subroutinesub marine {
$n += 1; # Global variable $n
print "Hello, sailor number $n!\n";
}
(If you have two subroutine definitions with the same name, the later one overwrites the earlier one.)
2>: Invoking a Subroutine
&marine; # says Hello, sailor number 1!
3>: Return Value
(the last expression evaluated will be the return value)
sub sum_of_fred_and_barney {
print "Hey, you called the sum_of_fred_and_barney subroutine!\n";
$fred + $barney; # That's not really the return value!
print "Hey, I'm returning a value now!\n"; # Oops!
} # Its return value will normally be 1, meaning “printing was successful"..
sub larger_of_fred_or_barney {
if ($fred > $barney) {
$fred;
} else {
$barney;
}
}
4>: Arguments
$n = &max(10, 15); # This sub call has two parameters
the parameter list must be stored into some array: " @_ "
use it: $_[0], $_[1] ..
sub max {
# Compare this to &larger_of_fred_or_barney
if ($_[0] > $_[1]) {
$_[0];
} else {
$_[1];
}
}
$n = &max(10, 15, 27); # Oops! Excess parameters are ignored
" @_ " is always the parameter list for the current subroutine invocation.
5>: Private Variables in Subroutines
sub max {
if (@_ != 2){ # uses the “name” of the array in a scalar context
print "WARNING! &max should get exactly two arguments!\n";
}
my($m, $n) = @_; # Name the subroutine parameters
if ($m > $n) { $m } else { $n }
}
6>: A Better &max Routine
$maximum = &max(3, 5, 10, 4, 6);
sub max {
my($max_now) = shift(@_); # the first one is the largest yet seen
foreach (@_) { # look at the remaining arguments
if ($_ >$max_now){ # could this one be bigger yet?
$max_now = $_;
}
}
$max_now;
}
Empty Parameter Lists (the return value may be undef)
7>: Notes on Lexical (my) Variables
my($num) = @_; # list context, gets the first parameter
my $num = @_; # scalar context, gets the number of parameters
my $fred, $barney; # WRONG! Fails to declare $barney
my($fred, $barney); # declares both
my @phone_number;
(Any new variable will start out empty—undef for scalars, or the empty list for arrays.)
8>: The use strict Pragma
use strict; # Enforce some good programming rules
(Most people recommend that programs that are longer than a screenful of text generally
need use strict. And we agree.)
9>: The return Operator
(many Perl programmers believe it’s just an extra seven characters of typing.)
my @names = qw/ fred barney betty dino wilma pebbles bamm-bamm /;
my $result = &which_element_is("dino", @names);
sub which_element_is {
my($what, @array) = @_;
foreach (0..$#array) { # indices of @array's elements
if ($what eq $array[$_]) {
return $_; # return early once found
}
}
−1; # element not found (return is optional here)
}
10>: Omitting the Ampersand
(the subroutine definition before invocation)
sub division {
$_[0] / $_[1]; # Divide first param by second
}
my $quotient = division 355, 113; # No & necessary uses &division
(if the subroutine has the same name as a Perl built-in, you must use the ampersand to call it.)
sub chomp {
print "Munch, munch!\n";
}
&chomp; # That ampersand is not optional!
11>: Nonscalar Return Values
sub list_from_fred_to_barney {
if ($fred < $barney) {
# Count upwards from $fred to $barney
$fred..$barney;
} else {
# Count downwards from $fred to $barney
reverse $barney..$fred;
}
}
$fred = 11;
$barney = 6;
@c = &list_from_fred_to_barney; # @c gets (11, 10, 9, 8, 7, 6)
(A return with no arguments will return undef in a scalar context or an empty list in a list context.)
12>: Persistent, Private Variables
use strict;
use warnings;
use 5.12.2;
running_sum( 5, 6 );
running_sum( 1..3 );
running_sum( 4 );
sub running_sum {
state $sum = 0;
state @numbers;
foreach my $number ( @_ ) {
push @numbers, $number;
$sum += $number;
}
say "The sum of (@numbers) is $sum";
}
The sum of (5 6) is 11
The sum of (5 6 1 2 3) is 17
The sum of (5 6 1 2 3 4) is 21
state @array = qw(a b c); # Error!
Initialization of state variables in list context currently forbidden ...