File: Cookbook.pm

package info (click to toggle)
libstring-formatter-perl 1.235-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 224 kB
  • sloc: perl: 658; makefile: 7
file content (359 lines) | stat: -rw-r--r-- 9,791 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
use strict;
use warnings;
package String::Formatter::Cookbook 1.235;
# ABSTRACT: ways to put String::Formatter to use
1;

#pod =encoding utf-8
#pod
#pod =head1 OVERVIEW
#pod
#pod String::Formatter is a pretty simple system for building formatting routines,
#pod but it can be hard to get started without an idea of the sort of things that
#pod are possible.
#pod
#pod =head1 BASIC RECIPES
#pod
#pod =head2 constants only
#pod
#pod The simplest stringf interface you can provide is one that just formats
#pod constant strings, allowing the user to put them inside other fixed strings with
#pod alignment:
#pod
#pod   use String::Formatter stringf => {
#pod     input_processor => 'forbid_input',
#pod     codes => {
#pod       a => 'apples',
#pod       b => 'bananas',
#pod       w => 'watermelon',
#pod     },
#pod   };
#pod
#pod   print stringf('I eat %a and %b but never %w.');
#pod
#pod   # Output:
#pod   # I eat apples and bananas but never watermelon.
#pod
#pod If the user tries to parameterize the string by passing arguments after the
#pod format string, an exception will be raised.
#pod
#pod =head2 sprintf-like conversions
#pod
#pod Another common pattern is to create a routine that behaves like Perl's
#pod C<sprintf>, but with a different set of conversion routines.  (It will also
#pod almost certainly have much simpler semantics than Perl's wildly complex
#pod behavior.)
#pod
#pod   use String::Formatter stringf => {
#pod     codes => {
#pod       s => sub { $_ },     # string itself
#pod       l => sub { length }, # length of input string
#pod       e => sub { /[^\x00-\x7F]/ ? '8bit' : '7bit' }, # ascii-safeness
#pod     },
#pod   };
#pod
#pod   print stringf(
#pod     "My name is %s.  I am about %l feet tall.  I use an %e alphabet.\n",
#pod     'Ricardo',
#pod     'ffffff',
#pod     'abcchdefghijklllmnñopqrrrstuvwxyz',
#pod   );
#pod
#pod   # Output:
#pod   # My name is Ricardo.  I am about 6 feet tall.  I use an 8bit alphabet.
#pod
#pod B<Warning>: The behavior of positional string replacement when the conversion
#pod codes mix constant strings and code references is currently poorly nailed-down.
#pod Do not rely on it yet.
#pod
#pod =head2 named conversions
#pod
#pod This recipe acts a bit like Python's format operator when given a dictionary.
#pod Rather than matching format code position with input ordering, inputs can be
#pod chosen by name.
#pod
#pod   use String::Formatter stringf => {
#pod     input_processor => 'require_named_input',
#pod     string_replacer => 'named_replace',
#pod
#pod     codes => {
#pod       s => sub { $_ },     # string itself
#pod       l => sub { length }, # length of input string
#pod       e => sub { /[^\x00-\x7F]/ ? '8bit' : '7bit' }, # ascii-safeness
#pod     },
#pod   };
#pod
#pod   print stringf(
#pod     "My %{which}s name is %{name}s.  My name is %{name}l letters long.",
#pod     {
#pod       which => 'first',
#pod       name  => 'Marvin',
#pod     },
#pod   );
#pod
#pod   # Output:
#pod   # My first name is Marvin.  My name is 6 letters long.
#pod
#pod Because this is a useful recipe, there is a shorthand for it:
#pod
#pod   use String::Formatter named_stringf => {
#pod     codes => {
#pod       s => sub { $_ },     # string itself
#pod       l => sub { length }, # length of input string
#pod       e => sub { /[^\x00-\x7F]/ ? '8bit' : '7bit' }, # ascii-safeness
#pod     },
#pod   };
#pod
#pod =head2 method calls
#pod
#pod Some objects provide methods to stringify them flexibly.  For example, many
#pod objects that represent timestamps allow you to call C<strftime> or something
#pod similar.  The C<method_replace> string replacer comes in handy here:
#pod
#pod   use String::Formatter stringf => {
#pod     input_processor => 'require_single_input',
#pod     string_replacer => 'method_replace',
#pod
#pod     codes => {
#pod       f => 'strftime',
#pod       c => 'format_cldr',
#pod       s => sub { "$_[0]" },
#pod     },
#pod   };
#pod
#pod   print stringf(
#pod     "%{%Y-%m-%d}f is also %{yyyy-MM-dd}c.  Default string is %s.",
#pod     DateTime->now,
#pod   );
#pod
#pod   # Output:
#pod   # 2009-11-17 is also 2009-11-17.  Default string is 2009-11-17T15:35:11.
#pod
#pod This recipe is available as the export C<method_stringf>:
#pod
#pod   use String::Formatter method_stringf => {
#pod     codes => {
#pod       f => 'strftime',
#pod       c => 'format_cldr',
#pod       s => sub { "$_[0]" },
#pod     },
#pod   };
#pod
#pod You can easily use this to implement an actual stringf-like method:
#pod
#pod   package MyClass;
#pod
#pod   use String::Formatter method_stringf => {
#pod     -as => '_stringf',
#pod     codes => {
#pod       f => 'strftime',
#pod       c => 'format_cldr',
#pod       s => sub { "$_[0]" },
#pod     },
#pod   };
#pod
#pod   sub format {
#pod     my ($self, $format) = @_;
#pod     return _stringf($format, $self);
#pod   }
#pod
#pod =cut

