File Coverage

blib/lib/Text/Template/Permute.pm
Criterion Covered Total %
statement 64 82 78.0
branch 11 22 50.0
condition n/a
subroutine 10 12 83.3
pod 0 4 0.0
total 85 120 70.8


line stmt bran cond sub pod time code
1             package Text::Template::Permute;
2              
3 2     2   356450 use 5.010001;
  2         7  
4 2     2   10 use strict;
  2         3  
  2         75  
5 2     2   19 use warnings;
  2         4  
  2         124  
6              
7 2     2   942 use Permute::Unnamed ();
  2         1132  
  2         2379  
8              
9             our $AUTHORITY = 'cpan:PERLANCAR'; # AUTHORITY
10             our $DATE = '2026-02-21'; # DATE
11             our $DIST = 'Text-Template-Permute'; # DIST
12             our $VERSION = '0.004'; # VERSION
13              
14             sub new {
15 2     2 0 420514 my $class = shift;
16              
17 2         7 bless {}, $class;
18             }
19              
20             sub _process_directive {
21 7     7   42 my $self = shift;
22 7         13 my $directive = shift;
23              
24 7 50       47 my ($command, $opts, $args) = $directive =~ /\A(\w+)\s*([^:]*)\s*:\s*(.+)\z/s
25             or die "Invalid directive syntax '$directive', please use COMMAND[OPTIONS]: ...";
26             #use DD; dd {command=>$command, opts=>$opts, args=>$args};
27 7 100       21 if ($command eq 'comment') {
    50          
    0          
28 3         16 return ();
29             } elsif ($command eq 'permute') {
30 4         14 $args =~ s!/\*.*?\*/!!g;
31 4         10 my @items = split /\|/, $args;
32 4         6 push @{ $self->{_permute_args} }, \@items;
  4         8  
33 4         7 my $i = $self->{_permute_idx}++;
34 4     16   25 return (sub { $self->{_permute_items}[$_[0]][$i] });
  16         23  
35             } elsif ($command eq 'pick') {
36 0         0 $args =~ s!/\*.*?\*/!!g;
37 0         0 my @choices = split /\|/, $args;
38 0 0       0 if ($opts eq 'once') {
39 0         0 return ($choices[rand @choices]);
40             } else {
41 0     0   0 return (sub { $choices[rand @choices] });
  0         0  
42             }
43             } else {
44 0         0 die "Unknown command '$command'";
45             }
46             }
47              
48             sub template {
49 3     3 0 14 my $self = shift;
50              
51 3 50       11 if (@_) {
52 3         7 my $template = shift;
53 3         14 $self->{template} = $template;
54 3         8 $self->{_permute_args} = [];
55 3         6 $self->{_var_array} = [];
56 3         9 $self->{_var_idx} = {};
57 3         6 $self->{_idx} = 0;
58 3         11 $self->{_template_parts} = [];
59 3         6 $self->{_permute_idx} = 0;
60 3         24 $template =~ s{( # 1. whole match
61             \{\{(.*?)\}\} | # 2. directive
62             (?:[^\{]+) | # -. normal text
63             (?:[\{\}]+) # -. normal text
64             )
65             }{
66 14 100       35 if (defined $2) {
67 7         7 push @{ $self->{_template_parts} }, $self->_process_directive($2);
  7         20  
68             } else {
69 7         12 push @{ $self->{_template_parts} }, $1;
  7         31  
70             }
71              
72             }egsx;
73             }
74 3         9 return $self->{template};
75             }
76              
77             sub _fill {
78 9     9   11 my $self = shift;
79 9         9 my $i = shift;
80 9         9 my @res;
81 9         11 for my $part (@{ $self->{_template_parts} }) {
  9         12  
82 41 100       44 if (ref $part) {
83 16         18 push @res, $part->($i);
84             } else {
85 25         34 push @res, $part;
86             }
87             }
88 9         25 join "", @res;
89             }
90              
91             sub var {
92 0     0 0 0 my $self = shift;
93              
94 0         0 my $name = shift;
95 0         0 my $val;
96 0 0       0 if (exists $self->{_var_idx}{$name}) {
97 0         0 $val = $self->{vars}{$name};
98             } else {
99 0         0 die "Variable '$name' not mentioned in template";
100             }
101 0 0       0 if (@_) {
102 0         0 $val = shift;
103 0         0 $self->{vars}{$name} = $val;
104 0         0 $self->{_var_array}[ $self->{_var_idx}{$name} - 1] = $val;
105             }
106 0         0 $val;
107             }
108              
109             sub process {
110             #no warnings 'uninitialized';
111              
112 3     3 0 11 my $self = shift;
113              
114             # generate the permutations of args
115 3         30 $self->{_permute_items} = [];
116 3 100       6 if (@{ $self->{_permute_args} }) {
  3         9  
117 2         3 $self->{_permute_items} = Permute::Unnamed::permute_unnamed(@{ $self->{_permute_args} });
  2         8  
118             } else {
119 1         1 push @{ $self->{_permute_items} }, [];
  1         4  
120             }
121             #use DD; dd $self->{_permute_items};
122              
123             # generate the permutations of text
124 3         526 my @res;
125 3         11 for my $i (0 .. $#{ $self->{_permute_items} }) {
  3         10  
126 9         17 push @res, $self->_fill($i);
127             }
128              
129 3         21 @res;
130             }
131              
132             1;
133             # ABSTRACT: Template for generating permutation of text
134              
135             __END__