To return a value from a subroutine, simply use the return function.
sub print_headers { my ($title, $author) = @_; return "$title\nby\n$author\n\n"; } sub sum { my $total; foreach (@_) { $total = $total + $_; } return $total; } |
You can also return lists from your subroutine:
# subroutine to return the first three arguments passed to it sub firstthree { return @_[0..2]; } my @three_items = firstthree("x", "y", "z", "a", "b"); # sets @three_items to ("x", "y", "z"); |