__END__

=pod

=encoding utf-8

=head1 NAME

String::Formatter::Cookbook - ways to put String::Formatter to use

=head1 VERSION

version 1.235

=head1 OVERVIEW

String::Formatter is a pretty simple system for building formatting routines,
but it can be hard to get started without an idea of the sort of things that
are possible.

=head1 PERL VERSION

This library should run on perls released even a long time ago.  It should work
on any version of perl released in the last five years.

Although it may work on older versions of perl, no guarantee is made that the
minimum required version will not be increased.  The version may be increased
for any reason, and there is no promise that patches will be accepted to lower
the minimum required perl.

=head1 BASIC RECIPES

=head2 constants only

The simplest stringf interface you can provide is one that just formats
constant strings, allowing the user to put them inside other fixed strings with
alignment:

  use String::Formatter stringf => {
    input_processor => 'forbid_input',
    codes => {
      a => 'apples',
      b => 'bananas',
      w => 'watermelon',
    },
  };

  print stringf('I eat %a and %b but never %w.');

  # Output:
  # I eat apples and bananas but never watermelon.

If the user tries to parameterize the string by passing arguments after the
format string, an exception will be raised.

=head2 sprintf-like conversions

Another common pattern is to create a routine that behaves like Perl's
C<sprintf>, but with a different set of conversion routines.  (It will also
almost certainly have much simpler semantics than Perl's wildly complex
behavior.)

  use String::Formatter stringf => {
    codes => {
      s => sub { $_ },     # string itself
      l => sub { length }, # length of input string
      e => sub { /[^\x00-\x7F]/ ? '8bit' : '7bit' }, # ascii-safeness
    },
  };

  print stringf(
    "My name is %s.  I am about %l feet tall.  I use an %e alphabet.\n",
    'Ricardo',
    'ffffff',
    'abcchdefghijklllmnñopqrrrstuvwxyz',
  );

  # Output:
  # My name is Ricardo.  I am about 6 feet tall.  I use an 8bit alphabet.

B<Warning>: The behavior of positional string replacement when the conversion
codes mix constant strings and code references is currently poorly nailed-down.
Do not rely on it yet.

=head2 named conversions

This recipe acts a bit like Python's format operator when given a dictionary.
Rather than matching format code position with input ordering, inputs can be
chosen by name.

  use String::Formatter stringf => {
    input_processor => 'require_named_input',
    string_replacer => 'named_replace',

    codes => {
      s => sub { $_ },     # string itself
      l => sub { length }, # length of input string
      e => sub { /[^\x00-\x7F]/ ? '8bit' : '7bit' }, # ascii-safeness
    },
  };

  print stringf(
    "My %{which}s name is %{name}s.  My name is %{name}l letters long.",
    {
      which => 'first',
      name  => 'Marvin',
    },
  );

  # Output:
  # My first name is Marvin.  My name is 6 letters long.

Because this is a useful recipe, there is a shorthand for it:

  use String::Formatter named_stringf => {
    codes => {
      s => sub { $_ },     # string itself
      l => sub { length }, # length of input string
      e => sub { /[^\x00-\x7F]/ ? '8bit' : '7bit' }, # ascii-safeness
    },
  };

=head2 method calls

Some objects provide methods to stringify them flexibly.  For example, many
objects that represent timestamps allow you to call C<strftime> or something
similar.  The C<method_replace> string replacer comes in handy here:

  use String::Formatter stringf => {
    input_processor => 'require_single_input',
    string_replacer => 'method_replace',

    codes => {
      f => 'strftime',
      c => 'format_cldr',
      s => sub { "$_[0]" },
    },
  };

  print stringf(
    "%{%Y-%m-%d}f is also %{yyyy-MM-dd}c.  Default string is %s.",
    DateTime->now,
  );

  # Output:
  # 2009-11-17 is also 2009-11-17.  Default string is 2009-11-17T15:35:11.

This recipe is available as the export C<method_stringf>:

  use String::Formatter method_stringf => {
    codes => {
      f => 'strftime',
      c => 'format_cldr',
      s => sub { "$_[0]" },
    },
  };

You can easily use this to implement an actual stringf-like method:

  package MyClass;

  use String::Formatter method_stringf => {
    -as => '_stringf',
    codes => {
      f => 'strftime',
      c => 'format_cldr',
      s => sub { "$_[0]" },
    },
  };

  sub format {
    my ($self, $format) = @_;
    return _stringf($format, $self);
  }

=head1 AUTHORS

=over 4

=item *

Ricardo Signes <cpan@semiotic.systems>

=item *

Darren Chamberlain <darren@cpan.org>

=back

=head1 COPYRIGHT AND LICENSE

This software is Copyright (c) 2022 by Ricardo Signes <cpan@semiotic.systems>.

This is free software, licensed under:

  The GNU General Public License, Version 2, June 1991

=cut