Formatter.pm

filter: Statementstmt 94.0% 143 / 152 Branchbran 65.7% 25 / 38 Conditioncond 58.3% 14 / 24 MC/DCmcdc 20.0% 3 / 15 Subroutinesub 91.6% 33 / 36 Podpod 84.6% 11 / 13 totaltotal 82.3% 229 / 278 CC 35 SCAR 13.0
CC
35
Cov
85%
format_simply
9.2
forbid_input
6.0
new
5.1
CRAP 3.7
↑ Summary Formatter/Cookbook.pm »
1 5 ▶use v5.8.0;
Subroutine BEGIN: called CC=1 SCAR=0.0
2 5 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 5 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 package String::Formatter 1.235;
5 # ABSTRACT: build sprintf-like functions of your own
6  
7 #pod =head1 SYNOPSIS
8 #pod
9 #pod use String::Formatter stringf => {
10 #pod -as => 'str_rf',
11 #pod codes => {
12 #pod f => sub { $_ },
13 #pod b => sub { scalar reverse $_ },
14 #pod o => 'Okay?',
15 #pod },
16 #pod };
17 #pod
18 #pod print str_rf('This is %10f and this is %-15b, %o', 'forward', 'backward');
19 #pod
20 #pod ...prints...
21 #pod
22 #pod This is forward and this is drawkcab , okay?
23 #pod
24 #pod =head1 DESCRIPTION
25 #pod
26 #pod String::Formatter is a tool for building sprintf-like formatting routines.
27 #pod It supports named or positional formatting, custom conversions, fixed string
28 #pod interpolation, and simple width-matching out of the box. It is easy to alter
29 #pod its behavior to write new kinds of format string expanders. For most cases, it
30 #pod should be easy to build all sorts of formatters out of the options built into
31 #pod String::Formatter.
32 #pod
33 #pod Normally, String::Formatter will be used to import a sprintf-like routine
34 #pod referred to as "C<stringf>", but which can be given any name you like. This
35 #pod routine acts like sprintf in that it takes a string and some inputs and returns
36 #pod a new string:
37 #pod
38 #pod my $output = stringf "Some %a format %s for you to %u.\n", { ... };
39 #pod
40 #pod This routine is actually a wrapper around a String::Formatter object created by
41 #pod importing stringf. In the following code, the entire hashref after "stringf"
42 #pod is passed to String::Formatter's constructor (the C<new> method), save for the
43 #pod C<-as> key and any other keys that start with a dash.
44 #pod
45 #pod use String::Formatter
46 #pod stringf => {
47 #pod -as => 'fmt_time',
48 #pod codes => { ... },
49 #pod format_hunker => ...,
50 #pod input_processor => ...,
51 #pod },
52 #pod stringf => {
53 #pod -as => 'fmt_date',
54 #pod codes => { ... },
55 #pod string_replacer => ...,
56 #pod hunk_formatter => ...,
57 #pod },
58 #pod ;
59 #pod
60 #pod As you can see, this will generate two stringf routines, with different
61 #pod behaviors, which are installed with different names. Since the behavior of
62 #pod these routines is based on the C<format> method of a String::Formatter object,
63 #pod the rest of the documentation will describe the way the object behaves.
64 #pod
65 #pod There's also a C<named_stringf> export, which behaves just like the C<stringf>
66 #pod export, but defaults to the C<named_replace> and C<require_named_input>
67 #pod arguments. There's a C<method_stringf> export, which defaults
68 #pod C<method_replace> and C<require_single_input>. Finally, a C<indexed_stringf>,
69 #pod which defaults to C<indexed_replaced> and C<require_arrayref_input>. For more
70 #pod on these, keep reading, and check out the cookbook.
71 #pod
72 #pod L<String::Formatter::Cookbook> provides a number of recipes for ways to put
73 #pod String::Formatter to use.
74 #pod
75 #pod =head1 FORMAT STRINGS
76 #pod
77 #pod Format strings are generally assumed to look like Perl's sprintf's format
78 #pod strings:
79 #pod
80 #pod There's a bunch of normal strings and then %s format %1.4c with %% signs.
81 #pod
82 #pod The exact semantics of the format codes are not totally settled yet -- and they
83 #pod can be replaced on a per-formatter basis. Right now, they're mostly a subset
84 #pod of Perl's astonishingly large and complex system. That subset looks like this:
85 #pod
86 #pod % - a percent sign to begin the format
87 #pod ... - (optional) various modifiers to the format like "-5" or "#" or "2$"
88 #pod {..} - (optional) a string inside braces
89 #pod s - a short string (usually one character) identifying the conversion
90 #pod
91 #pod Not all format modifiers found in Perl's C<sprintf> are yet supported.
92 #pod Currently the only format modifiers must match:
93 #pod
94 #pod (-)? # left-align, rather than right
95 #pod (\d*)? # (optional) minimum field width
96 #pod (?:\.(\d*))? # (optional) maximum field width
97 #pod
98 #pod Some additional format semantics may be added, but probably nothing exotic.
99 #pod Even things like C<2$> and C<*> are probably not going to appear in
100 #pod String::Formatter's default behavior.
101 #pod
102 #pod Another subtle difference, introduced intentionally, is in the handling of
103 #pod C<%%>. With the default String::Formatter behavior, string C<%%> is not
104 #pod interpreted as a formatting code. This is different from the behavior of
105 #pod Perl's C<sprintf>, which interprets it as a special formatting character that
106 #pod doesn't consume input and always acts like the fixed string C<%>. The upshot
107 #pod of this is:
108 #pod
109 #pod sprintf "%%"; # ==> returns "%"
110 #pod stringf "%%"; # ==> returns "%%"
111 #pod
112 #pod sprintf "%10%"; # ==> returns " %"
113 #pod stringf "%10%"; # ==> dies: unknown format code %
114 #pod
115 #pod =cut
116  
117 5 ▶use Params::Util ();
Subroutine BEGIN: called CC=1 SCAR=0.0
118 5 ▶use Sub::Exporter -setup => {
Subroutine BEGIN: called CC=1 SCAR=0.1
119 exports => {
120 2 ▶ stringf => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
121 2 my ($class, $name, $arg, $col) = @_;
122 2 my $formatter = $class->new($arg);
123 2 ▶ return sub { $formatter->format(@_) };
Subroutine __ANON__: called CC=1 SCAR=0.0
124 },
125 0 ▶ method_stringf => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
126 0 my ($class, $name, $arg, $col) = @_;
127 0 my $formatter = $class->new({
128 input_processor => 'require_single_input',
129 string_replacer => 'method_replace',
130 %$arg,
131 });
132 0 ▶ return sub { $formatter->format(@_) };
Subroutine __ANON__: not called CC=1 SCAR=6.9
133 },
134 1 ▶ named_stringf => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
135 1 my ($class, $name, $arg, $col) = @_;
136 1 my $formatter = $class->new({
137 input_processor => 'require_named_input',
138 string_replacer => 'named_replace',
139 %$arg,
140 });
141 1 ▶ return sub { $formatter->format(@_) };
Subroutine __ANON__: called CC=1 SCAR=0.0
142 },
143 1 ▶ indexed_stringf => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
144 1 my ($class, $name, $arg, $col) = @_;
145 1 my $formatter = $class->new({
146 input_processor => 'require_arrayref_input',
147 string_replacer => 'indexed_replace',
148 %$arg,
149 });
150 1 ▶ return sub { $formatter->format(@_) };
Subroutine __ANON__: called CC=1 SCAR=0.0
151 },
152 },
153 5 };
154  
155 5 my %METHODS;
156 5 ▶BEGIN {
Subroutine BEGIN: called CC=2 SCAR=6.9
157 5 %METHODS = (
158 format_hunker => 'hunk_simply',
159 input_processor => 'return_input',
160 string_replacer => 'positional_replace',
161 hunk_formatter => 'format_simply',
162 );
163  
164 5 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
165 5 for my $method (keys %METHODS) {
166 20 ▶ *$method = sub { $_[0]->{ $method } };
Subroutine __ANON__: called CC=1 SCAR=0.0
167  
168 20 my $default = "default_$method";
169 20 ▶ *$default = sub { $METHODS{ $method } };
Subroutine __ANON__: called CC=1 SCAR=0.0
170 }
171 }
172  
173 #pod =method new
174 #pod
175 #pod my $formatter = String::Formatter->new({
176 #pod codes => { ... },
177 #pod format_hunker => ...,
178 #pod input_processor => ...,
179 #pod string_replacer => ...,
180 #pod hunk_formatter => ...,
181 #pod });
182 #pod
183 #pod This returns a new formatter. The C<codes> argument contains the formatting
184 #pod codes for the formatter in the form:
185 #pod
186 #pod codes => {
187 #pod s => 'fixed string',
188 #pod S => 'different string',
189 #pod c => sub { ... },
190 #pod }
191 #pod
192 #pod Code values (or "conversions") should either be strings or coderefs. This
193 #pod hashref can be accessed later with the C<codes> method.
194 #pod
195 #pod The other four arguments change how the formatting occurs. Formatting happens
196 #pod in five phases:
197 #pod
198 #pod =for :list
199 #pod 1. format_hunker - format string is broken down into fixed and %-code hunks
200 #pod 2. input_processor - the other inputs are validated and processed
201 #pod 3. string_replacer - replacement strings are generated by using conversions
202 #pod 4. hunk_formatter - replacement strings in hunks are formatted
203 #pod 5. all hunks, now strings, are recombined; this phase is just C<join>
204 #pod
205 #pod The defaults are found by calling C<default_WHATEVER> for each helper that
206 #pod isn't given. Values must be either strings (which are interpreted as method
207 #pod names) or coderefs. The semantics for each method are described in the
208 #pod methods' sections, below.
209 #pod
210 #pod =cut
211  
212 11 ▶sub default_codes {
Subroutine default_codes: called CC=1 SCAR=0.0
Pod undocumented
213 11 return {};
214 }
215  
216 11 ▶sub new {
Subroutine new: called CC=5 SCAR=16.3
Pod documented
217 11 my ($class, $arg) = @_;
218  
219 my $_codes = {
220 11 %{ $class->default_codes },
221 11 ▶ %{ $arg->{codes} || {} },
Condition50%1/2
$arg->{'codes'} || {}
l!l
110
MC/DC0%0/1
$arg->{'codes'} || {}
$arg->{'codes'}
Truth table50%1/2
A
A= $arg->{'codes'}
Aresult
11
00
222 };
223  
224 11 my $self = bless { codes => $_codes } => $class;
225  
226 11 for (keys %METHODS) {
227 44 ▶ $self->{ $_ } = $arg->{ $_ } || do {
Condition66%2/3
$arg->{$_} || do { my $default_method = "default_$_"; $class->$default_method }
l!l&&r!l&&!r
14300
MC/DC0%0/2
$arg->{$_} || do { my $default_method = "default_$_"; $class->$default_method }
$arg->{$_} do { my $default_method = "default_$_"; $class->$default_method }
Truth table66%2/3
A || B
A= $arg->{$_}B= do { my $default_method = "default_$_"; $class->$default_method }
ABresult
1X1
011
000
228 30 my $default_method = "default_$_";
229 30 $class->$default_method;
230 };
231  
232 44 ▶ $self->{$_} = $self->can($self->{$_}) unless ref $self->{$_};
Branch: 1 branch
BranchTrueFalseTotal
unless ref $self->{$_} 44 0 44
233 }
234  
235 11 my $codes = $self->codes;
236  
237 11 return $self;
238 }
239  
240 32 ▶sub codes { $_[0]->{codes} }
Subroutine codes: called CC=1 SCAR=0.0
Pod undocumented
241  
242 #pod =method format
243 #pod
244 #pod my $result = $formatter->format( $format_string, @input );
245 #pod
246 #pod print $formatter->format("My %h is full of %e.\n", 'hovercraft', 'eels');
247 #pod
248 #pod This does the actual formatting, calling the methods described above, under
249 #pod C<L</new>> and returning the result.
250 #pod
251 #pod =cut
252  
253 21 ▶sub format {
Subroutine format: called CC=4 SCAR=13.9
Pod documented
254 21 my $self = shift;
255 21 my $format = shift;
256  
257 21 ▶ Carp::croak("not enough arguments for stringf-based format")
Branch: 1 branch
BranchTrueFalseTotal
unless defined $format 0 21 21
258 unless defined $format;
259  
260 21 my $hunker = $self->format_hunker;
261 21 my $hunks = $self->$hunker($format);
262  
263 21 my $processor = $self->input_processor;
264 21 my $input = $self->$processor([ @_ ]);
265  
266 21 my $replacer = $self->string_replacer;
267 21 $self->$replacer($hunks, $input);
268  
269 20 my $formatter = $self->hunk_formatter;
270 20 ▶ ref($_) and $_ = $self->$formatter($_) for @$hunks;
Branch: 1 branch
BranchTrueFalseTotal
ref $_ and $_ = $self->$formatter($_) 34 52 86
271  
272 20 my $string = join q{}, @$hunks;
273  
274 20 return $string;
275 }
276  
277 #pod =method format_hunker
278 #pod
279 #pod Format hunkers are passed strings and return arrayrefs containing strings (for
280 #pod fixed content) and hashrefs (for formatting code sections).
281 #pod
282 #pod The hashref hunks should contain at least two entries: C<conversion> for the
283 #pod conversion code (the s, d, or u in %s, %d, or %u); and C<literal> for the
284 #pod complete original text of the hunk. For example, a bare minimum hunker should
285 #pod turn the following:
286 #pod
287 #pod I would like to buy %d %s today.
288 #pod
289 #pod ...into...
290 #pod
291 #pod [
292 #pod 'I would like to buy ',
293 #pod { conversion => 'd', literal => '%d' },
294 #pod ' ',
295 #pod { conversion => 's', literal => '%d' },
296 #pod ' today.',
297 #pod ]
298 #pod
299 #pod Another common entry is C<argument>. In the format strings expected by
300 #pod C<hunk_simply>, for example, these are free strings inside of curly braces.
301 #pod These are used extensively other existing helpers for things liked accessing
302 #pod named arguments or providing method names.
303 #pod
304 #pod =method hunk_simply
305 #pod
306 #pod This is the default format hunker. It implements the format string semantics
307 #pod L<described above|/FORMAT STRINGS>.
308 #pod
309 #pod This hunker will produce C<argument> and C<conversion> and C<literal>. Its
310 #pod other entries are not yet well-defined for public consumption.
311 #pod
312 #pod =cut
313  
314 5 my $regex = qr/
315 (% # leading '%'
316 (-)? # left-align, rather than right
317 ([0-9]+)? # (optional) minimum field width
318 (?:\.([0-9]*))? # (optional) maximum field width
319 (?:{(.*?)})? # (optional) stuff inside
320 (\S) # actual format character
321 )
322 /x;
323  
324 21 ▶sub hunk_simply {
Subroutine hunk_simply: called CC=4 SCAR=13.9
Pod documented
325 21 my ($self, $string) = @_;
326  
327 21 my @to_fmt;
328 21 my $pos = 0;
329  
330 21 ▶ while ($string =~ m{\G(.*?)$regex}gs) {
Branch: 1 branch
BranchTrueFalseTotal
if ($string =~ /\G(.*?)$regex/gs) 36 21 57
331 36 push @to_fmt, $1, {
332 alignment => $3,
333 min_width => $4,
334 max_width => $5,
335  
336 literal => $2,
337 argument => $6,
338 conversion => $7,
339 };
340  
341 36 ▶ $to_fmt[-1] = '%' if $to_fmt[-1]{literal} eq '%%';
Branch: 1 branch
BranchTrueFalseTotal
if $to_fmt[-1]{"literal"} eq "%%" 1 35 36
342  
343 36 $pos = pos $string;
344 }
345  
346 21 ▶ push @to_fmt, substr $string, $pos if $pos < length $string;
Branch: 1 branch
BranchTrueFalseTotal
if $pos < length $string 17 4 21
347  
348 21 return \@to_fmt;
349 }
350  
351 #pod =method input_processor
352 #pod
353 #pod The input processor is responsible for inspecting the post-format-string
354 #pod arguments, validating them, and returning them in a possibly-transformed form.
355 #pod The processor is passed an arrayref containing the arguments and should return
356 #pod a scalar value to be used as the input going forward.
357 #pod
358 #pod =method return_input
359 #pod
360 #pod This input processor, the default, simply returns the input it was given with
361 #pod no validation or transformation.
362 #pod
363 #pod =cut
364  
365 14 ▶sub return_input {
Subroutine return_input: called CC=1 SCAR=0.0
Pod documented
366 14 return $_[1];
367 }
368  
369 #pod =method require_named_input
370 #pod
371 #pod This input processor will raise an exception unless there is exactly one
372 #pod post-format-string argument to the format call, and unless that argument is a
373 #pod hashref. It will also replace the arrayref with the given hashref so
374 #pod subsequent phases of the format can avoid lots of needless array dereferencing.
375 #pod
376 #pod =cut
377  
378 3 ▶sub require_named_input {
Subroutine require_named_input: called CC=3 SCAR=12.5
Pod documented
379 3 my ($self, $args) = @_;
380  
381 3 ▶ Carp::croak("routine must be called with exactly one hashref arg")
Branch: 1 branch
BranchTrueFalseTotal
if @$args != 1 or not &Params::Util::_HASHLIKE($args->[0]) 0 3 3
Condition33%1/3
@$args != 1 or not &Params::Util::_HASHLIKE($args->[0])
l!l&&r!l&&!r
003
MC/DC0%0/2
@$args != 1 or not &Params::Util::_HASHLIKE($args->[0])
@$args != 1 not &Params::Util::_HASHLIKE($args->[0])
Truth table33%1/3
A or not(B)
A= @$args != 1B= not &Params::Util::_HASHLIKE($args->[0])
ABresult
1X1
011
000
382 if @$args != 1 or ! Params::Util::_HASHLIKE($args->[0]);
383  
384 3 return $args->[0];
385 }
386  
387 #pod =method require_arrayref_input
388 #pod
389 #pod This input processor will raise an exception unless there is exactly one
390 #pod post-format-string argument to the format call, and unless that argument is a
391 #pod arrayref. It will also replace the input with that single arrayref it found so
392 #pod subsequent phases of the format can avoid lots of needless array dereferencing.
393 #pod
394 #pod =cut
395  
396 1 ▶sub require_arrayref_input {
Subroutine require_arrayref_input: called CC=3 SCAR=12.5
Pod documented
397 1 my ($self, $args) = @_;
398  
399 1 ▶ Carp::croak("routine must be called with exactly one arrayref arg")
Branch: 1 branch
BranchTrueFalseTotal
if @$args != 1 or not &Params::Util::_ARRAYLIKE($args->[0]) 0 1 1
Condition33%1/3
@$args != 1 or not &Params::Util::_ARRAYLIKE($args->[0])
l!l&&r!l&&!r
001
MC/DC0%0/2
@$args != 1 or not &Params::Util::_ARRAYLIKE($args->[0])
@$args != 1 not &Params::Util::_ARRAYLIKE($args->[0])
Truth table33%1/3
A or not(B)
A= @$args != 1B= not &Params::Util::_ARRAYLIKE($args->[0])
ABresult
1X1
011
000
400 if @$args != 1 or ! Params::Util::_ARRAYLIKE($args->[0]);
401  
402 1 return $args->[0];
403 }
404  
405 #pod =method require_single_input
406 #pod
407 #pod This input processor will raise an exception if more than one input is given.
408 #pod After input processing, the single element in the input will be used as the
409 #pod input itself.
410 #pod
411 #pod =cut
412  
413 3 ▶sub require_single_input {
Subroutine require_single_input: called CC=2 SCAR=7.1
Pod documented
414 3 my ($self, $args) = @_;
415  
416 3 ▶ Carp::croak("routine must be called with exactly one argument after string")
Branch: 1 branch
BranchTrueFalseTotal
if @$args != 1 0 3 3
417 if @$args != 1;
418  
419 3 return $args->[0];
420 }
421  
422 #pod =method forbid_input
423 #pod
424 #pod This input processor will raise an exception if any input is given. In other
425 #pod words, formatters with this input processor accept format strings and nothing
426 #pod else.
427 #pod
428 #pod =cut
429  
430 0 ▶sub forbid_input {
Subroutine forbid_input: not called CC=2 SCAR=17.9
Pod documented
431 0 my ($self, $args) = @_;
432  
433 0 ▶ Carp::croak("routine must be called with no arguments after format string")
Branch: 1 branch
BranchTrueFalseTotal
if @$args 0 0 0
434 if @$args;
435  
436 0 return $args;
437 }
438  
439 #pod =method string_replacer
440 #pod
441 #pod The string_replacer phase is responsible for adding a C<replacement> entry to
442 #pod format code hunks. This should be a string-value entry that will be formatted
443 #pod and concatenated into the output string. String replacers can also replace the
444 #pod whole hunk with a string to avoid any subsequent formatting.
445 #pod
446 #pod =method positional_replace
447 #pod
448 #pod This replacer matches inputs to the hunk's position in the format string. This
449 #pod is the default replacer, used in the L<synopsis, above|/SYNOPSIS>, which should
450 #pod make its behavior clear. At present, fixed-string conversions B<do not> affect
451 #pod the position of arg matched, meaning that given the following:
452 #pod
453 #pod my $formatter = String::Formatter->new({
454 #pod codes => {
455 #pod f => 'fixed string',
456 #pod s => sub { ... },
457 #pod }
458 #pod });
459 #pod
460 #pod $formatter->format("%s %f %s", 1, 2);
461 #pod
462 #pod The subroutine is called twice, once for the input C<1> and once for the input
463 #pod C<2>. B<This behavior may change> after some more experimental use.
464 #pod
465 #pod =method named_replace
466 #pod
467 #pod This replacer should be used with the C<require_named_input> input processor.
468 #pod It expects the input to be a hashref and it finds values to be interpolated by
469 #pod looking in the hashref for the brace-enclosed name on each format code. Here's
470 #pod an example use:
471 #pod
472 #pod $formatter->format("This was the %{adj}s day in %{num}d weeks.", {
473 #pod adj => 'best',
474 #pod num => 6,
475 #pod });
476 #pod
477 #pod =method indexed_replace
478 #pod
479 #pod This replacer should be used with the C<require_arrayref_input> input
480 #pod processor. It expects the input to be an arrayref and it finds values to be
481 #pod interpolated by looking in the arrayref for the brace-enclosed index on each
482 #pod format code. Here's an example use:
483 #pod
484 #pod $formatter->format("This was the %{1}s day in %{0}d weeks.", [ 6, 'best' ]);
485 #pod
486 #pod =cut
487  
488 15 ▶sub __closure_replace {
Subroutine __closure_replace: called CC=1 SCAR=0.0
489 15 my ($closure) = @_;
490  
491 18 ▶ return sub {
Subroutine __ANON__: called CC=4 SCAR=13.9
492 18 my ($self, $hunks, $input) = @_;
493  
494 18 my $heap = {};
495 18 my $code = $self->codes;
496  
497 18 for my $i (grep { ref $hunks->[$_] } 0 .. $#$hunks) {
498 28 my $hunk = $hunks->[ $i ];
499 28 my $conv = $code->{ $hunk->{conversion} };
500  
501 28 ▶ Carp::croak("Unknown conversion in stringf: $hunk->{conversion}")
Branch: 1 branch
BranchTrueFalseTotal
unless defined $conv 1 27 28
502 unless defined $conv;
503  
504 27 ▶ if (ref $conv) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $conv) { } 13 14 27
505 13 $hunks->[ $i ]->{replacement} = $self->$closure({
506 conv => $conv,
507 hunk => $hunk,
508 heap => $heap,
509 input => $input,
510 });
511 } else {
512 14 $hunks->[ $i ]->{replacement} = $conv;
513 }
514 }
515 15 };
516 }
517  
518 # $self->$string_replacer($hunks, $input);
519 5 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
520 5 ▶ *positional_replace = __closure_replace(sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
521 5 my ($self, $arg) = @_;
522 5 local $_ = $arg->{input}->[ $arg->{heap}{nth}++ ];
523 5 return $arg->{conv}->($self, $_, $arg->{hunk}{argument});
524 5 });
525  
526 6 ▶ *named_replace = __closure_replace(sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
527 6 my ($self, $arg) = @_;
528 6 local $_ = $arg->{input}->{ $arg->{hunk}{argument} };
529 6 return $arg->{conv}->($self, $_, $arg->{hunk}{argument});
530 5 });
531  
532 2 ▶ *indexed_replace = __closure_replace(sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
533 2 my ($self, $arg) = @_;
534 2 local $_ = $arg->{input}->[ $arg->{hunk}{argument} ];
535 2 return $arg->{conv}->($self, $_, $arg->{hunk}{argument});
536 5 });
537 }
538  
539 #pod =method method_replace
540 #pod
541 #pod This string replacer method expects the input to be a single value on which
542 #pod methods can be called. If a value was given in braces to the format code, it
543 #pod is passed as an argument.
544 #pod
545 #pod =cut
546  
547 # should totally be rewritten with commonality with keyed_replace factored out
548 2 ▶sub method_replace {
Subroutine method_replace: called CC=5 SCAR=16.1
Pod documented
549 2 my ($self, $hunks, $input) = @_;
550  
551 2 my $heap = {};
552 2 my $code = $self->codes;
553  
554 2 for my $i (grep { ref $hunks->[$_] } 0 .. $#$hunks) {
555 5 my $hunk = $hunks->[ $i ];
556 5 my $conv = $code->{ $hunk->{conversion} };
557  
558 5 ▶ Carp::croak("Unknown conversion in stringf: $hunk->{conversion}")
Branch: 1 branch
BranchTrueFalseTotal
unless defined $conv 0 5 5
559 unless defined $conv;
560  
561 5 ▶ if (ref $conv) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $conv) { } 3 2 5
562 3 local $_ = $input;
563 3 $hunks->[ $i ]->{replacement} = $input->$conv($hunk->{argument});
564 } else {
565 2 local $_ = $input;
566 $hunks->[ $i ]->{replacement} = $input->$conv(
567 defined $hunk->{argument} ? $hunk->{argument} : ()
568 2 ▶ );
Branch: 1 branch
BranchTrueFalseTotal
defined $hunk->{'argument'} ? : 1 1 2
569 }
570 }
571 }
572  
573 #pod =method keyed_replace
574 #pod
575 #pod This string replacer method expects the input to be a single hashref. Coderef
576 #pod code values are used as callbacks, but strings are used as hash keys. If a
577 #pod value was given in braces to the format code, it is ignored.
578 #pod
579 #pod For example if the codes contain C<< i => 'ident' >> then C<%i> in the format
580 #pod string will be replaced with C<< $input->{ident} >> in the output.
581 #pod
582 #pod =cut
583  
584 # should totally be rewritten with commonality with method_replace factored out
585 1 ▶sub keyed_replace {
Subroutine keyed_replace: called CC=4 SCAR=14.4
Pod documented
586 1 my ($self, $hunks, $input) = @_;
587  
588 1 my $heap = {};
589 1 my $code = $self->codes;
590  
591 1 for my $i (grep { ref $hunks->[$_] } 0 .. $#$hunks) {
592 2 my $hunk = $hunks->[ $i ];
593 2 my $conv = $code->{ $hunk->{conversion} };
594  
595 2 ▶ Carp::croak("Unknown conversion in stringf: $hunk->{conversion}")
Branch: 1 branch
BranchTrueFalseTotal
unless defined $conv 0 2 2
596 unless defined $conv;
597  
598 2 ▶ if (ref $conv) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $conv) { } 0 2 2
599 0 local $_ = $input;
600 0 $hunks->[ $i ]->{replacement} = $input->$conv($hunk->{argument});
601 } else {
602 2 local $_ = $input;
603 2 $hunks->[ $i ]->{replacement} = $input->{$conv};
604 }
605 }
606 }
607  
608 #pod =method hunk_formatter
609 #pod
610 #pod The hunk_formatter processes each the hashref hunks left after string
611 #pod replacement and returns a string. When it is called, it is passed a hunk
612 #pod hashref and must return a string.
613 #pod
614 #pod =method format_simply
615 #pod
616 #pod This is the default hunk formatter. It deals with minimum and maximum width
617 #pod cues as well as left and right alignment. Beyond that, it does no formatting
618 #pod of the replacement string.
619 #pod
620 #pod =cut
621  
622 34 ▶sub format_simply {
Subroutine format_simply: called CC=8 SCAR=22.2
Pod documented
623 34 my ($self, $hunk) = @_;
624  
625 34 my $replacement = $hunk->{replacement};
626 34 my $replength = length $replacement;
627  
628 34 ▶ my $alignment = $hunk->{alignment} || '';
Condition100%2/2
$hunk->{'alignment'} || ''
l!l
133
MC/DC100%1/1
$hunk->{'alignment'} || ''
$hunk->{'alignment'}
Truth table100%2/2
A
A= $hunk->{'alignment'}
Aresult
11
00
629 34 ▶ my $min_width = $hunk->{min_width} || 0;
Condition100%2/2
$hunk->{'min_width'} || 0
l!l
331
MC/DC100%1/1
$hunk->{'min_width'} || 0
$hunk->{'min_width'}
Truth table100%2/2
A
A= $hunk->{'min_width'}
Aresult
11
00
630 34 ▶ my $max_width = $hunk->{max_width} || $replength;
Condition66%2/3
$hunk->{'max_width'} || $replength
l!l&&r!l&&!r
1330
MC/DC0%0/2
$hunk->{'max_width'} || $replength
$hunk->{'max_width'} $replength
Truth table66%2/3
A || B
A= $hunk->{'max_width'}B= $replength
ABresult
1X1
011
000
631  
632 34 ▶ $min_width ||= $replength > $min_width ? $min_width : $replength;
Branch: 1 branch
BranchTrueFalseTotal
$replength > $min_width ? : 31 0 31
Condition66%2/3
$min_width ||= $replength > $min_width ? $min_width : $replength
l!l&&r!l&&!r
3031
MC/DC50%1/2
$min_width ||= $replength > $min_width ? $min_width : $replength
$min_width $replength > $min_width ? $min_width : $replength
Truth table66%2/3
A ||= B
A= $min_widthB= $replength > $min_width ? $min_width : $replength
ABresult
1X1
011
000
633 34 ▶ $max_width ||= $max_width > $replength ? $max_width : $replength;
Branch: 1 branch
BranchTrueFalseTotal
$max_width > $replength ? : 0 0 0
Condition33%1/3
$max_width ||= $max_width > $replength ? $max_width : $replength
l!l&&r!l&&!r
3400
MC/DC0%0/2
$max_width ||= $max_width > $replength ? $max_width : $replength
$max_width $max_width > $replength ? $max_width : $replength
Truth table33%1/3
A ||= B
A= $max_widthB= $max_width > $replength ? $max_width : $replength
ABresult
1X1
011
000
634  
635 34 return sprintf "%$alignment${min_width}.${max_width}s", $replacement;
636 }
637  
638 5 1;
639  
640 #pod =begin :postlude
641 #pod
642 #pod =head1 HISTORY
643 #pod
644 #pod String::Formatter is based on L<String::Format|String::Format>, written by
645 #pod Darren Chamberlain. For a history of the code, check the project's source code
646 #pod repository. All bugs should be reported to Ricardo Signes and
647 #pod String::Formatter. Very little of the original code remains.
648 #pod
649 #pod =end :postlude
650 #pod
651 #pod =for Pod::Coverage
652 #pod codes
653 #pod default_format_hunker
654 #pod default_input_processor
655 #pod default_string_replacer
656 #pod default_hunk_formatter
657 #pod
658  
659 __END__
↑ Summary Formatter/Cookbook.pm »