ZetCode

Perl subroutines

last modified October 8, 2026

A Perl subroutine is a named block of code that can be called from elsewhere in a program. Subroutines help organize reusable operations and give those operations a clear interface.

Defining and calling a subroutine

Declare a subroutine with sub, a name, and a block. A named subroutine can be called with or without parentheses when the syntax is unambiguous.

greeting.pl
#!/usr/bin/perl

use strict;
use warnings;
use v5.30.0;

sub greet {
    say 'Hello!';
}

greet();

In this example, the subroutine greet prints a greeting. The caller invokes the subroutine with parentheses, but they are optional in this case because the syntax is unambiguous.

$ perl greeting.pl
Hello!

Arguments are passed through @_

When a subroutine is called, its arguments are placed in the special array @_. Assign them to lexical variables near the beginning of the subroutine to make their meaning clear.

parameters.pl
#!/usr/bin/perl

use strict;
use warnings;
use v5.30.0;

sub greet {
    my ($name, $greeting) = @_;
    $greeting //= 'Hello';
    say "$greeting, $name!";
}

greet('Ada', 'Welcome');
greet('Grace');

In this example, list assignment copies the first two argument values into lexical variables. //= supplies a default only when $greeting is undefined; an empty string remains an explicitly supplied value.

Subroutine arguments are not declared in a parameter list in this traditional syntax. Changes to elements of @_ can affect aliased arguments, so copying values into my variables is a useful default.

$ perl parameters.pl
Welcome, Ada!
Hello, Grace!

Returning values

A subroutine returns the value of its last evaluated expression unless it executes an explicit return. Use return to make early exits and the intended result obvious.

area.pl
#!/usr/bin/perl

use strict;
use warnings;
use v5.30.0;

sub rectangle_area {
    my ($width, $height) = @_;
    return $width * $height;
}

my $area = rectangle_area(5, 3);
say $area;

I n this example, the subroutine returns the area of a rectangle. The caller receives the value and prints it.

$ perl area.pl
15

Scalar and list return values

The value a subroutine returns is used in the context imposed by its caller. A list-producing subroutine can return multiple values in list context and a count or another scalar result in scalar context.

context_return.pl
#!/usr/bin/perl

use strict;
use warnings;
use v5.30.0;

sub coordinates {
    return (10, 20);
}

my ($x, $y) = coordinates();
my $last = coordinates();

say "x=$x y=$y";
say "scalar result=$last";

The same call can behave differently depending on where its result goes. Assigning to a list, as in my ($x, $y) = coordinates();, is list context, so both values are returned. Assigning to a single scalar variable is scalar context. Here the comma-separated list does not become a count or an array; Perl simply evaluates to its last element, so $last is 20.

$ perl context_return.pl
x=10 y=20
scalar result=20

Detecting the calling context with wantarray

A subroutine can find out which context it was called in with the built-in wantarray function. It returns a true value in list context, a defined false value (0) in scalar context, and undef in void context, that is, when the caller ignores the result. The subroutine can then choose what to return instead of relying on Perl's default behavior.

wantarray.pl
#!/usr/bin/perl

use strict;
use warnings;
use v5.30.0;

sub get_words {
    my @words = qw(falcon owl eagle);

    if (!defined wantarray) {
        say 'void context: result ignored';
        return;
    }

    return wantarray ? @words : join(', ', @words);
}

my @list = get_words();
my $text = get_words();
get_words();

say 'list:   ', join(' | ', @list);
say 'scalar: ', $text;

The get_words subroutine returns the words as a list when the caller wants a list, and as a single comma-separated string when the caller wants a scalar. The test !defined wantarray must come first, because wantarray is false in both scalar and void context; only defined tells them apart. In the last call the result is thrown away, so the subroutine just reports that and returns. Use this technique sparingly, because an interface whose result changes with context is harder to predict.

$ perl wantarray.pl
void context: result ignored
list:   falcon | owl | eagle
scalar: falcon, owl, eagle

Lexical scope inside subroutines

Variables declared with my are visible only within their lexical scope. A subroutine can use its own lexical variables without exposing or overwriting values in its caller.

lexical_scope.pl
use strict;
use warnings;
use v5.30.0;

my $prefix = 'ID';

sub make_id {
    my ($number) = @_;
    my $formatted = sprintf('%s-%04d', $prefix, $number);
    return $formatted;
}

say make_id(27);

The subroutine can also read the lexical $prefix because it was declared in an enclosing file scope. The local $number and $formatted names exist only inside make_id. Pass needed values as arguments when that makes dependencies easier to see.

$ perl lexical_scope.pl
ID-0027

See Perl variables for lexical scope and Perl basics for scalar and list context.

Source

Perl subroutine documentation

Author

My name is Jan Bodnar, and I am a passionate programmer with extensive programming experience. I have been writing programming articles since 2007. To date, I have authored over 1,400 articles and 8 e-books. I possess more than ten years of experience in teaching programming.

List all Perl tutorials.