.\" -*- mode: troff; coding: utf-8 -*- .\" Automatically generated by Pod::Man v6.0.2 (Pod::Simple 3.48) .\" .\" Standard preamble: .\" ======================================================================== .de Sp \" Vertical space (when we can't use .PP) .if t .sp .5v .if n .sp .. .de Vb \" Begin verbatim text .ft CW .nf .ne \\$1 .. .de Ve \" End verbatim text .ft R .fi .. .\" \*(C` and \*(C' are quotes in nroff, nothing in troff, for use with C<>. .ie n \{\ . ds C` "" . ds C' "" 'br\} .el\{\ . ds C` . ds C' 'br\} .\" .\" Escape single quotes in literal strings from groff's Unicode transform. .ie \n(.g .ds Aq \(aq .el .ds Aq ' .\" .\" If the F register is >0, we'll generate index entries on stderr for .\" titles (.TH), headers (.SH), subsections (.SS), items (.Ip), and index .\" entries marked with X<> in POD. Of course, you'll have to process the .\" output yourself in some meaningful fashion. .\" .\" Avoid warning from groff about undefined register 'F'. .de IX .. .nr rF 0 .if \n(.g .if rF .nr rF 1 .if (\n(rF:(\n(.g==0)) \{\ . if \nF \{\ . de IX . tm Index:\\$1\t\\n%\t"\\$2" .. . if !\nF==2 \{\ . nr % 0 . nr F 2 . \} . \} .\} .rr rF .\" .\" Required to disable full justification in groff 1.23.0. .if n .ds AD l .\" ======================================================================== .\" .IX Title "String::Formatter::Cookbook 3" .TH String::Formatter::Cookbook 3 2026-10-26 "perl v5.44.0" "User Contributed Perl Documentation" .\" For nroff, turn off justification. Always turn off hyphenation; it makes .\" way too many mistakes in technical documents. .if n .ad l .nh .SH OVERVIEW .IX Header "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. .SH "BASIC RECIPES" .IX Header "BASIC RECIPES" .SS "constants only" .IX Subsection "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: .PP .Vb 8 \& use String::Formatter stringf => { \& input_processor => \*(Aqforbid_input\*(Aq, \& codes => { \& a => \*(Aqapples\*(Aq, \& b => \*(Aqbananas\*(Aq, \& w => \*(Aqwatermelon\*(Aq, \& }, \& }; \& \& print stringf(\*(AqI eat %a and %b but never %w.\*(Aq); \& \& # Output: \& # I eat apples and bananas but never watermelon. .Ve .PP If the user tries to parameterize the string by passing arguments after the format string, an exception will be raised. .SS "sprintf\-like conversions" .IX Subsection "sprintf-like conversions" Another common pattern is to create a routine that behaves like Perl\*(Aqs \&\f(CW\*(C`sprintf\*(C'\fR, but with a different set of conversion routines. (It will also almost certainly have much simpler semantics than Perl\*(Aqs wildly complex behavior.) .PP .Vb 7 \& use String::Formatter stringf => { \& codes => { \& s => sub { $_ }, # string itself \& l => sub { length }, # length of input string \& e => sub { /[^\ex00\-\ex7F]/ ? \*(Aq8bit\*(Aq : \*(Aq7bit\*(Aq }, # ascii\-safeness \& }, \& }; \& \& print stringf( \& "My name is %s. I am about %l feet tall. I use an %e alphabet.\en", \& \*(AqRicardo\*(Aq, \& \*(Aqffffff\*(Aq, \& \*(Aqabcchdefghijklllmnñopqrrrstuvwxyz\*(Aq, \& ); \& \& # Output: \& # My name is Ricardo. I am about 6 feet tall. I use an 8bit alphabet. .Ve .PP \&\fBWarning\fR: 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. .SS "named conversions" .IX Subsection "named conversions" This recipe acts a bit like Python\*(Aqs format operator when given a dictionary. Rather than matching format code position with input ordering, inputs can be chosen by name. .PP .Vb 3 \& use String::Formatter stringf => { \& input_processor => \*(Aqrequire_named_input\*(Aq, \& string_replacer => \*(Aqnamed_replace\*(Aq, \& \& codes => { \& s => sub { $_ }, # string itself \& l => sub { length }, # length of input string \& e => sub { /[^\ex00\-\ex7F]/ ? \*(Aq8bit\*(Aq : \*(Aq7bit\*(Aq }, # ascii\-safeness \& }, \& }; \& \& print stringf( \& "My %{which}s name is %{name}s. My name is %{name}l letters long.", \& { \& which => \*(Aqfirst\*(Aq, \& name => \*(AqMarvin\*(Aq, \& }, \& ); \& \& # Output: \& # My first name is Marvin. My name is 6 letters long. .Ve .PP Because this is a useful recipe, there is a shorthand for it: .PP .Vb 7 \& use String::Formatter named_stringf => { \& codes => { \& s => sub { $_ }, # string itself \& l => sub { length }, # length of input string \& e => sub { /[^\ex00\-\ex7F]/ ? \*(Aq8bit\*(Aq : \*(Aq7bit\*(Aq }, # ascii\-safeness \& }, \& }; .Ve .SS "method calls" .IX Subsection "method calls" Some objects provide methods to stringify them flexibly. For example, many objects that represent timestamps allow you to call \f(CW\*(C`strftime\*(C'\fR or something similar. The \f(CW\*(C`method_replace\*(C'\fR string replacer comes in handy here: .PP .Vb 3 \& use String::Formatter stringf => { \& input_processor => \*(Aqrequire_single_input\*(Aq, \& string_replacer => \*(Aqmethod_replace\*(Aq, \& \& codes => { \& f => \*(Aqstrftime\*(Aq, \& c => \*(Aqformat_cldr\*(Aq, \& 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. .Ve .PP This recipe is available as the export \f(CW\*(C`method_stringf\*(C'\fR: .PP .Vb 7 \& use String::Formatter method_stringf => { \& codes => { \& f => \*(Aqstrftime\*(Aq, \& c => \*(Aqformat_cldr\*(Aq, \& s => sub { "$_[0]" }, \& }, \& }; .Ve .PP You can easily use this to implement an actual stringf\-like method: .PP .Vb 1 \& package MyClass; \& \& use String::Formatter method_stringf => { \& \-as => \*(Aq_stringf\*(Aq, \& codes => { \& f => \*(Aqstrftime\*(Aq, \& c => \*(Aqformat_cldr\*(Aq, \& s => sub { "$_[0]" }, \& }, \& }; \& \& sub format { \& my ($self, $format) = @_; \& return _stringf($format, $self); \& } .Ve