subroutines beginner

How to return values from a subroutine in Perl?

Question

How to return values from a subroutine in Perl?

In Perl, subroutines (declared with sub) can return values using the return keyword or implicitly by being the last evaluated expression. Perl supports returning a single scalar or a list of values, and how you capture those values depends on the context in which the subroutine is called (scalar vs list context). Understanding Perl’s contexts and the use of sigils is key here.

Returning Values from a Subroutine

To return values explicitly, you use the return keyword followed by the value(s) you want to return. If you don’t use return, Perl will return the value of the last expression evaluated inside the subroutine by default.

  • Return a single scalar value: Return one scalar (string, number, reference, etc.)
  • Return a list of values: Return multiple values as a list (array) which can be captured by the caller

Examples and Concepts

#!/usr/bin/perl
use strict;
use warnings;

# Subroutine returning a scalar value
sub greet {
    my $name = shift;
    return "Hello, $name!";
}

# Subroutine returning a list of values
sub get_numbers {
    # returns a list of numbers
    return (1, 2, 3, 4, 5);
}

# Subroutine with implicit return (last evaluated value)
sub add {
    my ($x, $y) = @_;
    $x + $y;   # no return keyword, adds two numbers and returns result
}

# Calling and printing returned values
my $greeting = greet("Alice");
print "$greeting\n";   # prints: Hello, Alice!

my @nums = get_numbers();
print "Numbers: @nums\n";   # prints: Numbers: 1 2 3 4 5

my $sum = add(10, 20);
print "Sum: $sum\n";  # prints: Sum: 30

Key Perl Concepts Illustrated

  • sigils: $ for scalars, @ for arrays (lists), % for hashes. Your return type affects how you capture result.
  • Context: Calling a subroutine in scalar context (my $var = sub()) expects a scalar; in list context (my @arr = sub()) expects a list.
  • return vs implicit return: Using return is explicit and improves readability, but you can also rely on Perl to return the last evaluated expression.
  • TMTOWTDI (“There’s more than one way to do it”): Perl allows multiple idiomatic ways to return values, giving flexibility in writing subroutines.

Common Pitfalls

  • Returning a list but capturing it in scalar context will only give the last element of the list. Example:
    my $val = get_numbers();  # returns 5, the last number
  • Returning array references if you want to return a list as a single scalar (common when returning complex data):
sub get_numbers_ref {
    return [1, 2, 3, 4, 5]; # returns array reference
}
my $array_ref = get_numbers_ref();
print "Second number is: " . $array_ref->[1] . "\n";  # prints: 2

Using references avoids unintended flattening of lists, useful in complex data structures.

Summary

Returning values from Perl subroutines is flexible due to Perl’s scalar/list context and sigils. You can return scalars or lists explicitly with return or rely on implicit returns. Capturing these values properly requires matching the calling context (scalar or list). Embracing Perl’s “There's more than one way to do it” philosophy allows you to write clean, idiomatic subroutines.

Verified Code

Executed in a sandbox to capture real output. • v5.34.1 • 6ms

Tip: edit code and use “Run (Browser)”. Server runs always execute the published, verified snippet.
STDOUT
Hello, Alice!
Numbers: 1 2 3 4 5
Sum: 30
STDERR
(empty)

Was this helpful?

Related Questions