cpan intermediate

How to use List::Util reduce for custom aggregation in Perl?

Question

How to use List::Util reduce for custom aggregation in Perl?

The List::Util module's reduce function is a versatile tool for custom aggregation in Perl. It "folds" or "reduces" a list by applying a block repeatedly, combining elements according to your logic.

What is reduce?

reduce takes a block and a list. In the block, the special variables $a and $b represent the accumulated value so far and the next element, respectively. The block returns the new accumulated value. After processing the entire list, reduce returns a single scalar result.

Key Perl Concepts

  • Sigils: $a and $b are package variables specially localized by reduce, not classical lexical variables.
  • Context: reduce returns a scalar—the final accumulation.
  • TMTOWTDI: Perl lets you define your logic flexibly inside the block, so you can aggregate or combine values however you want.

Correct Basic Example: Multiply All Numbers


use strict;
use warnings;
use List::Util qw(reduce);

my @numbers = (2, 3, 5, 7);

my $product = reduce { $a * $b } @numbers;

print "Product of (@numbers) is $product\n";

This prints:

Product of (2 3 5 7) is 210

Advanced Example: Concatenate Odd Numbers as a String

The following shows how to accumulate only odd numbers as a concatenated string:


use strict;
use warnings;
use List::Util qw(reduce);

my @vals = (1, 2, 3, 4, 5);

my $concat_odds = reduce {
    $a . ($b % 2 ? $b : '')
} '', @vals;

print "Concatenated odd numbers: $concat_odds\n";

Expected output:

Concatenated odd numbers: 135

Common Gotchas

  • No explicit initial): Unlike some other languages, this reduce does not accept an explicit initial accumulator argument. $a starts with the first list element.
  • Empty list: Returns undef without running the block.
  • Single element: Returns that element directly.
  • Global vars: $a and $b are package globals localized only inside the block. Avoid confusion by not declaring my variables named $a or $b.
  • Use warnings: Helps catch mistakes like missing braces or typos.

Summary

List::Util::reduce is your go-to for flexible aggregation and folding logic. With Perl’s TMTOWTDI spirit, you can craft any combining operation inside the { } block using $a and $b. Just be mindful of the initial conditions and the special variable scope.

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
Product of (2 3 5 7) is 210
STDERR
(empty)

Was this helpful?

Related Questions