| line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
|
1
|
|
|
|
|
|
|
#! perl |
|
2
|
|
|
|
|
|
|
|
|
3
|
|
|
|
|
|
|
package String::Interpolate::Named; |
|
4
|
|
|
|
|
|
|
|
|
5
|
8
|
|
|
8
|
|
2945322
|
use warnings; |
|
|
8
|
|
|
|
|
18
|
|
|
|
8
|
|
|
|
|
782
|
|
|
6
|
8
|
|
|
8
|
|
84
|
use strict; |
|
|
8
|
|
|
|
|
17
|
|
|
|
8
|
|
|
|
|
348
|
|
|
7
|
8
|
|
|
8
|
|
4675
|
use utf8; |
|
|
8
|
|
|
|
|
5822
|
|
|
|
8
|
|
|
|
|
52
|
|
|
8
|
8
|
|
|
8
|
|
392
|
use Carp qw( carp croak ); |
|
|
8
|
|
|
|
|
28
|
|
|
|
8
|
|
|
|
|
935
|
|
|
9
|
|
|
|
|
|
|
|
|
10
|
|
|
|
|
|
|
# Disable 'Unicode character 0xfddX is illegal' warnings. |
|
11
|
8
|
|
|
8
|
|
61
|
no if $] < 5.014, q|warnings|, qw(utf8); |
|
|
8
|
|
|
|
|
19
|
|
|
|
8
|
|
|
|
|
394
|
|
|
12
|
|
|
|
|
|
|
|
|
13
|
8
|
|
|
8
|
|
553
|
use parent 'Exporter'; |
|
|
8
|
|
|
|
|
359
|
|
|
|
8
|
|
|
|
|
74
|
|
|
14
|
|
|
|
|
|
|
our @EXPORT = qw( interpolate ); |
|
15
|
|
|
|
|
|
|
|
|
16
|
|
|
|
|
|
|
=head1 NAME |
|
17
|
|
|
|
|
|
|
|
|
18
|
|
|
|
|
|
|
String::Interpolate::Named - Interpolated named arguments in string |
|
19
|
|
|
|
|
|
|
|
|
20
|
|
|
|
|
|
|
=cut |
|
21
|
|
|
|
|
|
|
|
|
22
|
|
|
|
|
|
|
our $VERSION = '1.06'; |
|
23
|
|
|
|
|
|
|
|
|
24
|
|
|
|
|
|
|
=head1 SYNOPSIS |
|
25
|
|
|
|
|
|
|
|
|
26
|
|
|
|
|
|
|
use String::Interpolate::Named; |
|
27
|
|
|
|
|
|
|
|
|
28
|
|
|
|
|
|
|
my $ctl = { args => { fn => "Johan", ln => "Bach" } }; |
|
29
|
|
|
|
|
|
|
say interpolate( $ctl, "The famous %{fn} %{ln}." ); |
|
30
|
|
|
|
|
|
|
|
|
31
|
|
|
|
|
|
|
# If you like object orientation. |
|
32
|
|
|
|
|
|
|
my $int = String::Interpolate::Named->new( { args => { ... } } ); |
|
33
|
|
|
|
|
|
|
say $int->interpolate("The famous %{fn} %{ln}."); |
|
34
|
|
|
|
|
|
|
|
|
35
|
|
|
|
|
|
|
=head1 DESCRIPTION |
|
36
|
|
|
|
|
|
|
|
|
37
|
|
|
|
|
|
|
String::Interpolate::Named provides a function to interpolate named |
|
38
|
|
|
|
|
|
|
I by I in a template string. The target texts |
|
39
|
|
|
|
|
|
|
are provided to the function via a hash, where the keys correspond to |
|
40
|
|
|
|
|
|
|
the named argument to be replaced, or a subroutine that performs the |
|
41
|
|
|
|
|
|
|
lookup. |
|
42
|
|
|
|
|
|
|
|
|
43
|
|
|
|
|
|
|
=head2 Named Arguments |
|
44
|
|
|
|
|
|
|
|
|
45
|
|
|
|
|
|
|
The arguments to be replaced are marked in the template by enclosing |
|
46
|
|
|
|
|
|
|
them between C<%{> and C<}>. For example, the string C<"The famous |
|
47
|
|
|
|
|
|
|
%{fn} %{ln}."> contains two named arguments, C and C. |
|
48
|
|
|
|
|
|
|
|
|
49
|
|
|
|
|
|
|
Note that the activator may be changed from C<%> into something else, |
|
50
|
|
|
|
|
|
|
see below. Throughout this document we use the default value. |
|
51
|
|
|
|
|
|
|
|
|
52
|
|
|
|
|
|
|
=head2 Basic Interpolation |
|
53
|
|
|
|
|
|
|
|
|
54
|
|
|
|
|
|
|
When interpolated, the keys C and C are looked up in the hash, |
|
55
|
|
|
|
|
|
|
and the corresponding values are substituted. If no value was found |
|
56
|
|
|
|
|
|
|
for a named argument, nothing is substituted and the C<%{...}> is |
|
57
|
|
|
|
|
|
|
removed. |
|
58
|
|
|
|
|
|
|
|
|
59
|
|
|
|
|
|
|
You can precede C<%>, C<{>, C<}> (and C<|>, see below) with a |
|
60
|
|
|
|
|
|
|
backslash C<\> to hide their special meanings. For example, C<\}> will |
|
61
|
|
|
|
|
|
|
I be considered closing an argument but yield a plain C<}> in the |
|
62
|
|
|
|
|
|
|
text. |
|
63
|
|
|
|
|
|
|
|
|
64
|
|
|
|
|
|
|
=head2 Conditional Interpolation |
|
65
|
|
|
|
|
|
|
|
|
66
|
|
|
|
|
|
|
It is possible to select replacement values depending on whether |
|
67
|
|
|
|
|
|
|
the named argument has a value or not: |
|
68
|
|
|
|
|
|
|
|
|
69
|
|
|
|
|
|
|
"This book has %{title|title %{title}}" |
|
70
|
|
|
|
|
|
|
"This book has %{title|title %{title}|no title}" |
|
71
|
|
|
|
|
|
|
|
|
72
|
|
|
|
|
|
|
These are considered C<%{if|then}> and C<%{if|then|else}> cases. |
|
73
|
|
|
|
|
|
|
|
|
74
|
|
|
|
|
|
|
Assuming argument C has the value C<"My Book">, in the first |
|
75
|
|
|
|
|
|
|
example the text C<"title My Book">, the 'then' text, will be |
|
76
|
|
|
|
|
|
|
substituted, resulting in |
|
77
|
|
|
|
|
|
|
|
|
78
|
|
|
|
|
|
|
"This book has title My Title" |
|
79
|
|
|
|
|
|
|
|
|
80
|
|
|
|
|
|
|
If C does not have a value, the empty string is substituted. In |
|
81
|
|
|
|
|
|
|
the second example, the string C<"no title">, the 'else' text, will be |
|
82
|
|
|
|
|
|
|
substituted. |
|
83
|
|
|
|
|
|
|
|
|
84
|
|
|
|
|
|
|
As can be seen, the replacement texts may contain interpolations as |
|
85
|
|
|
|
|
|
|
well. For convenience, you can use C<%{}> to refer to the value of the |
|
86
|
|
|
|
|
|
|
named argument currently being examinated. The last example above can |
|
87
|
|
|
|
|
|
|
be written more shortly and elegantly as: |
|
88
|
|
|
|
|
|
|
|
|
89
|
|
|
|
|
|
|
"This book has %{title|title %{}|no title}" |
|
90
|
|
|
|
|
|
|
|
|
91
|
|
|
|
|
|
|
=head2 Testing Values |
|
92
|
|
|
|
|
|
|
|
|
93
|
|
|
|
|
|
|
Instead of testing for named variables to have a value, you can also |
|
94
|
|
|
|
|
|
|
test for specific values: |
|
95
|
|
|
|
|
|
|
|
|
96
|
|
|
|
|
|
|
"This takes %{days=1|%{} day|%{} days}" |
|
97
|
|
|
|
|
|
|
|
|
98
|
|
|
|
|
|
|
=head2 List Values |
|
99
|
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
The replacement values hash may be scalar (in general: strings and |
|
101
|
|
|
|
|
|
|
numbers) or lists of scalars. If a value is a list of scalars, it is |
|
102
|
|
|
|
|
|
|
possible to select a particular value from the list by appending an |
|
103
|
|
|
|
|
|
|
index (period and a number) to the named argument. |
|
104
|
|
|
|
|
|
|
|
|
105
|
|
|
|
|
|
|
Assume C has value C<[ "Jones", "Smith" ]>, then: |
|
106
|
|
|
|
|
|
|
|
|
107
|
|
|
|
|
|
|
"%{customer} will be Jones Smith" |
|
108
|
|
|
|
|
|
|
"%{customer.0} will be Jones Smith" |
|
109
|
|
|
|
|
|
|
"%{customer.1} will be Jones" |
|
110
|
|
|
|
|
|
|
"%{customer.2} will be Smith" |
|
111
|
|
|
|
|
|
|
|
|
112
|
|
|
|
|
|
|
When the value exceeds the number of elements in the list, an empty |
|
113
|
|
|
|
|
|
|
value is returned. |
|
114
|
|
|
|
|
|
|
Index zero, or no index, will return all values concatenated. |
|
115
|
|
|
|
|
|
|
|
|
116
|
|
|
|
|
|
|
=head2 Format modifiers |
|
117
|
|
|
|
|
|
|
|
|
118
|
|
|
|
|
|
|
A named variable may have I attached to perform |
|
119
|
|
|
|
|
|
|
formatting operations on the substituted value. |
|
120
|
|
|
|
|
|
|
Format modifiers start with a colon C<:>. |
|
121
|
|
|
|
|
|
|
|
|
122
|
|
|
|
|
|
|
Assuming argument C has the value C<"My Book">, then |
|
123
|
|
|
|
|
|
|
C<"%{title:lc}"> |
|
124
|
|
|
|
|
|
|
will yield the title in lowercase C<"my book">. |
|
125
|
|
|
|
|
|
|
|
|
126
|
|
|
|
|
|
|
The following format modifiers are available: |
|
127
|
|
|
|
|
|
|
|
|
128
|
|
|
|
|
|
|
=over 6 |
|
129
|
|
|
|
|
|
|
|
|
130
|
|
|
|
|
|
|
=item C<:lc> |
|
131
|
|
|
|
|
|
|
|
|
132
|
|
|
|
|
|
|
Yields the substituted value in all lower case. |
|
133
|
|
|
|
|
|
|
|
|
134
|
|
|
|
|
|
|
Using the example above, this will be C<"my book">. |
|
135
|
|
|
|
|
|
|
|
|
136
|
|
|
|
|
|
|
=item C<:uc> |
|
137
|
|
|
|
|
|
|
|
|
138
|
|
|
|
|
|
|
Yields the substituted value in all upper case. |
|
139
|
|
|
|
|
|
|
|
|
140
|
|
|
|
|
|
|
Using the example above, this will be C<"MY BOOK">. |
|
141
|
|
|
|
|
|
|
|
|
142
|
|
|
|
|
|
|
=item C<:ic> |
|
143
|
|
|
|
|
|
|
|
|
144
|
|
|
|
|
|
|
Yields the substituted value with initial caps, e.g. the first letter |
|
145
|
|
|
|
|
|
|
of each word is capitalized. |
|
146
|
|
|
|
|
|
|
|
|
147
|
|
|
|
|
|
|
Using the example above, this will be C<"My Book">. |
|
148
|
|
|
|
|
|
|
Indeed, no difference since the value is already correctly cased. |
|
149
|
|
|
|
|
|
|
|
|
150
|
|
|
|
|
|
|
To enforce lower case before applying initial case, use format modifiers |
|
151
|
|
|
|
|
|
|
C<:lc:ic>. |
|
152
|
|
|
|
|
|
|
|
|
153
|
|
|
|
|
|
|
=item C<:sc> |
|
154
|
|
|
|
|
|
|
|
|
155
|
|
|
|
|
|
|
Yields the substituted value with an initial cap and the rest lower case. |
|
156
|
|
|
|
|
|
|
|
|
157
|
|
|
|
|
|
|
Using the example above, this will be C<"My Book">. |
|
158
|
|
|
|
|
|
|
Again, no difference since the value already has an initial capital. |
|
159
|
|
|
|
|
|
|
|
|
160
|
|
|
|
|
|
|
To enforce lower case before applying initial case, use format modifiers |
|
161
|
|
|
|
|
|
|
C<:lc:sc>. Now the result will be C<"My book">. |
|
162
|
|
|
|
|
|
|
|
|
163
|
|
|
|
|
|
|
=item C<:lpad(>IC<)> C<:lpad(>IC<,>IC<)> |
|
164
|
|
|
|
|
|
|
|
|
165
|
|
|
|
|
|
|
Pads the value by repeatedly prepending the string I until the |
|
166
|
|
|
|
|
|
|
total width is I. |
|
167
|
|
|
|
|
|
|
|
|
168
|
|
|
|
|
|
|
If I is omitted, uses spaces. |
|
169
|
|
|
|
|
|
|
|
|
170
|
|
|
|
|
|
|
=item C<:rpad(>IC<)> C<:rpad(>IC<,>IC<)> |
|
171
|
|
|
|
|
|
|
|
|
172
|
|
|
|
|
|
|
Pads the value by repeatedly appending the string I until the |
|
173
|
|
|
|
|
|
|
total width is I. |
|
174
|
|
|
|
|
|
|
|
|
175
|
|
|
|
|
|
|
If I is omitted, uses spaces. |
|
176
|
|
|
|
|
|
|
|
|
177
|
|
|
|
|
|
|
=item C<:replace(>IC<,>IC<)> |
|
178
|
|
|
|
|
|
|
|
|
179
|
|
|
|
|
|
|
Replaces all occurrences of I by I, |
|
180
|
|
|
|
|
|
|
|
|
181
|
|
|
|
|
|
|
If I is omitted, uses spaces. |
|
182
|
|
|
|
|
|
|
|
|
183
|
|
|
|
|
|
|
=item C<:%>I |
|
184
|
|
|
|
|
|
|
|
|
185
|
|
|
|
|
|
|
Apply standard printf() formatting, e.g. C<%{key:%03d}> yields the numeric |
|
186
|
|
|
|
|
|
|
value of C as a 3-digit string, adding leading zeroes if necessary. |
|
187
|
|
|
|
|
|
|
|
|
188
|
|
|
|
|
|
|
=back |
|
189
|
|
|
|
|
|
|
|
|
190
|
|
|
|
|
|
|
Note that, when combining formatting and conditional interpolation, |
|
191
|
|
|
|
|
|
|
you must check for the I value: |
|
192
|
|
|
|
|
|
|
|
|
193
|
|
|
|
|
|
|
"This takes %{days:%02d=01|%{} day|%{} days}" |
|
194
|
|
|
|
|
|
|
|
|
195
|
|
|
|
|
|
|
You can prevent a colon from splitting formatters with a backslash: |
|
196
|
|
|
|
|
|
|
|
|
197
|
|
|
|
|
|
|
%{title:replace( ,\:)} |
|
198
|
|
|
|
|
|
|
|
|
199
|
|
|
|
|
|
|
=head2 The Control Hash |
|
200
|
|
|
|
|
|
|
|
|
201
|
|
|
|
|
|
|
The interpolation process requires two parameters: a hash with |
|
202
|
|
|
|
|
|
|
settings and values for the named arguments, and the string to be used |
|
203
|
|
|
|
|
|
|
as a template for interpolation. The hash will be further referred to |
|
204
|
|
|
|
|
|
|
as the I. |
|
205
|
|
|
|
|
|
|
|
|
206
|
|
|
|
|
|
|
The hash can have the following keys: |
|
207
|
|
|
|
|
|
|
|
|
208
|
|
|
|
|
|
|
=over |
|
209
|
|
|
|
|
|
|
|
|
210
|
|
|
|
|
|
|
=item args |
|
211
|
|
|
|
|
|
|
|
|
212
|
|
|
|
|
|
|
This is either a hash that contains replacement texts for the named |
|
213
|
|
|
|
|
|
|
variables, or a subroutine that gets called with a variable as |
|
214
|
|
|
|
|
|
|
argument and returns a replacement value. |
|
215
|
|
|
|
|
|
|
|
|
216
|
|
|
|
|
|
|
This element should be considered mandatory. |
|
217
|
|
|
|
|
|
|
|
|
218
|
|
|
|
|
|
|
=item separator |
|
219
|
|
|
|
|
|
|
|
|
220
|
|
|
|
|
|
|
The separator used to concatenate list values, see L above. |
|
221
|
|
|
|
|
|
|
|
|
222
|
|
|
|
|
|
|
It defaults to Perl variable C<$"> that, on its turn, defaults to a |
|
223
|
|
|
|
|
|
|
single space. |
|
224
|
|
|
|
|
|
|
|
|
225
|
|
|
|
|
|
|
=item activator |
|
226
|
|
|
|
|
|
|
|
|
227
|
|
|
|
|
|
|
This is a single character that activates interpolation. By default |
|
228
|
|
|
|
|
|
|
this is the percent C<%> character. |
|
229
|
|
|
|
|
|
|
|
|
230
|
|
|
|
|
|
|
=item keypattern |
|
231
|
|
|
|
|
|
|
|
|
232
|
|
|
|
|
|
|
The pattern to match key names. Default is C. |
|
233
|
|
|
|
|
|
|
|
|
234
|
|
|
|
|
|
|
=item maxiter |
|
235
|
|
|
|
|
|
|
|
|
236
|
|
|
|
|
|
|
To enable nested substitutions and recursive replacement, the |
|
237
|
|
|
|
|
|
|
interpolation process is repeated until there are no more |
|
238
|
|
|
|
|
|
|
interpolations to be made. The maximun number of iterations is limited |
|
239
|
|
|
|
|
|
|
to the value of C. |
|
240
|
|
|
|
|
|
|
|
|
241
|
|
|
|
|
|
|
By default maxiter is 16. |
|
242
|
|
|
|
|
|
|
|
|
243
|
|
|
|
|
|
|
=back |
|
244
|
|
|
|
|
|
|
|
|
245
|
|
|
|
|
|
|
An example of a control hash: |
|
246
|
|
|
|
|
|
|
|
|
247
|
|
|
|
|
|
|
my %ctl = |
|
248
|
|
|
|
|
|
|
( args => { |
|
249
|
|
|
|
|
|
|
customer => [ "Jones", "Smith" ], |
|
250
|
|
|
|
|
|
|
days => 2, |
|
251
|
|
|
|
|
|
|
title => "My Title", |
|
252
|
|
|
|
|
|
|
}, |
|
253
|
|
|
|
|
|
|
separator => ", ", |
|
254
|
|
|
|
|
|
|
); |
|
255
|
|
|
|
|
|
|
|
|
256
|
|
|
|
|
|
|
=head2 Object Oriented API |
|
257
|
|
|
|
|
|
|
|
|
258
|
|
|
|
|
|
|
my $ii = String::Interpolate::Named->new; |
|
259
|
|
|
|
|
|
|
$ii->ctl(\%ctl); |
|
260
|
|
|
|
|
|
|
$result = $ii->interpolate($template); |
|
261
|
|
|
|
|
|
|
|
|
262
|
|
|
|
|
|
|
For convenience, the control hash may be passed to the constructor: |
|
263
|
|
|
|
|
|
|
|
|
264
|
|
|
|
|
|
|
my $ii = String::Interpolate::Named->new(\%ctl); |
|
265
|
|
|
|
|
|
|
$result = $ii->interpolate($template); |
|
266
|
|
|
|
|
|
|
|
|
267
|
|
|
|
|
|
|
=head2 Functional API |
|
268
|
|
|
|
|
|
|
|
|
269
|
|
|
|
|
|
|
String::Interpolate::Named privides a single function, C, |
|
270
|
|
|
|
|
|
|
which is exported by default. |
|
271
|
|
|
|
|
|
|
|
|
272
|
|
|
|
|
|
|
The subroutine takes two arguments: a reference to a control hash and |
|
273
|
|
|
|
|
|
|
the template string. |
|
274
|
|
|
|
|
|
|
|
|
275
|
|
|
|
|
|
|
$result = interpolate( \%ctl, $template ); |
|
276
|
|
|
|
|
|
|
|
|
277
|
|
|
|
|
|
|
=cut |
|
278
|
|
|
|
|
|
|
|
|
279
|
|
|
|
|
|
|
=head1 METHODS |
|
280
|
|
|
|
|
|
|
|
|
281
|
|
|
|
|
|
|
=head2 new |
|
282
|
|
|
|
|
|
|
|
|
283
|
|
|
|
|
|
|
Constructs a new String::Interpolate::Named object. |
|
284
|
|
|
|
|
|
|
|
|
285
|
|
|
|
|
|
|
my $ii = String::Interpolate::Named->new; |
|
286
|
|
|
|
|
|
|
|
|
287
|
|
|
|
|
|
|
or |
|
288
|
|
|
|
|
|
|
|
|
289
|
|
|
|
|
|
|
my $ii = String::Interpolate::Named->new(\%ctl); |
|
290
|
|
|
|
|
|
|
|
|
291
|
|
|
|
|
|
|
=cut |
|
292
|
|
|
|
|
|
|
|
|
293
|
|
|
|
|
|
|
sub new { |
|
294
|
2
|
|
|
2
|
1
|
1566
|
my ( $pkg, $ctl ) = @_; |
|
295
|
2
|
|
100
|
|
|
15
|
$ctl //= {}; |
|
296
|
2
|
|
|
|
|
8
|
bless $ctl => $pkg; |
|
297
|
|
|
|
|
|
|
} |
|
298
|
|
|
|
|
|
|
|
|
299
|
|
|
|
|
|
|
=head2 ctl |
|
300
|
|
|
|
|
|
|
|
|
301
|
|
|
|
|
|
|
Associates a control has with an existing object. |
|
302
|
|
|
|
|
|
|
|
|
303
|
|
|
|
|
|
|
$ii->ctl(\%ctl); |
|
304
|
|
|
|
|
|
|
|
|
305
|
|
|
|
|
|
|
=cut |
|
306
|
|
|
|
|
|
|
|
|
307
|
|
|
|
|
|
|
sub ctl { |
|
308
|
1
|
|
|
1
|
1
|
19
|
my ( $self, $ctl ) = @_; |
|
309
|
1
|
|
|
|
|
9
|
$self->{$_} = $ctl->{$_} for keys(%$ctl); |
|
310
|
1
|
|
|
|
|
3
|
return $self; |
|
311
|
|
|
|
|
|
|
} |
|
312
|
|
|
|
|
|
|
|
|
313
|
|
|
|
|
|
|
=head2 interpolate |
|
314
|
|
|
|
|
|
|
|
|
315
|
|
|
|
|
|
|
This routine performs the actual interpolations. It can be used as a method: |
|
316
|
|
|
|
|
|
|
|
|
317
|
|
|
|
|
|
|
$ii->interpolate($template); |
|
318
|
|
|
|
|
|
|
|
|
319
|
|
|
|
|
|
|
and functional: |
|
320
|
|
|
|
|
|
|
|
|
321
|
|
|
|
|
|
|
interpolate( \%ctl, $template ); |
|
322
|
|
|
|
|
|
|
|
|
323
|
|
|
|
|
|
|
=cut |
|
324
|
|
|
|
|
|
|
|
|
325
|
|
|
|
|
|
|
sub interpolate { |
|
326
|
434
|
|
|
434
|
1
|
526251
|
my ( $ctl, $tpl ) = @_; |
|
327
|
|
|
|
|
|
|
|
|
328
|
434
|
|
50
|
|
|
2657
|
my $maxiter = $ctl->{maxiter} // 16; |
|
329
|
434
|
|
100
|
|
|
1647
|
my $activator = $ctl->{activator} // '%'; |
|
330
|
434
|
|
66
|
|
|
2801
|
my $keypat = $ctl->{keypattern} // qr/\w+[-_\w.]*/; |
|
331
|
|
|
|
|
|
|
|
|
332
|
434
|
|
|
|
|
1746
|
for ( my $cnt = 1; $cnt <= $maxiter; $cnt++ ) { |
|
333
|
|
|
|
|
|
|
|
|
334
|
829
|
|
|
|
|
1643
|
my $prev = $tpl; |
|
335
|
|
|
|
|
|
|
|
|
336
|
|
|
|
|
|
|
# Hide escaped specials by replacing them with Unicode noncharacters. |
|
337
|
829
|
|
|
|
|
2025
|
$tpl =~ s/\\\\/\x{fdd0}/g; |
|
338
|
829
|
|
|
|
|
1665
|
$tpl =~ s/\\\{/\x{fdd1}/g; |
|
339
|
829
|
|
|
|
|
1630
|
$tpl =~ s/\\\}/\x{fdd2}/g; |
|
340
|
829
|
|
|
|
|
2180
|
$tpl =~ s/\\\|/\x{fdd3}/g; |
|
341
|
829
|
|
|
|
|
4031
|
$tpl =~ s/\\\Q$activator\E/\x{fdd4}/g; |
|
342
|
|
|
|
|
|
|
|
|
343
|
|
|
|
|
|
|
# Replace some seqs by a single char for easy matching. |
|
344
|
829
|
|
|
|
|
3194
|
$tpl =~ s/\Q$activator\E\{\}/\x{fdde}/g; |
|
345
|
829
|
|
|
|
|
3764
|
$tpl =~ s/\Q$activator\E\{/\x{fddf}/g; |
|
346
|
|
|
|
|
|
|
|
|
347
|
|
|
|
|
|
|
# %{ key [ .index ] [ = value ] [ | then [ | else ] ] } |
|
348
|
|
|
|
|
|
|
|
|
349
|
829
|
|
|
|
|
1685
|
my $pre = ''; |
|
350
|
829
|
|
|
|
|
1444
|
my $post = ''; |
|
351
|
829
|
100
|
|
|
|
9122
|
if ( $tpl =~ s; ( ^ |
|
352
|
|
|
|
|
|
|
(? .*? ) |
|
353
|
|
|
|
|
|
|
\x{fddf} |
|
354
|
|
|
|
|
|
|
(? $keypat ) |
|
355
|
|
|
|
|
|
|
(?: : (? .*? ) )? |
|
356
|
|
|
|
|
|
|
(?: (? \= ) |
|
357
|
|
|
|
|
|
|
(? [^|}\x{fddf}]*) )? |
|
358
|
|
|
|
|
|
|
(?: \| (? [^|}\x{fddf}]* ) |
|
359
|
|
|
|
|
|
|
(?: \| (? [^|}\x{fddf}]* ) )? |
|
360
|
|
|
|
|
|
|
)? |
|
361
|
|
|
|
|
|
|
\} |
|
362
|
|
|
|
|
|
|
(? .* ) |
|
363
|
|
|
|
|
|
|
$ |
|
364
|
|
|
|
|
|
|
) |
|
365
|
395
|
|
|
|
|
12639
|
; _interpolate($ctl, {%+} ) ;exso ) { |
|
366
|
395
|
|
|
|
|
3006
|
$pre = $+{pre}; |
|
367
|
395
|
|
|
|
|
1895
|
$post = $+{post}; |
|
368
|
|
|
|
|
|
|
} |
|
369
|
|
|
|
|
|
|
else { |
|
370
|
434
|
|
|
|
|
799
|
$pre = $tpl; |
|
371
|
434
|
|
|
|
|
953
|
$tpl = ''; |
|
372
|
|
|
|
|
|
|
} |
|
373
|
829
|
|
|
|
|
3551
|
for ( $pre, $tpl, $post ) { |
|
374
|
|
|
|
|
|
|
# Unescape escaped specials. |
|
375
|
2487
|
|
|
|
|
4824
|
s/\x{fdd0}/\\\\/g; |
|
376
|
2487
|
|
|
|
|
4562
|
s/\x{fdd1}/\\\{/g; |
|
377
|
2487
|
|
|
|
|
4444
|
s/\x{fdd2}/\\\}/g; |
|
378
|
2487
|
|
|
|
|
4572
|
s/\x{fdd3}/\\\|/g; |
|
379
|
2487
|
|
|
|
|
4626
|
s/\x{fdd4}/\\$activator/g; |
|
380
|
|
|
|
|
|
|
|
|
381
|
|
|
|
|
|
|
# Restore (some) seqs. |
|
382
|
2487
|
|
|
|
|
4389
|
s/\x{fdde}/$activator."{}"/ge; |
|
|
6
|
|
|
|
|
50
|
|
|
383
|
2487
|
|
|
|
|
5221
|
s/\x{fddf}/$activator."{"/ge; |
|
|
48
|
|
|
|
|
217
|
|
|
384
|
|
|
|
|
|
|
} |
|
385
|
829
|
|
|
|
|
4634
|
$tpl =~ s/\\(\Q$activator\E|[{}|\\])/$1/g; |
|
386
|
829
|
50
|
|
|
|
2653
|
warn ("'$prev' => '$pre' '$tpl' '$post'\n" ) if $ctl->{trace}; |
|
387
|
|
|
|
|
|
|
|
|
388
|
829
|
|
|
|
|
1862
|
my $t = $pre . $tpl . $post; |
|
389
|
829
|
100
|
|
|
|
4863
|
if ( $prev eq $t ) { |
|
390
|
|
|
|
|
|
|
# De-escape in subst part only (issue #6); |
|
391
|
434
|
|
|
|
|
1604
|
$tpl =~ s/\\(\Q$activator\E|[{}|])/$1/g; |
|
392
|
434
|
|
|
|
|
3063
|
return $pre . $tpl . $post; |
|
393
|
|
|
|
|
|
|
} |
|
394
|
395
|
|
|
|
|
986
|
$tpl = $t; |
|
395
|
395
|
50
|
|
|
|
1794
|
warn("$cnt: $prev -> $tpl\n") if $ctl->{trace}; |
|
396
|
|
|
|
|
|
|
} |
|
397
|
0
|
|
|
|
|
0
|
Carp::croak("Maximum number of iterations exceeded"); |
|
398
|
|
|
|
|
|
|
} |
|
399
|
|
|
|
|
|
|
|
|
400
|
|
|
|
|
|
|
sub _interpolate { |
|
401
|
395
|
|
|
395
|
|
1070
|
my ( $ctl, $i ) = @_; |
|
402
|
395
|
|
50
|
|
|
1465
|
my $key = $i->{key} // ''; |
|
403
|
395
|
|
|
|
|
850
|
my $m = $ctl->{args}; |
|
404
|
|
|
|
|
|
|
|
|
405
|
|
|
|
|
|
|
# Establish the value for this key. |
|
406
|
395
|
|
|
|
|
711
|
my $val = ''; |
|
407
|
395
|
|
|
|
|
707
|
my $inx = 0; |
|
408
|
|
|
|
|
|
|
|
|
409
|
|
|
|
|
|
|
# Split off possible index. |
|
410
|
395
|
100
|
|
|
|
1491
|
if ( $key =~ /^(.*)\.(-?\d+)$/ ) { |
|
411
|
7
|
|
|
|
|
28
|
( $key, $inx ) = ( $1, $2 ); |
|
412
|
|
|
|
|
|
|
} |
|
413
|
|
|
|
|
|
|
|
|
414
|
395
|
100
|
|
|
|
1654
|
my $newval = ref($m) eq 'CODE' ? $m->($key) : $m->{$key}; |
|
415
|
395
|
100
|
|
|
|
1441
|
if ( defined $newval ) { |
|
416
|
314
|
|
|
|
|
668
|
$val = $newval; |
|
417
|
|
|
|
|
|
|
|
|
418
|
314
|
100
|
|
|
|
1329
|
if ( UNIVERSAL::isa( $val, 'ARRAY' ) ) { |
|
|
|
50
|
|
|
|
|
|
|
419
|
|
|
|
|
|
|
# 1, 2, ... selects 1st, 2nd value; -1 counts from end. |
|
420
|
236
|
100
|
|
|
|
569
|
if ( $inx ) { |
|
421
|
6
|
100
|
|
|
|
20
|
if ( $inx > 0 ) { |
|
422
|
4
|
100
|
|
|
|
13
|
if ( $inx <= @$val ) { |
|
423
|
3
|
|
|
|
|
9
|
$val = $val->[$inx-1]; |
|
424
|
|
|
|
|
|
|
} |
|
425
|
|
|
|
|
|
|
else { |
|
426
|
1
|
|
|
|
|
3
|
$val = ""; |
|
427
|
|
|
|
|
|
|
} |
|
428
|
|
|
|
|
|
|
} |
|
429
|
|
|
|
|
|
|
else { |
|
430
|
2
|
|
|
|
|
7
|
$val = $val->[$inx]; |
|
431
|
|
|
|
|
|
|
} |
|
432
|
|
|
|
|
|
|
} |
|
433
|
|
|
|
|
|
|
# Zero or none means concatenate all. |
|
434
|
|
|
|
|
|
|
else { |
|
435
|
230
|
|
66
|
|
|
1312
|
$val = join( $ctl->{separator} // $", @$val ); |
|
436
|
|
|
|
|
|
|
} |
|
437
|
|
|
|
|
|
|
} |
|
438
|
|
|
|
|
|
|
elsif ( $inx ) { |
|
439
|
0
|
|
|
|
|
0
|
Carp::croak("Expecting an array for variable '$key'") |
|
440
|
|
|
|
|
|
|
} |
|
441
|
|
|
|
|
|
|
} |
|
442
|
|
|
|
|
|
|
|
|
443
|
395
|
|
|
|
|
837
|
my $subst = ''; |
|
444
|
395
|
|
100
|
|
|
2125
|
for ( split( /(?{fmt}//'' ) ) { |
|
445
|
165
|
100
|
|
|
|
462
|
last unless defined $newval; |
|
446
|
155
|
50
|
|
|
|
453
|
next unless my $fmt = $_; |
|
447
|
|
|
|
|
|
|
|
|
448
|
|
|
|
|
|
|
# Simple formatters. |
|
449
|
155
|
100
|
|
|
|
1196
|
if ( $fmt eq 'lc' ) { $val = lc($val) } |
|
|
15
|
100
|
|
|
|
51
|
|
|
|
|
100
|
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
|
450
|
5
|
|
|
|
|
22
|
elsif ( $fmt eq 'uc' ) { $val = uc($val) } |
|
451
|
10
|
|
|
|
|
54
|
elsif ( $fmt eq 'sc' ) { $val = ucfirst($val) } |
|
452
|
10
|
|
|
|
|
43
|
elsif ( $fmt eq 'ic' ) { $val = f_ic($val) } |
|
453
|
|
|
|
|
|
|
|
|
454
|
|
|
|
|
|
|
# Functions. |
|
455
|
|
|
|
|
|
|
elsif ( $fmt =~ /^([lr])pad\((\d+)(?:,(.*?))?\)$/ ) { |
|
456
|
55
|
|
|
|
|
181
|
$val = f_pad( $val, $1, $2, $3 ); |
|
457
|
|
|
|
|
|
|
} |
|
458
|
|
|
|
|
|
|
|
|
459
|
|
|
|
|
|
|
elsif ( $fmt =~ /^replace\((.+?),(.*?)\)$/ ) { |
|
460
|
40
|
|
|
|
|
119
|
$val = f_replace( $val, $1, $2 ); |
|
461
|
|
|
|
|
|
|
} |
|
462
|
|
|
|
|
|
|
|
|
463
|
|
|
|
|
|
|
# Printf formatting. |
|
464
|
20
|
|
|
|
|
64
|
elsif ( $fmt =~ /^%/ ) { $val = f_printf( $val, $fmt ) } |
|
465
|
|
|
|
|
|
|
|
|
466
|
0
|
|
|
|
|
0
|
else { Carp::croak("Invalid format code '$fmt'"); } |
|
467
|
|
|
|
|
|
|
} |
|
468
|
395
|
100
|
|
|
|
1307
|
if ( $i->{op} ) { |
|
|
|
100
|
|
|
|
|
|
|
469
|
76
|
|
50
|
|
|
236
|
my $test = $i->{test} // ''; |
|
470
|
76
|
100
|
66
|
|
|
440
|
if ( $i->{op} eq '=' && $val eq $test ) { |
|
471
|
40
|
|
100
|
|
|
258
|
$subst = $i->{then} // ''; |
|
472
|
|
|
|
|
|
|
} |
|
473
|
|
|
|
|
|
|
else { |
|
474
|
36
|
|
100
|
|
|
163
|
$subst = $i->{else} // ''; |
|
475
|
|
|
|
|
|
|
} |
|
476
|
|
|
|
|
|
|
} |
|
477
|
|
|
|
|
|
|
elsif ( $val ne '' ) { |
|
478
|
254
|
|
100
|
|
|
1009
|
$subst = $i->{then} // $val; |
|
479
|
|
|
|
|
|
|
} |
|
480
|
|
|
|
|
|
|
else { |
|
481
|
65
|
|
100
|
|
|
366
|
$subst = $i->{else} // ''; |
|
482
|
|
|
|
|
|
|
} |
|
483
|
|
|
|
|
|
|
|
|
484
|
395
|
|
|
|
|
1140
|
$subst =~ s/\x{fdde}/$val/g; |
|
485
|
395
|
|
|
|
|
2454
|
return $subst; |
|
486
|
|
|
|
|
|
|
} |
|
487
|
|
|
|
|
|
|
|
|
488
|
|
|
|
|
|
|
# Formatter functions. |
|
489
|
|
|
|
|
|
|
# First arg = $val. |
|
490
|
|
|
|
|
|
|
# Return new value. |
|
491
|
|
|
|
|
|
|
|
|
492
|
|
|
|
|
|
|
sub f_ic { |
|
493
|
10
|
|
|
10
|
0
|
27
|
my ( $val ) = @_; |
|
494
|
10
|
|
|
|
|
103
|
join('', map { ucfirst } (split( /(^|\s+|-)/, $val ))); |
|
|
30
|
|
|
|
|
157
|
|
|
495
|
|
|
|
|
|
|
} |
|
496
|
|
|
|
|
|
|
|
|
497
|
|
|
|
|
|
|
sub f_pad { |
|
498
|
55
|
|
|
55
|
0
|
328
|
my ( $val, $lr, $len, $str ) = @_; |
|
499
|
55
|
|
100
|
|
|
269
|
$str //= " "; |
|
500
|
55
|
100
|
|
|
|
263
|
return $val unless ( my $need = $len - length($val) ) > 0; |
|
501
|
30
|
|
|
|
|
171
|
my $pad = $str x (1+int(($len-1)/length($str))); |
|
502
|
30
|
100
|
|
|
|
95
|
if ( $lr eq 'l' ) { |
|
503
|
15
|
|
|
|
|
125
|
return substr( $pad, 0, $need ) . $val; |
|
504
|
|
|
|
|
|
|
} |
|
505
|
15
|
|
|
|
|
91
|
$val . substr( $pad, 0, $need ); |
|
506
|
|
|
|
|
|
|
} |
|
507
|
|
|
|
|
|
|
|
|
508
|
|
|
|
|
|
|
sub f_replace { |
|
509
|
40
|
|
|
40
|
0
|
244
|
my ( $val, $rep, $str ) = @_; |
|
510
|
40
|
|
|
|
|
725
|
$val =~ s/\Q$rep\E/$str/g; |
|
511
|
40
|
|
|
|
|
187
|
$val; |
|
512
|
|
|
|
|
|
|
} |
|
513
|
|
|
|
|
|
|
|
|
514
|
|
|
|
|
|
|
sub f_printf { |
|
515
|
20
|
|
|
20
|
0
|
52
|
my ( $val, $fmt ) = @_; |
|
516
|
|
|
|
|
|
|
# A common problem is when a numeric format does not |
|
517
|
|
|
|
|
|
|
# have a value to format. Suppress the warning. |
|
518
|
8
|
|
|
8
|
|
21471
|
no warnings qw(numeric); |
|
|
8
|
|
|
|
|
18
|
|
|
|
8
|
|
|
|
|
948
|
|
|
519
|
20
|
|
|
|
|
167
|
sprintf( $fmt, $val ); |
|
520
|
|
|
|
|
|
|
} |
|
521
|
|
|
|
|
|
|
|
|
522
|
|
|
|
|
|
|
=head1 REQUIREMENTS |
|
523
|
|
|
|
|
|
|
|
|
524
|
|
|
|
|
|
|
Minimal Perl version 5.10.1. |
|
525
|
|
|
|
|
|
|
|
|
526
|
|
|
|
|
|
|
=head1 AUTHOR |
|
527
|
|
|
|
|
|
|
|
|
528
|
|
|
|
|
|
|
Johan Vromans, C<< >> |
|
529
|
|
|
|
|
|
|
|
|
530
|
|
|
|
|
|
|
=head1 SUPPORT |
|
531
|
|
|
|
|
|
|
|
|
532
|
|
|
|
|
|
|
Development of this module takes place on GitHub: |
|
533
|
|
|
|
|
|
|
L. |
|
534
|
|
|
|
|
|
|
|
|
535
|
|
|
|
|
|
|
You can find documentation for this module with the perldoc command. |
|
536
|
|
|
|
|
|
|
|
|
537
|
|
|
|
|
|
|
perldoc String::Interpolate::Named |
|
538
|
|
|
|
|
|
|
|
|
539
|
|
|
|
|
|
|
Please report any bugs or feature requests using the issue tracker on |
|
540
|
|
|
|
|
|
|
GitHub. |
|
541
|
|
|
|
|
|
|
|
|
542
|
|
|
|
|
|
|
=head1 ACKNOWLEDGEMENTS |
|
543
|
|
|
|
|
|
|
|
|
544
|
|
|
|
|
|
|
Many of the existing template / interpolate / substitute modules. |
|
545
|
|
|
|
|
|
|
|
|
546
|
|
|
|
|
|
|
=head1 COPYRIGHT & LICENSE |
|
547
|
|
|
|
|
|
|
|
|
548
|
|
|
|
|
|
|
Copyright 2018,2025 Johan Vromans, all rights reserved. |
|
549
|
|
|
|
|
|
|
|
|
550
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or modify it |
|
551
|
|
|
|
|
|
|
under the same terms as Perl itself. |
|
552
|
|
|
|
|
|
|
|
|
553
|
|
|
|
|
|
|
=cut |
|
554
|
|
|
|
|
|
|
|
|
555
|
|
|
|
|
|
|
1; |