The Perl Advent Calendar needs more articles for 2022. Submit your idea today!

NAME

Permute::Named::Iter - Permute multiple-valued key-value pairs

VERSION

This document describes version 0.04 of Permute::Named::Iter (from Perl distribution Permute-Named-Iter), released on 2016-09-26.

SYNOPSIS

 use Permute::Named::Iter qw(permute_named_iter);

 my $iter = permute_named_iter(bool => [ 0, 1 ], x => [qw(foo bar baz)]);
 while (my $h = $iter->()) {
     some_setup() if $h->{bool};
     other_setup($h->{x});
     # ... now maybe do some tests ...
 }

DESCRIPTION

This module is like Permute::Named, except that it offers an iterator interface. Some other differences: 1) it only accepts an even-sized list and not arrayref or hashref; 2) it does not use deep cloning, so if one of the values is a reference and you modify the content of the reference, the next iteration will see the modification; 3) the function permute_named_iter is not exported by default, you have to import it explicitly.

FUNCTIONS

permute_named_iter(@list) => CODE

Takes a list of key-specification pairs where the specifications can be single values or references to arrays of possible values. It then returns an iterator (coderef) which you can call repeatedly to permute all key-specification combinations.

The function expects the pairs as an even-sized list. Each specification can be a scalar or a reference to an array of possible values. The returned iterator can be called and will return a hashref, or undef if all the permutation has been exhausted.

Example 1:

 my $iter = permute_named_iter(bool => [ 0, 1 ], x => [qw(foo bar baz)]);
 my @p; while (my $h = $iter->()) { push @p, $h }

@p will contain:

 ( { bool => 0, x => 'foo' },
   { bool => 0, x => 'bar' },
   { bool => 0, x => 'baz' },
   { bool => 1, x => 'foo' },
   { bool => 1, x => 'bar' },
   { bool => 1, x => 'baz' }, )

Example 2:

 my $iter = permute_named_iter(bool => 1, x => 'foo');
 my @p; while (my $h = $iter->()) { push @p, $h }

@p will just contain the one permutation:

 ({bool => 1, x => 'foo'})

HOMEPAGE

Please visit the project's homepage at https://metacpan.org/release/Permute-Named-Iter.

SOURCE

Source repository is at https://github.com/perlancar/perl-Permute-Named-Iter.

BUGS

Please report any bugs or feature requests on the bugtracker website https://rt.cpan.org/Public/Dist/Display.html?Name=Permute-Named-Iter

When submitting a bug or request, please include a test-file or a patch to an existing test-file that illustrates the bug or desired feature.

SEE ALSO

Permute::Named, PERLANCAR::Permute::Named and CLI permute-named.

Set::CrossProduct, Set::Product, et al (see the POD of Set::Product for more similar modules) and CLI cross.

AUTHOR

perlancar <perlancar@cpan.org>

COPYRIGHT AND LICENSE

This software is copyright (c) 2016 by perlancar@cpan.org.

This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.