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";

In list context, both values are returned. In scalar context, the list expression in this subroutine evaluates to its last value. When a routine should behave differently by context, wantarray can detect list, scalar, or void context; use that technique sparingly because it makes the interface less predictable.

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

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.