| 1 | #! perl | ||||||||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||||||||
| 3 | package String::Interpolate::Named; | ||||||||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||||||||
| 5 | 8 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 6 | 8 | ▶ | use strict; | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 7 | 8 | ▶ | use utf8; | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 8 | 8 | ▶ | use Carp qw( carp croak ); | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||||||||
| 10 | # Disable 'Unicode character 0xfddX is illegal' warnings. | ||||||||||||||||||||||||||||||||||
| 11 | 8 | ▶ | no if $] < 5.014, q|warnings|, qw(utf8); | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||||||||
| 13 | 8 | ▶ | use parent 'Exporter'; | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 14 | 8 | our @EXPORT = qw( interpolate ); | |||||||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||||||||
| 16 | =head1 NAME | ||||||||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||||||||
| 18 | String::Interpolate::Named - Interpolated named arguments in string | ||||||||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||||||||
| 20 | =cut | ||||||||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||||||||
| 22 | 8 | 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<arguments> by I<target texts> 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<fn> and C<ln>. | ||||||||||||||||||||||||||||||||||
| 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<fn> and C<ln> 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<not> 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<title> 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<title> 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<customer> 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<format modifiers> attached to perform | ||||||||||||||||||||||||||||||||||
| 119 | formatting operations on the substituted value. | ||||||||||||||||||||||||||||||||||
| 120 | Format modifiers start with a colon C<:>. | ||||||||||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||||||||
| 122 | Assuming argument C<title> 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(>I<N>C<)> C<:lpad(>I<N>C<,>I<S>C<)> | ||||||||||||||||||||||||||||||||||
| 164 | |||||||||||||||||||||||||||||||||||
| 165 | Pads the value by repeatedly prepending the string I<S> until the | ||||||||||||||||||||||||||||||||||
| 166 | total width is I<N>. | ||||||||||||||||||||||||||||||||||
| 167 | |||||||||||||||||||||||||||||||||||
| 168 | If I<S> is omitted, uses spaces. | ||||||||||||||||||||||||||||||||||
| 169 | |||||||||||||||||||||||||||||||||||
| 170 | =item C<:rpad(>I<N>C<)> C<:rpad(>I<N>C<,>I<S>C<)> | ||||||||||||||||||||||||||||||||||
| 171 | |||||||||||||||||||||||||||||||||||
| 172 | Pads the value by repeatedly appending the string I<S> until the | ||||||||||||||||||||||||||||||||||
| 173 | total width is I<N>. | ||||||||||||||||||||||||||||||||||
| 174 | |||||||||||||||||||||||||||||||||||
| 175 | If I<S> is omitted, uses spaces. | ||||||||||||||||||||||||||||||||||
| 176 | |||||||||||||||||||||||||||||||||||
| 177 | =item C<:replace(>I<SRC>C<,>I<DST>C<)> | ||||||||||||||||||||||||||||||||||
| 178 | |||||||||||||||||||||||||||||||||||
| 179 | Replaces all occurrences of I<STR> by I<DST>, | ||||||||||||||||||||||||||||||||||
| 180 | |||||||||||||||||||||||||||||||||||
| 181 | If I<S> is omitted, uses spaces. | ||||||||||||||||||||||||||||||||||
| 182 | |||||||||||||||||||||||||||||||||||
| 183 | =item C<:%>I<fmt> | ||||||||||||||||||||||||||||||||||
| 184 | |||||||||||||||||||||||||||||||||||
| 185 | Apply standard printf() formatting, e.g. C<%{key:%03d}> yields the numeric | ||||||||||||||||||||||||||||||||||
| 186 | value of C<key> 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<formatted> 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<control hash>. | ||||||||||||||||||||||||||||||||||
| 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<List Values> 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<qr/\w+[-_\w.]*/>. | ||||||||||||||||||||||||||||||||||
| 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<maxiter>. | ||||||||||||||||||||||||||||||||||
| 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<interpolate>, | ||||||||||||||||||||||||||||||||||
| 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 | 2 | ▶ | sub new { | ||||||||||||||||||||||||||||||||
|
Subroutine
new: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||
| 294 | 2 | my ( $pkg, $ctl ) = @_; | |||||||||||||||||||||||||||||||||
| 295 | 2 | ▶ | $ctl //= {}; | ||||||||||||||||||||||||||||||||
|
Condition100%2/2
$ctl //= {}
MC/DC100%1/1
$ctl //= {}
$ctl
Truth table100%2/2
A
A= $ctl
| |||||||||||||||||||||||||||||||||||
| 296 | 2 | 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 | 1 | ▶ | sub ctl { | ||||||||||||||||||||||||||||||||
|
Subroutine
ctl: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||
| 308 | 1 | my ( $self, $ctl ) = @_; | |||||||||||||||||||||||||||||||||
| 309 | 1 | $self->{$_} = $ctl->{$_} for keys(%$ctl); | |||||||||||||||||||||||||||||||||
| 310 | 1 | 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 | 434 | ▶ | sub interpolate { | ||||||||||||||||||||||||||||||||
|
Subroutine
interpolate: called CC=10 SCAR=23.1
Pod
documented
| |||||||||||||||||||||||||||||||||||
| 326 | 434 | my ( $ctl, $tpl ) = @_; | |||||||||||||||||||||||||||||||||
| 327 | |||||||||||||||||||||||||||||||||||
| 328 | 434 | ▶ | my $maxiter = $ctl->{maxiter} // 16; | ||||||||||||||||||||||||||||||||
|
Condition50%1/2
$ctl->{"maxiter"} // 16
MC/DC0%0/1
$ctl->{"maxiter"} // 16
$ctl->{"maxiter"}
Truth table50%1/2
A
A= $ctl->{"maxiter"}
| |||||||||||||||||||||||||||||||||||
| 329 | 434 | ▶ | my $activator = $ctl->{activator} // '%'; | ||||||||||||||||||||||||||||||||
|
Condition100%2/2
$ctl->{"activator"} // "%"
MC/DC100%1/1
$ctl->{"activator"} // "%"
$ctl->{"activator"}
Truth table100%2/2
A
A= $ctl->{"activator"}
| |||||||||||||||||||||||||||||||||||
| 330 | 434 | ▶ | my $keypat = $ctl->{keypattern} // qr/\w+[-_\w.]*/; | ||||||||||||||||||||||||||||||||
|
Condition100%2/2
$ctl->{"keypattern"} // qr/\w+[-_\w.]*/
MC/DC100%1/1
$ctl->{"keypattern"} // qr/\w+[-_\w.]*/
$ctl->{"keypattern"}
Truth table100%2/2
A
A= $ctl->{"keypattern"}
| |||||||||||||||||||||||||||||||||||
| 331 | |||||||||||||||||||||||||||||||||||
| 332 | 434 | ▶ | for ( my $cnt = 1; $cnt <= $maxiter; $cnt++ ) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 333 | |||||||||||||||||||||||||||||||||||
| 334 | 829 | my $prev = $tpl; | |||||||||||||||||||||||||||||||||
| 335 | |||||||||||||||||||||||||||||||||||
| 336 | # Hide escaped specials by replacing them with Unicode noncharacters. | ||||||||||||||||||||||||||||||||||
| 337 | 829 | $tpl =~ s/\\\\/\x{fdd0}/g; | |||||||||||||||||||||||||||||||||
| 338 | 829 | $tpl =~ s/\\\{/\x{fdd1}/g; | |||||||||||||||||||||||||||||||||
| 339 | 829 | $tpl =~ s/\\\}/\x{fdd2}/g; | |||||||||||||||||||||||||||||||||
| 340 | 829 | $tpl =~ s/\\\|/\x{fdd3}/g; | |||||||||||||||||||||||||||||||||
| 341 | 829 | $tpl =~ s/\\\Q$activator\E/\x{fdd4}/g; | |||||||||||||||||||||||||||||||||
| 342 | |||||||||||||||||||||||||||||||||||
| 343 | # Replace some seqs by a single char for easy matching. | ||||||||||||||||||||||||||||||||||
| 344 | 829 | $tpl =~ s/\Q$activator\E\{\}/\x{fdde}/g; | |||||||||||||||||||||||||||||||||
| 345 | 829 | $tpl =~ s/\Q$activator\E\{/\x{fddf}/g; | |||||||||||||||||||||||||||||||||
| 346 | |||||||||||||||||||||||||||||||||||
| 347 | # %{ key [ .index ] [ = value ] [ | then [ | else ] ] } | ||||||||||||||||||||||||||||||||||
| 348 | |||||||||||||||||||||||||||||||||||
| 349 | 829 | my $pre = ''; | |||||||||||||||||||||||||||||||||
| 350 | 829 | my $post = ''; | |||||||||||||||||||||||||||||||||
| 351 | 829 | ▶ | if ( $tpl =~ s; ( ^ | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 352 | (?<pre> .*? ) | ||||||||||||||||||||||||||||||||||
| 353 | \x{fddf} | ||||||||||||||||||||||||||||||||||
| 354 | (?<key> $keypat ) | ||||||||||||||||||||||||||||||||||
| 355 | (?: : (?<fmt> .*? ) )? | ||||||||||||||||||||||||||||||||||
| 356 | (?: (?<op> \= ) | ||||||||||||||||||||||||||||||||||
| 357 | (?<test> [^|}\x{fddf}]*) )? | ||||||||||||||||||||||||||||||||||
| 358 | (?: \| (?<then> [^|}\x{fddf}]* ) | ||||||||||||||||||||||||||||||||||
| 359 | (?: \| (?<else> [^|}\x{fddf}]* ) )? | ||||||||||||||||||||||||||||||||||
| 360 | )? | ||||||||||||||||||||||||||||||||||
| 361 | \} | ||||||||||||||||||||||||||||||||||
| 362 | (?<post> .* ) | ||||||||||||||||||||||||||||||||||
| 363 | $ | ||||||||||||||||||||||||||||||||||
| 364 | ) | ||||||||||||||||||||||||||||||||||
| 365 | 395 | ; _interpolate($ctl, {%+} ) ;exso ) { | |||||||||||||||||||||||||||||||||
| 366 | 395 | $pre = $+{pre}; | |||||||||||||||||||||||||||||||||
| 367 | 395 | $post = $+{post}; | |||||||||||||||||||||||||||||||||
| 368 | } | ||||||||||||||||||||||||||||||||||
| 369 | else { | ||||||||||||||||||||||||||||||||||
| 370 | 434 | $pre = $tpl; | |||||||||||||||||||||||||||||||||
| 371 | 434 | $tpl = ''; | |||||||||||||||||||||||||||||||||
| 372 | } | ||||||||||||||||||||||||||||||||||
| 373 | 829 | for ( $pre, $tpl, $post ) { | |||||||||||||||||||||||||||||||||
| 374 | # Unescape escaped specials. | ||||||||||||||||||||||||||||||||||
| 375 | 2487 | s/\x{fdd0}/\\\\/g; | |||||||||||||||||||||||||||||||||
| 376 | 2487 | s/\x{fdd1}/\\\{/g; | |||||||||||||||||||||||||||||||||
| 377 | 2487 | s/\x{fdd2}/\\\}/g; | |||||||||||||||||||||||||||||||||
| 378 | 2487 | s/\x{fdd3}/\\\|/g; | |||||||||||||||||||||||||||||||||
| 379 | 2487 | s/\x{fdd4}/\\$activator/g; | |||||||||||||||||||||||||||||||||
| 380 | |||||||||||||||||||||||||||||||||||
| 381 | # Restore (some) seqs. | ||||||||||||||||||||||||||||||||||
| 382 | 2487 | s/\x{fdde}/$activator."{}"/ge; | |||||||||||||||||||||||||||||||||
| 383 | 2487 | s/\x{fddf}/$activator."{"/ge; | |||||||||||||||||||||||||||||||||
| 384 | } | ||||||||||||||||||||||||||||||||||
| 385 | 829 | $tpl =~ s/\\(\Q$activator\E|[{}|\\])/$1/g; | |||||||||||||||||||||||||||||||||
| 386 | 829 | ▶ | warn ("'$prev' => '$pre' '$tpl' '$post'\n" ) if $ctl->{trace}; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 387 | |||||||||||||||||||||||||||||||||||
| 388 | 829 | my $t = $pre . $tpl . $post; | |||||||||||||||||||||||||||||||||
| 389 | 829 | ▶ | if ( $prev eq $t ) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 390 | # De-escape in subst part only (issue #6); | ||||||||||||||||||||||||||||||||||
| 391 | 434 | $tpl =~ s/\\(\Q$activator\E|[{}|])/$1/g; | |||||||||||||||||||||||||||||||||
| 392 | 434 | return $pre . $tpl . $post; | |||||||||||||||||||||||||||||||||
| 393 | } | ||||||||||||||||||||||||||||||||||
| 394 | 395 | $tpl = $t; | |||||||||||||||||||||||||||||||||
| 395 | 395 | ▶ | warn("$cnt: $prev -> $tpl\n") if $ctl->{trace}; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 396 | } | ||||||||||||||||||||||||||||||||||
| 397 | 0 | Carp::croak("Maximum number of iterations exceeded"); | |||||||||||||||||||||||||||||||||
| 398 | } | ||||||||||||||||||||||||||||||||||
| 399 | |||||||||||||||||||||||||||||||||||
| 400 | 395 | ▶ | sub _interpolate { | ||||||||||||||||||||||||||||||||
|
Subroutine
_interpolate: called CC=31 SCAR=34.6
| |||||||||||||||||||||||||||||||||||
| 401 | 395 | my ( $ctl, $i ) = @_; | |||||||||||||||||||||||||||||||||
| 402 | 395 | ▶ | my $key = $i->{key} // ''; | ||||||||||||||||||||||||||||||||
|
Condition50%1/2
$i->{"key"} // ""
MC/DC0%0/1
$i->{"key"} // ""
$i->{"key"}
Truth table50%1/2
A
A= $i->{"key"}
| |||||||||||||||||||||||||||||||||||
| 403 | 395 | my $m = $ctl->{args}; | |||||||||||||||||||||||||||||||||
| 404 | |||||||||||||||||||||||||||||||||||
| 405 | # Establish the value for this key. | ||||||||||||||||||||||||||||||||||
| 406 | 395 | my $val = ''; | |||||||||||||||||||||||||||||||||
| 407 | 395 | my $inx = 0; | |||||||||||||||||||||||||||||||||
| 408 | |||||||||||||||||||||||||||||||||||
| 409 | # Split off possible index. | ||||||||||||||||||||||||||||||||||
| 410 | 395 | ▶ | if ( $key =~ /^(.*)\.(-?\d+)$/ ) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 411 | 7 | ( $key, $inx ) = ( $1, $2 ); | |||||||||||||||||||||||||||||||||
| 412 | } | ||||||||||||||||||||||||||||||||||
| 413 | |||||||||||||||||||||||||||||||||||
| 414 | 395 | ▶ | my $newval = ref($m) eq 'CODE' ? $m->($key) : $m->{$key}; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 415 | 395 | ▶ | if ( defined $newval ) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 416 | 314 | $val = $newval; | |||||||||||||||||||||||||||||||||
| 417 | |||||||||||||||||||||||||||||||||||
| 418 | 314 | ▶ | if ( UNIVERSAL::isa( $val, 'ARRAY' ) ) { | ||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||
| 419 | # 1, 2, ... selects 1st, 2nd value; -1 counts from end. | ||||||||||||||||||||||||||||||||||
| 420 | 236 | ▶ | if ( $inx ) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 421 | 6 | ▶ | if ( $inx > 0 ) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 422 | 4 | ▶ | if ( $inx <= @$val ) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 423 | 3 | $val = $val->[$inx-1]; | |||||||||||||||||||||||||||||||||
| 424 | } | ||||||||||||||||||||||||||||||||||
| 425 | else { | ||||||||||||||||||||||||||||||||||
| 426 | 1 | $val = ""; | |||||||||||||||||||||||||||||||||
| 427 | } | ||||||||||||||||||||||||||||||||||
| 428 | } | ||||||||||||||||||||||||||||||||||
| 429 | else { | ||||||||||||||||||||||||||||||||||
| 430 | 2 | $val = $val->[$inx]; | |||||||||||||||||||||||||||||||||
| 431 | } | ||||||||||||||||||||||||||||||||||
| 432 | } | ||||||||||||||||||||||||||||||||||
| 433 | # Zero or none means concatenate all. | ||||||||||||||||||||||||||||||||||
| 434 | else { | ||||||||||||||||||||||||||||||||||
| 435 | 230 | ▶ | $val = join( $ctl->{separator} // $", @$val ); | ||||||||||||||||||||||||||||||||
|
Condition66%2/3
$ctl->{"separator"} // $"
MC/DC0%0/2
$ctl->{"separator"} // $"
$ctl->{"separator"}
$"
Truth table66%2/3
A // B
A= $ctl->{"separator"}B= $"
| |||||||||||||||||||||||||||||||||||
| 436 | } | ||||||||||||||||||||||||||||||||||
| 437 | } | ||||||||||||||||||||||||||||||||||
| 438 | elsif ( $inx ) { | ||||||||||||||||||||||||||||||||||
| 439 | 0 | Carp::croak("Expecting an array for variable '$key'") | |||||||||||||||||||||||||||||||||
| 440 | } | ||||||||||||||||||||||||||||||||||
| 441 | } | ||||||||||||||||||||||||||||||||||
| 442 | |||||||||||||||||||||||||||||||||||
| 443 | 395 | my $subst = ''; | |||||||||||||||||||||||||||||||||
| 444 | 395 | ▶ | for ( split( /(?<!\\):/, $i->{fmt}//'' ) ) { | ||||||||||||||||||||||||||||||||
|
Condition100%2/2
$i->{"fmt"} // ""
MC/DC100%1/1
$i->{"fmt"} // ""
$i->{"fmt"}
Truth table100%2/2
A
A= $i->{"fmt"}
| |||||||||||||||||||||||||||||||||||
| 445 | 165 | ▶ | last unless defined $newval; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 446 | 155 | ▶ | next unless my $fmt = $_; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 447 | |||||||||||||||||||||||||||||||||||
| 448 | # Simple formatters. | ||||||||||||||||||||||||||||||||||
| 449 | 155 | ▶ | if ( $fmt eq 'lc' ) { $val = lc($val) } | ||||||||||||||||||||||||||||||||
|
Branch: 7 branches
| |||||||||||||||||||||||||||||||||||
| 450 | 5 | elsif ( $fmt eq 'uc' ) { $val = uc($val) } | |||||||||||||||||||||||||||||||||
| 451 | 10 | elsif ( $fmt eq 'sc' ) { $val = ucfirst($val) } | |||||||||||||||||||||||||||||||||
| 452 | 10 | elsif ( $fmt eq 'ic' ) { $val = f_ic($val) } | |||||||||||||||||||||||||||||||||
| 453 | |||||||||||||||||||||||||||||||||||
| 454 | # Functions. | ||||||||||||||||||||||||||||||||||
| 455 | elsif ( $fmt =~ /^([lr])pad\((\d+)(?:,(.*?))?\)$/ ) { | ||||||||||||||||||||||||||||||||||
| 456 | 55 | $val = f_pad( $val, $1, $2, $3 ); | |||||||||||||||||||||||||||||||||
| 457 | } | ||||||||||||||||||||||||||||||||||
| 458 | |||||||||||||||||||||||||||||||||||
| 459 | elsif ( $fmt =~ /^replace\((.+?),(.*?)\)$/ ) { | ||||||||||||||||||||||||||||||||||
| 460 | 40 | $val = f_replace( $val, $1, $2 ); | |||||||||||||||||||||||||||||||||
| 461 | } | ||||||||||||||||||||||||||||||||||
| 462 | |||||||||||||||||||||||||||||||||||
| 463 | # Printf formatting. | ||||||||||||||||||||||||||||||||||
| 464 | 20 | elsif ( $fmt =~ /^%/ ) { $val = f_printf( $val, $fmt ) } | |||||||||||||||||||||||||||||||||
| 465 | |||||||||||||||||||||||||||||||||||
| 466 | 0 | else { Carp::croak("Invalid format code '$fmt'"); } | |||||||||||||||||||||||||||||||||
| 467 | } | ||||||||||||||||||||||||||||||||||
| 468 | 395 | ▶ | if ( $i->{op} ) { | ||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||
| 469 | 76 | ▶ | my $test = $i->{test} // ''; | ||||||||||||||||||||||||||||||||
|
Condition50%1/2
$i->{"test"} // ""
MC/DC0%0/1
$i->{"test"} // ""
$i->{"test"}
Truth table50%1/2
A
A= $i->{"test"}
| |||||||||||||||||||||||||||||||||||
| 470 | 76 | ▶ | if ( $i->{op} eq '=' && $val eq $test ) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$i->{"op"} eq "=" and $val eq $test
MC/DC50%1/2
$i->{"op"} eq "=" and $val eq $test
$i->{"op"} eq "="
$val eq $test
Truth table66%2/3
A and B
A= $i->{"op"} eq "="B= $val eq $test
| |||||||||||||||||||||||||||||||||||
| 471 | 40 | ▶ | $subst = $i->{then} // ''; | ||||||||||||||||||||||||||||||||
|
Condition100%2/2
$i->{"then"} // ""
MC/DC100%1/1
$i->{"then"} // ""
$i->{"then"}
Truth table100%2/2
A
A= $i->{"then"}
| |||||||||||||||||||||||||||||||||||
| 472 | } | ||||||||||||||||||||||||||||||||||
| 473 | else { | ||||||||||||||||||||||||||||||||||
| 474 | 36 | ▶ | $subst = $i->{else} // ''; | ||||||||||||||||||||||||||||||||
|
Condition100%2/2
$i->{"else"} // ""
MC/DC100%1/1
$i->{"else"} // ""
$i->{"else"}
Truth table100%2/2
A
A= $i->{"else"}
| |||||||||||||||||||||||||||||||||||
| 475 | } | ||||||||||||||||||||||||||||||||||
| 476 | } | ||||||||||||||||||||||||||||||||||
| 477 | elsif ( $val ne '' ) { | ||||||||||||||||||||||||||||||||||
| 478 | 254 | ▶ | $subst = $i->{then} // $val; | ||||||||||||||||||||||||||||||||
|
Condition66%2/3
$i->{"then"} // $val
MC/DC0%0/2
$i->{"then"} // $val
$i->{"then"}
$val
Truth table66%2/3
A // B
A= $i->{"then"}B= $val
| |||||||||||||||||||||||||||||||||||
| 479 | } | ||||||||||||||||||||||||||||||||||
| 480 | else { | ||||||||||||||||||||||||||||||||||
| 481 | 65 | ▶ | $subst = $i->{else} // ''; | ||||||||||||||||||||||||||||||||
|
Condition100%2/2
$i->{"else"} // ""
MC/DC100%1/1
$i->{"else"} // ""
$i->{"else"}
Truth table100%2/2
A
A= $i->{"else"}
| |||||||||||||||||||||||||||||||||||
| 482 | } | ||||||||||||||||||||||||||||||||||
| 483 | |||||||||||||||||||||||||||||||||||
| 484 | 395 | $subst =~ s/\x{fdde}/$val/g; | |||||||||||||||||||||||||||||||||
| 485 | 395 | return $subst; | |||||||||||||||||||||||||||||||||
| 486 | } | ||||||||||||||||||||||||||||||||||
| 487 | |||||||||||||||||||||||||||||||||||
| 488 | # Formatter functions. | ||||||||||||||||||||||||||||||||||
| 489 | # First arg = $val. | ||||||||||||||||||||||||||||||||||
| 490 | # Return new value. | ||||||||||||||||||||||||||||||||||
| 491 | |||||||||||||||||||||||||||||||||||
| 492 | 10 | ▶ | sub f_ic { | ||||||||||||||||||||||||||||||||
|
Subroutine
f_ic: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||
| 493 | 10 | my ( $val ) = @_; | |||||||||||||||||||||||||||||||||
| 494 | 10 | join('', map { ucfirst } (split( /(^|\s+|-)/, $val ))); | |||||||||||||||||||||||||||||||||
| 495 | } | ||||||||||||||||||||||||||||||||||
| 496 | |||||||||||||||||||||||||||||||||||
| 497 | 55 | ▶ | sub f_pad { | ||||||||||||||||||||||||||||||||
|
Subroutine
f_pad: called CC=4 SCAR=13.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||
| 498 | 55 | my ( $val, $lr, $len, $str ) = @_; | |||||||||||||||||||||||||||||||||
| 499 | 55 | ▶ | $str //= " "; | ||||||||||||||||||||||||||||||||
|
Condition100%2/2
$str //= " "
MC/DC100%1/1
$str //= " "
$str
Truth table100%2/2
A
A= $str
| |||||||||||||||||||||||||||||||||||
| 500 | 55 | ▶ | return $val unless ( my $need = $len - length($val) ) > 0; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 501 | 30 | my $pad = $str x (1+int(($len-1)/length($str))); | |||||||||||||||||||||||||||||||||
| 502 | 30 | ▶ | if ( $lr eq 'l' ) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 503 | 15 | return substr( $pad, 0, $need ) . $val; | |||||||||||||||||||||||||||||||||
| 504 | } | ||||||||||||||||||||||||||||||||||
| 505 | 15 | $val . substr( $pad, 0, $need ); | |||||||||||||||||||||||||||||||||
| 506 | } | ||||||||||||||||||||||||||||||||||
| 507 | |||||||||||||||||||||||||||||||||||
| 508 | 40 | ▶ | sub f_replace { | ||||||||||||||||||||||||||||||||
|
Subroutine
f_replace: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||
| 509 | 40 | my ( $val, $rep, $str ) = @_; | |||||||||||||||||||||||||||||||||
| 510 | 40 | $val =~ s/\Q$rep\E/$str/g; | |||||||||||||||||||||||||||||||||
| 511 | 40 | $val; | |||||||||||||||||||||||||||||||||
| 512 | } | ||||||||||||||||||||||||||||||||||
| 513 | |||||||||||||||||||||||||||||||||||
| 514 | 20 | ▶ | sub f_printf { | ||||||||||||||||||||||||||||||||
|
Subroutine
f_printf: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||
| 515 | 20 | 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 | ▶ | no warnings qw(numeric); | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 519 | 20 | 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<< <JV at CPAN dot org> >> | ||||||||||||||||||||||||||||||||||
| 529 | |||||||||||||||||||||||||||||||||||
| 530 | =head1 SUPPORT | ||||||||||||||||||||||||||||||||||
| 531 | |||||||||||||||||||||||||||||||||||
| 532 | Development of this module takes place on GitHub: | ||||||||||||||||||||||||||||||||||
| 533 | L<https://github.com/sciurius/perl-String-Interpolate-Named>. | ||||||||||||||||||||||||||||||||||
| 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 | 8 | 1; | |||||||||||||||||||||||||||||||||