Sign in to PrePAN

List::Breakdown Build list sublists matching conditions



my @words = qw(foo bar baz quux wibble florb);
my $cats  = {
    all    => sub { 1 },
    has_b  => sub { m/ b /msx },
    has_w  => sub { m/ w /msx },
    length => {
        3    => sub { length == 3 },
        4    => sub { length == 4 },
        long => sub { length > 4 },
    has_ba => qr/ba/msx,
my %filtered = breakdown $cats, @words;


EDIT: I have now added this module to CPAN:

This module "breaks down" a list--filtering elements from a list into a specified bucket layout. It may be useful in situations where you have a big list of things to generate reports on or to otherwise filter into several sublists.

It differs from the excellent List::Categorize in the use of subroutine references for each category and in not requiring only one final category for any given item; an item can end up in the result set for more than one filter.

You could maybe think of this as a multi-grep that returns named results.

The synopsis code produces this structure in %filtered:

    all => ['foo', 'bar', 'baz', 'quux', 'wibble', 'florb'],
    has_b => ['bar', 'baz', 'wibble', 'florb'],
    has_w => ['wibble'],
    length => {
        3 => ['foo', 'bar', 'baz'],
        4 => ['quux'],
        long => ['wibble', 'florb'],
    has_ba => ['bar', 'baz'],

The module is written and working, with a basic test suite, and can be downloaded and browsed here:


Please sign up to post a review.