How to get the length of an array in Perl?

Question

How to get the length of an array in Perl?

In Perl, arrays are fundamental data structures used to store ordered lists of scalars. One very common operation when working with arrays is to determine how many elements they contain — in other words, to get the length or size of the array.

Getting the length of an array

To get the length of an array in Perl, you use the scalar function or scalar context on the array. This converts the array into a scalar value representing the number of elements it contains.

Here’s how it works:

  • scalar(@array) returns the number of elements in @array.
  • If you just use @array in scalar context (e.g., assigning to a scalar variable), it automatically gives you the length.

Example code


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

# Define an array of fruits
my @fruits = ('apple', 'banana', 'orange', 'grape');

# Get length using scalar()
my $count1 = scalar(@fruits);

# Get length by scalar context directly
my $count2 = @fruits;

# Print results
print "Number of fruits (scalar): $count1\n";
print "Number of fruits (context): $count2\n";

# Show what happens if you don't use scalar context
print "Array in list context prints all elements: @fruits\n";

Output:


Number of fruits (scalar): 4
Number of fruits (context): 4
Array in list context prints all elements: apple banana orange grape

Explanation: scalar context & sigils

  • Perl's arrays use the @ sigil, e.g. @fruits.
  • When you access the whole array @fruits, you're in list context by default, so it returns all elements.
  • To get the length, you need to put it in scalar context — which you can do with the scalar() function or by assigning to a scalar variable.
  • The scalar variable has $ sigil, e.g. $count.

Common pitfalls

  • Trying to get length with just $fruits will not work — it refers to the first element if you use $fruits[0] or is undefined otherwise.
  • Don't use the scalar of an individual element to get length; you need the entire array.
  • Beware of empty arrays — length is 0.

Additional notes

Perl's "There's more than one way to do it" (TMTOWTDI) philosophy means you can get the length in these ways:

  • scalar(@array)
  • Simply assigning array to a scalar: my $len = @array;
  • Using $#array + 1, which is the last index plus one (because array indices start at 0).

For example:


my $len = $#fruits + 1;  # also valid, but less idiomatic

This technique is slightly more old-school but works identically for non-empty arrays.

In summary, the preferred modern way to get the length of an array is to use scalar(@array) or scalar context on @array.

Verified Code

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

Tip: edit code and use “Run (Browser)”. Server runs always execute the published, verified snippet.
STDOUT
Number of fruits (scalar): 4
Number of fruits (context): 4
Array in list context prints all elements: apple banana orange grape
STDERR
(empty)

Was this helpful?

Related Questions