| 1 | 2 | ▶ | use 5.008; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 2 | 2 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 3 | 2 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 4 | |||||||||||
| 5 | package Permute::Named; | ||||||||||
| 6 | 2 | ▶ | BEGIN { | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 7 | 2 | $Permute::Named::VERSION = '1.100980'; | |||||||||
| 8 | } | ||||||||||
| 9 | |||||||||||
| 10 | # ABSTRACT: permute multiple-valued key-value pairs | ||||||||||
| 11 | 2 | ▶ | use Storable 'dclone'; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 12 | 2 | ▶ | use Exporter qw(import); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 13 | 2 | our @EXPORT = qw(permute_named); | |||||||||
| 14 | |||||||||||
| 15 | 15 | ▶ | sub permute_named { | ||||||||
|
Subroutine
permute_named: called CC=6 SCAR=18.1
Pod
documented
| |||||||||||
| 16 | 15 | my $args; | |||||||||
| 17 | 15 | ▶ | if (@_ == 1) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 18 | 9 | $args = shift; | |||||||||
| 19 | } else { | ||||||||||
| 20 | 6 | $args = \@_; | |||||||||
| 21 | } | ||||||||||
| 22 | 15 | ▶ | if (ref $args eq 'HASH') { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 23 | 0 | $args = [%$args]; | |||||||||
| 24 | } | ||||||||||
| 25 | 15 | ▶ | if (ref $args ne 'ARRAY') { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 26 | 0 | die "permute_named expects an array ref or a hash ref\n"; | |||||||||
| 27 | } | ||||||||||
| 28 | 15 | ▶ | if (@$args % 2 == 1) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 29 | 0 | die "permute_named got an uneven-sized list of arguments\n"; | |||||||||
| 30 | } | ||||||||||
| 31 | 15 | my @permutations; | |||||||||
| 32 | my $permute; | ||||||||||
| 33 | 75 | ▶ | $permute = sub { | ||||||||
|
Subroutine
__ANON__: called CC=4 SCAR=13.9
| |||||||||||
| 34 | 75 | my ($args, $done) = @_; | |||||||||
| 35 | 75 | my $done_clone = dclone $done; | |||||||||
| 36 | 75 | ▶ | if (@$args == 0) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 37 | 45 | push @permutations => $done_clone; | |||||||||
| 38 | 45 | return; | |||||||||
| 39 | } | ||||||||||
| 40 | 30 | my $args_clone = dclone $args; | |||||||||
| 41 | 30 | my ($key, $value) = splice @$args_clone, 0, 2; | |||||||||
| 42 | 30 | ▶ | $value = [$value] unless ref $value eq 'ARRAY'; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 43 | 30 | for my $element (@$value) { | |||||||||
| 44 | 60 | $done_clone->{$key} = $element; | |||||||||
| 45 | 60 | $permute->($args_clone, $done_clone); | |||||||||
| 46 | } | ||||||||||
| 47 | 15 | }; | |||||||||
| 48 | 15 | $permute->($args, {}); | |||||||||
| 49 | 15 | ▶ | wantarray ? @permutations : \@permutations; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 50 | } | ||||||||||
| 51 | 2 | 1; | |||||||||
| 52 | |||||||||||
| 53 | |||||||||||
| 54 | __END__ | ||||||||||