Subroutines

Define and call

A subroutine is a named block of code introduced with the sub keyword. Call it by name with parentheses around the arguments; the ampersand form from very old code is optional and best forgotten.

sub greet { my ($name) = @_; return "hello, $name"; } print greet("Ada"), "\n"; # hello, Ada

perl demo.pl

hello, Ada

Arguments arrive in the array @_, which the next lesson takes apart. Here the list assignment copies the first argument into $name and ignores the rest.

Return values

return leaves the subroutine immediately with the given value. Without a return, the value of the last expression is used, which is convenient for short functions and confusing for long ones.

sub max { my ($a, $b) = @_; return $a if $a >= $b; $b; } print max(3, 9), "\n"; # 9

perl demo.pl

9

Calling return with no argument gives an empty list in list context and undef in scalar context. Say return; when the point is an early exit, and return undef; never, because it surprises list callers.

Where to put the sub

A call to a subroutine with parentheses works anywhere, because the parentheses tell the parser it is a call. A call without parentheses must come after the definition, or you need an empty prototype or a forward declaration.

sub double { return $_[0] * 2 } print double(21), "\n"; # 42 print double 21, "\n"; # 42, no parentheses needed here print "\n"; # the call above had no newline: "\n" is an argument

perl demo.pl

42
42

The safest habit is to put every sub at the bottom of the file and call with parentheses. The program then reads top to bottom as a story, with the details below.

Private subroutines

A leading underscore marks a helper that is not part of the public interface. Nothing enforces the rule, but every Perl programmer reads the underscore the same way.

sub _trim { my ($s) = @_; $s =~ s/^\s+|\s+$//g; return $s; }

In a later lesson the same idea becomes a real module, where the leading underscore combines with the export list so that other files cannot see the helper at all.

Questions

A new set of questions in a new order every time. Choose one answer for each, then check.

  1. How do you call a subroutine named greet with one argument?

    Answer

    greet("Ada"), with the parentheses telling the parser it is a call

    The parentheses make the call work anywhere; the old ampersand form is optional and best forgotten.

  2. Where do the arguments of a subroutine arrive?

    Answer

    In the array @_

    @_ holds the argument list, and the usual my ($a, $b) = @_; line names them.

  3. What does a subroutine return when it has no return?

    Answer

    The value of its last expression

    The last expression is the return value, which is convenient for short subs and confusing for long ones.

  4. What does return; give the caller, compared with return undef;?

    Answer

    return; gives an empty list in list context and undef in scalar context; return undef; surprises list callers with one undef element

    The bare form is the honest early exit, while returning undef in a list puts a single undefined value where the caller expected none.

  5. Why does a call with parentheses work even when the sub is defined further down the file?

    Answer

    The parentheses tell the parser it is a call, so no forward declaration is needed

    A bareword call that the parser cannot resolve is the case that needs the definition above it, a prototype or a forward declaration.

  6. What does a leading underscore in _trim tell a reader?

    Answer

    That it is a private helper rather than part of the public interface

    Nothing enforces the rule, but every Perl programmer reads the underscore as internal.

  7. What does this program print? sub add { my ($a, $b) = @_; return $a + $b; } print add(2, 3), "\n";

    Answer

    5

    The list assignment names the two arguments and the return adds them.

  8. What does this program print? sub max { my ($a, $b) = @_; return $a if $a >= $b; $b; } print max(9, 3), "\n";

    Answer

    9

    The first argument wins the test, so the early return hands it back.

  9. What does this program print? sub double { return $_[0] * 2 } print double(21), "\n";

    Answer

    42

    $_[0] is the first argument, and the one call prints one line.

  10. What does this program print? sub add { my ($a, $b) = @_; $a + $b; } print add(1, 2), "\n";

    Answer

    3

    The last expression is the return value, so the missing return changes nothing here.

Article author: Arthur Isaev