File Coverage

blib/lib/String/Substitute.pm
Criterion Covered Total %
statement 29 29 100.0
branch 2 2 100.0
condition n/a
subroutine 6 6 100.0
pod 1 1 100.0
total 38 38 100.0


line stmt bran cond sub pod time code
1             package String::Substitute;
2              
3             our $VERSION = '0.006';
4              
5             use Exporter::Easy (
6 2         14 OK => [qw/get_all_substitutes/],
7 2     2   2283 );
  2         2359  
8 2     2   1017 use Set::CrossProduct;
  2         3332  
  2         49  
9 2     2   870 use List::Gather;
  2         26354  
  2         14  
10 2     2   1740 use Params::Validate qw(:all);
  2         15930  
  2         352  
11 2     2   844 use strictures 2;
  2         2827  
  2         60  
12              
13             sub get_all_substitutes {
14 2     2 1 1126 my %params = validate(
15             @_, {
16             string => { type => SCALAR },
17             substitutions => { type => HASHREF },
18             }
19             );
20              
21 2         8 my %subs = %{$params{substitutions}};
  2         6  
22              
23 2         6 my @character_sets = gather {
24 2         8 my @chars = split //, $params{string};
25 2         4 for my $char (@chars) {
26 10 100       14 if (exists $subs{$char}) {
27 6         14 take [ split(//, $subs{$char}) ];
28             }
29             else {
30 4         6 take [ $char ];
31             }
32             }
33 2         3 };
34              
35 2         16 my $exploded_results = Set::CrossProduct->new([@character_sets])->combinations;
36 2         767 my @imploded_results = map { join '', @$_ } @$exploded_results;
  20         26  
37 2         20 return @imploded_results;
38             }
39              
40              
41             1;
42              
43             # ABSTRACT: generate strings using different combinations of subsitute characters
44              
45             __END__