| 1 | use strict; | ||
| 2 | use warnings; | ||
| 3 | package String::Formatter::Cookbook 1.235; | ||
| 4 | # ABSTRACT: ways to put String::Formatter to use | ||
| 5 | 1; | ||
| 6 | |||
| 7 | #pod =encoding utf-8 | ||
| 8 | #pod | ||
| 9 | #pod =head1 OVERVIEW | ||
| 10 | #pod | ||
| 11 | #pod String::Formatter is a pretty simple system for building formatting routines, | ||
| 12 | #pod but it can be hard to get started without an idea of the sort of things that | ||
| 13 | #pod are possible. | ||
| 14 | #pod | ||
| 15 | #pod =head1 BASIC RECIPES | ||
| 16 | #pod | ||
| 17 | #pod =head2 constants only | ||
| 18 | #pod | ||
| 19 | #pod The simplest stringf interface you can provide is one that just formats | ||
| 20 | #pod constant strings, allowing the user to put them inside other fixed strings with | ||
| 21 | #pod alignment: | ||
| 22 | #pod | ||
| 23 | #pod use String::Formatter stringf => { | ||
| 24 | #pod input_processor => 'forbid_input', | ||
| 25 | #pod codes => { | ||
| 26 | #pod a => 'apples', | ||
| 27 | #pod b => 'bananas', | ||
| 28 | #pod w => 'watermelon', | ||
| 29 | #pod }, | ||
| 30 | #pod }; | ||
| 31 | #pod | ||
| 32 | #pod print stringf('I eat %a and %b but never %w.'); | ||
| 33 | #pod | ||
| 34 | #pod # Output: | ||
| 35 | #pod # I eat apples and bananas but never watermelon. | ||
| 36 | #pod | ||
| 37 | #pod If the user tries to parameterize the string by passing arguments after the | ||
| 38 | #pod format string, an exception will be raised. | ||
| 39 | #pod | ||
| 40 | #pod =head2 sprintf-like conversions | ||
| 41 | #pod | ||
| 42 | #pod Another common pattern is to create a routine that behaves like Perl's | ||
| 43 | #pod C<sprintf>, but with a different set of conversion routines. (It will also | ||
| 44 | #pod almost certainly have much simpler semantics than Perl's wildly complex | ||
| 45 | #pod behavior.) | ||
| 46 | #pod | ||
| 47 | #pod use String::Formatter stringf => { | ||
| 48 | #pod codes => { | ||
| 49 | #pod s => sub { $_ }, # string itself | ||
| 50 | #pod l => sub { length }, # length of input string | ||
| 51 | #pod e => sub { /[^\x00-\x7F]/ ? '8bit' : '7bit' }, # ascii-safeness | ||
| 52 | #pod }, | ||
| 53 | #pod }; | ||
| 54 | #pod | ||
| 55 | #pod print stringf( | ||
| 56 | #pod "My name is %s. I am about %l feet tall. I use an %e alphabet.\n", | ||
| 57 | #pod 'Ricardo', | ||
| 58 | #pod 'ffffff', | ||
| 59 | #pod 'abcchdefghijklllmnñopqrrrstuvwxyz', | ||
| 60 | #pod ); | ||
| 61 | #pod | ||
| 62 | #pod # Output: | ||
| 63 | #pod # My name is Ricardo. I am about 6 feet tall. I use an 8bit alphabet. | ||
| 64 | #pod | ||
| 65 | #pod B<Warning>: The behavior of positional string replacement when the conversion | ||
| 66 | #pod codes mix constant strings and code references is currently poorly nailed-down. | ||
| 67 | #pod Do not rely on it yet. | ||
| 68 | #pod | ||
| 69 | #pod =head2 named conversions | ||
| 70 | #pod | ||
| 71 | #pod This recipe acts a bit like Python's format operator when given a dictionary. | ||
| 72 | #pod Rather than matching format code position with input ordering, inputs can be | ||
| 73 | #pod chosen by name. | ||
| 74 | #pod | ||
| 75 | #pod use String::Formatter stringf => { | ||
| 76 | #pod input_processor => 'require_named_input', | ||
| 77 | #pod string_replacer => 'named_replace', | ||
| 78 | #pod | ||
| 79 | #pod codes => { | ||
| 80 | #pod s => sub { $_ }, # string itself | ||
| 81 | #pod l => sub { length }, # length of input string | ||
| 82 | #pod e => sub { /[^\x00-\x7F]/ ? '8bit' : '7bit' }, # ascii-safeness | ||
| 83 | #pod }, | ||
| 84 | #pod }; | ||
| 85 | #pod | ||
| 86 | #pod print stringf( | ||
| 87 | #pod "My %{which}s name is %{name}s. My name is %{name}l letters long.", | ||
| 88 | #pod { | ||
| 89 | #pod which => 'first', | ||
| 90 | #pod name => 'Marvin', | ||
| 91 | #pod }, | ||
| 92 | #pod ); | ||
| 93 | #pod | ||
| 94 | #pod # Output: | ||
| 95 | #pod # My first name is Marvin. My name is 6 letters long. | ||
| 96 | #pod | ||
| 97 | #pod Because this is a useful recipe, there is a shorthand for it: | ||
| 98 | #pod | ||
| 99 | #pod use String::Formatter named_stringf => { | ||
| 100 | #pod codes => { | ||
| 101 | #pod s => sub { $_ }, # string itself | ||
| 102 | #pod l => sub { length }, # length of input string | ||
| 103 | #pod e => sub { /[^\x00-\x7F]/ ? '8bit' : '7bit' }, # ascii-safeness | ||
| 104 | #pod }, | ||
| 105 | #pod }; | ||
| 106 | #pod | ||
| 107 | #pod =head2 method calls | ||
| 108 | #pod | ||
| 109 | #pod Some objects provide methods to stringify them flexibly. For example, many | ||
| 110 | #pod objects that represent timestamps allow you to call C<strftime> or something | ||
| 111 | #pod similar. The C<method_replace> string replacer comes in handy here: | ||
| 112 | #pod | ||
| 113 | #pod use String::Formatter stringf => { | ||
| 114 | #pod input_processor => 'require_single_input', | ||
| 115 | #pod string_replacer => 'method_replace', | ||
| 116 | #pod | ||
| 117 | #pod codes => { | ||
| 118 | #pod f => 'strftime', | ||
| 119 | #pod c => 'format_cldr', | ||
| 120 | #pod s => sub { "$_[0]" }, | ||
| 121 | #pod }, | ||
| 122 | #pod }; | ||
| 123 | #pod | ||
| 124 | #pod print stringf( | ||
| 125 | #pod "%{%Y-%m-%d}f is also %{yyyy-MM-dd}c. Default string is %s.", | ||
| 126 | #pod DateTime->now, | ||
| 127 | #pod ); | ||
| 128 | #pod | ||
| 129 | #pod # Output: | ||
| 130 | #pod # 2009-11-17 is also 2009-11-17. Default string is 2009-11-17T15:35:11. | ||
| 131 | #pod | ||
| 132 | #pod This recipe is available as the export C<method_stringf>: | ||
| 133 | #pod | ||
| 134 | #pod use String::Formatter method_stringf => { | ||
| 135 | #pod codes => { | ||
| 136 | #pod f => 'strftime', | ||
| 137 | #pod c => 'format_cldr', | ||
| 138 | #pod s => sub { "$_[0]" }, | ||
| 139 | #pod }, | ||
| 140 | #pod }; | ||
| 141 | #pod | ||
| 142 | #pod You can easily use this to implement an actual stringf-like method: | ||
| 143 | #pod | ||
| 144 | #pod package MyClass; | ||
| 145 | #pod | ||
| 146 | #pod use String::Formatter method_stringf => { | ||
| 147 | #pod -as => '_stringf', | ||
| 148 | #pod codes => { | ||
| 149 | #pod f => 'strftime', | ||
| 150 | #pod c => 'format_cldr', | ||
| 151 | #pod s => sub { "$_[0]" }, | ||
| 152 | #pod }, | ||
| 153 | #pod }; | ||
| 154 | #pod | ||
| 155 | #pod sub format { | ||
| 156 | #pod my ($self, $format) = @_; | ||
| 157 | #pod return _stringf($format, $self); | ||
| 158 | #pod } | ||
| 159 | #pod | ||
| 160 | #pod =cut | ||
| 161 | |||
| 162 | __END__ |