String:...Cookbook(3)
okay bestie, this is String:...Cookbook(3) from the User Contributed Perl Documentation manual β yes, the header is literally giving truncated ellipsis realness, but the page is the cookbook for String::Formatter and it's serving recipe-book energy. π
π NAME π
String::Formatter::Cookbook β ways to put String::Formatter to use. that's the name-and-desc combo, bestie, and it's iconic. π«Ά
β¨ VERSION β¨
it's version 1.235, no cap. π¦
π OVERVIEW π
String::Formatter is a pretty simple system for building formatting routines, but ngl it can be hard to get started without an idea of the sort of things that are possible. so here's the vibe tour, bestie. π
π§ 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. lowkey generous. β¨
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. so yeah, don't count on ancient perl forever. π
π BASIC RECIPES π
okay bestie, here are the starter recipes. each one is its own little formatting vibe. π
π§ 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. no sneaky extra args, periodt. π«
π¦ sprintf-like conversions π¦
another common pattern is to create a routine that behaves like Perl's sprintf, but with a different set of conversion routines. (it will also almost certainly have much simpler semantics than Perl's wildly complex behavior.) slay. β¨
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.
β οΈ 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, bestie. π
πΈ 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. iykyk. π
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
},
};
π method calls π
some objects provide methods to stringify them flexibly. for example, many objects that represent timestamps allow you to call strftime or something similar. the 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 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);
}
π«Ά AUTHORS π«Ά
Ricardo Signes <cpan@semiotic.systems>Darren Chamberlain <darren@cpan.org>
π 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
okay that's the whole cookbook, bestie. signing off from perl v5.42.0, dated 2023-01-01 β this was the String:...Cookbook(3) page, and it ate. π
β¨π«Ά
π¦ where this page comes from
- opensuse-leap 16.0: perl-String-Formatter 1.235-bp160.1.12 Β· GPL-1.0-or-later
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms