ZetCode

Perl map

last modified October 8, 2026

The Perl map operator evaluates a block or expression for each item in a list and returns the resulting values. It is useful for transforming a list into another list.

map is a list operator: its input is evaluated in list context. In the block form, the current item is available in $_; the block may produce zero, one, or several output values for each input item.

A basic map example

This example doubles every number and collects the results in a new array.

basic.pl
#!/usr/bin/perl

use strict;
use warnings;
use v5.30.0;

my @numbers = (2, 4, 6, 8);
my @doubled = map { $_ * 2 } @numbers;

say "@doubled";
$ perl basic.pl
4 8 12 16

The block runs once for each input element. The value of its final expression is added to the output list.

Using the current value

Inside a map block, $_ refers to the item currently being processed. You can use the block to build formatted strings or apply a calculation.

labels.pl
#!/usr/bin/perl

use strict;
use warnings;
use v5.30.0;

my @names = ('Ada', 'Grace', 'Linus');
my @labels = map { "User: $_" } @names;

say for @labels;
$ perl labels.pl
User: Ada
User: Grace
User: Linus

In this example, double-quoted strings interpolate each name. The original array is not changed by this transformation.

Filtering while mapping

A block can return an empty list for an input item, which omits that item from the output. It can also return several values, which expands the output.

filter_map.pl
#!/usr/bin/perl

use strict;
use warnings;
use v5.30.0;

my @values = (-3, -1, 0, 2, 5);
my @positive_squares = map { $_ > 0 ? $_ * $_ : () } @values;

say "@positive_squares";
$ perl filter_map.pl
4 25

For values that are not positive, the conditional expression returns an empty list. For clearer code, combine grep and map when filtering and transforming are distinct steps:

my @positive_squares = map { $_ * $_ } grep { $_ > 0 } @values;

Map key-value pairs

map can construct the flat list of key-value pairs used to initialize a hash. Each block execution should return an even number of values when the result is assigned to a hash.

hash_map.pl
#!/usr/bin/perl

use strict;
use warnings;
use v5.30.0;

my @words = ('pear', 'plum', 'kiwi');
my %lengths = map { $_ => length($_) } @words;

say "$_. $lengths{$_}" for sort keys %lengths;
$ perl hash_map.pl
kiwi. 4
pear. 4
plum. 4

Hash keys are not stored in a defined order, so the example sorts keys %lengths before displaying them. See Perl sort for ordering lists.

Map in scalar context

In scalar context, map returns the number of elements produced by its block, not the transformed list itself.

my $count = map { $_ * 2 } (1, 2, 3);
say $count; # 3

Use an array assignment or another list-context expression when you need the mapped values. Avoid using map only for side effects; a loop communicates that intent more clearly.

Modifying input items

The $_ alias in a map block refers to the current input element, so assigning to it can change the source array. Prefer returning a new value unless modifying the original list is intentional.

#!/usr/bin/perl

use strict;
use warnings;
use v5.30.0;

my @values = (1, 2, 3);
my @squares = map { $_ = $_ * $_ } @values;

say "@values"; # 1 4 9

For ordinary transformations, use map { expression using $_ } without assignment, as in the earlier examples.

See also Perl grep, which selects items from a list without transforming them.

Source

Perl map 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.