Skip to content

Perl Subroutines Guide — Functions, Parameters, and Return Values

DodaTech Updated 2026-06-28 2 min read

In this tutorial, you will learn about Perl Subroutines Guide. We cover key concepts, practical examples, and best practices to help you master this topic.

Perl subroutines are defined with the sub keyword, receive arguments in @_, return values with return, and support prototypes for compile-time argument checking -- with shift often used to extract individual parameters.

Basic Subroutine

use strict;
use warnings;

sub greet {
    my $name = shift;
    print "Hello, $name!\n";
}

greet("Alice");  # Hello, Alice!

Multiple Parameters

sub add {
    my ($a, $b) = @_;
    return $a + $b;
}

print add(3, 4);  # 7

Default Parameters

sub connect {
    my ($host, $port, $timeout) = @_;
    $host    ||= "localhost";
    $port    ||= 3306;
    $timeout //= 30;  # //= only for undef
    
    print "Connecting to $host:$port (timeout: ${timeout}s)\n";
}

connect();           # localhost:3306
connect("db.example.com", 5432);  # db.example.com:5432

Passing References

sub process_array {
    my $array_ref = shift;
    my $hash_ref  = shift;
    
    for my $elem (@$array_ref) {
        print "$elem: $hash_ref->{$elem}\n";
    }
}

my @items = qw(a b c);
my %scores = (a => 95, b => 87, c => 92);
process_array(\@items, \%scores);

Private Variables with state

use feature 'state';

sub counter {
    state $count = 0;
    $count++;
    return $count;
}

print counter();  # 1
print counter();  # 2
print counter();  # 3

Common Mistakes

1. Forgetting shift extracts one arg

Each shift removes one element from @_. Multiple shifts extract multiple arguments in order.

2. Not using my for parameters

Without my, variables are global. Always use my ($a, $b) = @_ for lexical scope.

3. Confusing return with print

return sends a value back to the caller. print sends output to STDOUT. They are different.

Practice Questions

1. How do you access subroutine arguments? Through @_. Use shift or assignment: my ($a, $b, $c) = @_.

2. What does shift do inside a subroutine? shift extracts and returns the first element of @_, removing it from the array.

3. How do you pass an array to a subroutine? Pass a reference: Process(\@array). Subroutines flatten arrays in @_.

FAQ

{{< faq question="Can subroutines be nested?" >}} Yes. Perl supports nested subroutines. However, they are compiled at compile time, not closure-like unless explicitly using anonymous subs. {{< /faq >}}

{{< faq question="What is the difference between shift and pop?" >}} shift removes and returns the first element. pop removes and returns the last element. Both modify the array. {{< /faq >}}

{{< faq question="How do I return multiple values?" >}} Return a list: return ($a, $b, $c). The caller assigns: my ($x, $y, $z) = func(). {{< /faq >}}

What's Next

Now learn about file I/O in Perl.

Topic Description Link
File I/O Reading and writing files {{< ref "08-file-io" >}}
Control Flow if, unless, given/when {{< ref "09-control-flow" >}}

Built by the developers of DodaTech

Doda Browser, DodaZIP & Durga Antivirus Pro