1Text::Sprintf::Named(3)User Contributed Perl DocumentatioTnext::Sprintf::Named(3)
2
3
4
6 Text::Sprintf::Named - sprintf-like function with named conversions
7
9 version 0.0405
10
12 use Text::Sprintf::Named;
13
14 my $formatter =
15 Text::Sprintf::Named->new(
16 {fmt => "Hello %(name)s! Today is %(day)s!"}
17 );
18
19 # Returns "Hello Ayeleth! Today is Sunday!"
20 $formatter->format({args => {'name' => "Ayeleth", 'day' => "Sunday"}});
21
22 # Returns "Hello John! Today is Thursday!"
23 $formatter->format({args => {'name' => "John", 'day' => "Thursday"}});
24
25 # Or alternatively using the non-OOP interface:
26
27 use Text::Sprintf::Named qw(named_sprintf);
28
29 # Prints "Hello Sophie!" (and a newline).
30 print named_sprintf("Hello %(name)s!\n", { name => 'Sophie' });
31
32 # Same, but with a flattened parameter list (not inside a hash reference)
33 print named_sprintf("Hello %(name)s!\n", name => 'Sophie');
34
36 Text::Sprintf::Named provides a sprintf equivalent with named
37 conversions. Named conversions are sprintf field specifiers (like "%s"
38 or ""%4d"") only they are associated with the key of an associative
39 array of parameters. So for example "%(name)s" will emit the 'name'
40 parameter as a string, and "%(num)4d" will emit the 'num' parameter as
41 a variable with a width of 4.
42
44 my $formatter = Text::Sprintf::Named->new({fmt => $format})
45 Creates a new object which formats according to the $format format.
46
47 $formatter->format({args => \%bindings})
48 Returns the formatting string as formatted using the named parameters
49 pointed to by the "args" parameter.
50
51 $self->calc_param({%args})
52 This method is used to calculate the parameter for the conversion. It
53 can be over-rided by subclasses so it will behave differently. An
54 example can be found in "t/02-override-param-retrieval.t" where it is
55 used to call the accessors of an object for values.
56
57 %args contains:
58
59 · named_params
60
61 The named paramters.
62
63 · name
64
65 The name of the conversion.
66
67 named_sprintf($format, {%parameters})
68 named_sprintf($format, %parameters)
69 This is a convenience function to directly format a string with the
70 named parameters, which can be specified inside a (non-blessed) hash
71 reference or as a flattened hash. See the synopsis for an example.
72
74 Shlomi Fish, "shlomif@cpan.org" , <http://www.shlomifish.org/>
75
77 The (possibly ad-hoc) regex for matching the optional digits+symbols
78 parameters' prefix of the sprintf conversion was originally written by
79 Bart Lateur (BARTL on CPAN) for his String::Sprintf module.
80
81 The syntax was borrowed directly from PythonXs "%" operator when used
82 with its dictionaries as the right-hand argument. A quick web search
83 did not yield good documentation about it (and I came with the idea of
84 a named sprintf without knowing that Python had it, only ran into the
85 fact that Python had it by web-searching).
86
88 Text::sprintfn is a newer module which only provides a procedural
89 interface that allows one to mix positional and named arguments, with
90 some other interesting features.
91
92 String::Formatter is a comprehensive module that allows one to define
93 custom sprintf-like functions (IXm not sure whether it has named
94 conversions). Its license is the GNU General Public Licence version 2
95 (GPLv2), which is both restrictive, and incompatible with version 3 of
96 the GPL and with many other open-source licenses.
97
98 String::Sprintf appears to allow one to provide custom sprintf/printf
99 formats (without providing named conversions).
100
101 For the lighter side, there is Acme::StringFormat, which provides a "%"
102 operator to format a string.
103
105 Copyright 2006 Shlomi Fish, all rights reserved.
106
107 This program is released under the following license: MIT/X11:
108
109 <http://www.opensource.org/licenses/mit-license.php>
110
112 Websites
113 The following websites have more information about this module, and may
114 be of help to you. As always, in addition to those websites please use
115 your favorite search engine to discover more resources.
116
117 · MetaCPAN
118
119 A modern, open-source CPAN search engine, useful to view POD in
120 HTML format.
121
122 <https://metacpan.org/release/Text-Sprintf-Named>
123
124 · RT: CPAN's Bug Tracker
125
126 The RT ( Request Tracker ) website is the default bug/issue
127 tracking system for CPAN.
128
129 <https://rt.cpan.org/Public/Dist/Display.html?Name=Text-Sprintf-Named>
130
131 · CPANTS
132
133 The CPANTS is a website that analyzes the Kwalitee ( code metrics )
134 of a distribution.
135
136 <http://cpants.cpanauthors.org/dist/Text-Sprintf-Named>
137
138 · CPAN Testers
139
140 The CPAN Testers is a network of smoke testers who run automated
141 tests on uploaded CPAN distributions.
142
143 <http://www.cpantesters.org/distro/T/Text-Sprintf-Named>
144
145 · CPAN Testers Matrix
146
147 The CPAN Testers Matrix is a website that provides a visual
148 overview of the test results for a distribution on various
149 Perls/platforms.
150
151 <http://matrix.cpantesters.org/?dist=Text-Sprintf-Named>
152
153 · CPAN Testers Dependencies
154
155 The CPAN Testers Dependencies is a website that shows a chart of
156 the test results of all dependencies for a distribution.
157
158 <http://deps.cpantesters.org/?module=Text::Sprintf::Named>
159
160 Bugs / Feature Requests
161 Please report any bugs or feature requests by email to
162 "bug-text-sprintf-named at rt.cpan.org", or through the web interface
163 at
164 <https://rt.cpan.org/Public/Bug/Report.html?Queue=Text-Sprintf-Named>.
165 You will be automatically notified of any progress on the request by
166 the system.
167
168 Source Code
169 The code is open to the world, and available for you to hack on. Please
170 feel free to browse it and play with it, or whatever. If you want to
171 contribute patches, please send me a diff or prod me to pull from your
172 repository :)
173
174 <https://github.com/shlomif/text-sprintf-named>
175
176 git clone https://bitbucket.org/shlomif/perl-text-sprintf
177
179 Shlomi Fish <shlomif@cpan.org>
180
182 Please report any bugs or feature requests on the bugtracker website
183 <https://github.com/shlomif/text-sprintf-named/issues>
184
185 When submitting a bug or request, please include a test-file or a patch
186 to an existing test-file that illustrates the bug or desired feature.
187
189 This software is Copyright (c) 2020 by Shlomi Fish.
190
191 This is free software, licensed under:
192
193 The MIT (X11) License
194
195
196
197perl v5.32.0 2020-10-20 Text::Sprintf::Named(3)