/usr/share/perl5/Text/sprintfn.pm is in libtext-sprintfn-perl 0.08-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 | package Text::sprintfn;
use 5.010001;
use strict;
use warnings;
require Exporter;
our @ISA = qw(Exporter);
our @EXPORT = qw(sprintfn printfn);
our $VERSION = '0.08'; # VERSION
our $distance = 10;
my $re1 = qr/[^)]+/s;
my $re2 = qr{(?<fmt>
%
(?<pi> \d+\$ | \((?<npi>$re1)\)\$?)?
(?<flags> [ +0#-]+)?
(?<vflag> \*?[v])?
(?<width> -?\d+ |
\*\d+\$? |
\((?<nwidth>$re1)\))?
(?<dot>\.?)
(?<prec>
(?: \d+ | \* |
\((?<nprec>$re1)\) ) ) ?
(?<conv> [%csduoxefgXEGbBpniDUOF])
)}x;
our $regex = qr{($re2|%|[^%]+)}s;
# faster version, without using named capture
if (1) {
$regex = qr{( #all=1
( #fmt=2
%
(#pi=3
\d+\$ | \(
(#npi=4
[^)]+)\)\$?)?
(#flags=5
[ +0#-]+)?
(#vflag=6
\*?[v])?
(#width=7
-?\d+ |
\*\d+\$? |
\((#nwidth=8
[^)]+)\))?
(#dot=9
\.?)
(#prec=10
(?: \d+ | \* |
\((#nprec=11
[^)]+)\) ) ) ?
(#conv=12
[%csduoxefgXEGbBpniDUOF])
) | % | [^%]+
)}xs;
}
sub sprintfn {
my ($format, @args) = @_;
my $hash;
if (ref($args[0]) eq 'HASH') {
$hash = shift(@args);
}
return sprintf($format, @args) if !$hash;
my %indexes; # key = $hash key, value = index for @args
push @args, (undef) x $distance;
$format =~ s{$regex}{
my ($all, $fmt, $pi, $npi, $flags,
$vflag, $width, $nwidth, $dot, $prec,
$nprec, $conv) =
($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12);
my $res;
if ($fmt) {
if (defined $npi) {
my $i = $indexes{$npi};
if (!$i) {
$i = @args + 1;
push @args, $hash->{$npi};
$indexes{$npi} = $i;
}
$pi = "${i}\$";
}
if (defined $nwidth) {
$width = $hash->{$nwidth};
}
if (defined $nprec) {
$prec = $hash->{$nprec};
}
$res = join("",
grep {defined} (
"%",
$pi, $flags, $vflag,
$width, $dot, $prec, $conv)
);
} else {
my $i = @args + 1;
push @args, $all;
$res = "\%${i}\$s";
}
$res;
}xego;
# DEBUG
#use Data::Dump; dd [$format, @args];
sprintf $format, @args;
}
sub printfn {
print sprintfn @_;
}
1;
# ABSTRACT: Drop-in replacement for sprintf(), with named parameter support
__END__
=pod
=encoding UTF-8
=head1 NAME
Text::sprintfn - Drop-in replacement for sprintf(), with named parameter support
=head1 VERSION
This document describes version 0.08 of Text::sprintfn (from Perl distribution Text-sprintfn), released on 2015-09-15.
=head1 SYNOPSIS
use Text::sprintfn; # by default exports sprintfn() and printfn()
# with no hash, behaves just like printf
printfn '<%04d>', 1, 2; # <0001>
# named parameter
printfn '<%(v1)-4d>', {v1=>-2}; # <-2 >
# mixed named and positional
printfn '<%d> <%(v1)d> <%d>', {v1=>1}, 2, 3; # <2> <1> <3>
# named width
printfn "<%(v1)(v2).1f>", {v1=>3, v2=>4}; # < 3>
# named precision
printfn "<%(v1)(v2).(v2)f>", {v1=>3, v2=>4}; # <3.0000>
=head1 DESCRIPTION
This module provides sprintfn() and printfn(), which are like sprintf() and
printf(), with the exception that they support named parameters from a hash.
=head1 FUNCTIONS
=head2 sprintfn $fmt, \%hash, ...
If first argument after format is not a hash, sprintfn() will behave exactly
like sprintf().
If hash is given, sprintfn() will look for named parameters in argument and
supply the values from the hash. Named parameters are surrounded with
parentheses, i.e. "(NAME)". They can occur in format parameter index:
%2$d # sprintf version, take argument at index 2
%(two)d # $ is optional
%(two)$d # same
or in width:
%-10d # sprintf version, use (minimum) width of 10
%-(width)d # like sprintf, but use width from hash key 'width'
%(var)-(width)d # format hash key 'var' with width from hash key 'width'
or in precision:
%6.2f # sprintf version, use precision of 2 decimals
%6.(prec)f # like sprintf, but use precision from hash key 'prec'
%(width).(prec)f
%(var)(width).(prec)f
The existence of formats using hash keys will not affect indexes of the rest of
the argument, example:
sprintfn "<%(v1)s> <%2$d> <%d>", {v1=>10}, 0, 1, 2; # "<10> <2> <0>"
Like sprintf(), if format is unknown/erroneous, it will be printed as-is.
There is currently no way to escape ")" in named parameter, e.g.:
%(var containing ))s
=head2 printfn $fmt, ...
Equivalent to: print sprintfn($fmt, ...).
=head1 RATIONALE
There exist other CPAN modules for string formatting with named parameter
support. Two of such modules are L<String::Formatter> and
L<Text::Sprintf::Named>. This module is far simpler to use and retains all of
the features of Perl's sprintf() (which we like, or perhaps hate, but
nevertheless are familiar with).
String::Formatter requires you to create a new formatter function first.
Text::Sprintf::Named also accordingly requires you to instantiate an object
first. There is currently no way to mix named and positional parameters. And you
don't get the full features of sprintf().
=head1 HOW IT WORKS
Text::sprintfn works by converting the format string into sprintf format, i.e.
replacing the named parameters like C<%(foo)s> to something like C<%11$s>.
=head1 DOWNSIDES
Currently the main downside is speed. C<sprintfn()> is about 2-3 orders of
magnitude slower than C<sprintf()>. A sample benchmark:
Rate sprintfn("%(a)s%(b)d%(c)f",{..}) 1k sprintfn("%(a)s",{a=>1}) 1k sprintfn("%s",1) 1k sprintf ("%s%d%f",1,2,3) 1k sprintf ("%s",1) 1k
sprintfn("%(a)s%(b)d%(c)f",{..}) 1k 45.354+-0.084/s -- -58.7% -97.3% -99.9% -99.9%
sprintfn("%(a)s",{a=>1}) 1k 109.82+-0.14/s 142.13+-0.54% -- -93.4% -99.7% -99.7%
sprintfn("%s",1) 1k 1672.62+-0.6/s 3587.9+-7% 1423.1+-2% -- -96.1% -96.2%
sprintf ("%s%d%f",1,2,3) 1k 42658+-12/s 93950+-180% 38744+-50% 2450.4% -- -2.2%
sprintf ("%s",1) 1k 43596+-48/s 96020+-210% 39599+-66% 2506.5+-3% 2.2+-0.12% --
=head1 TIPS AND TRICKS
=head2 Common mistake 1
Writing
%(var)
instead of
%(var)s
=head2 Common mistake 2 (a bit more newbish)
Writing
sprintfn $format, %hash, ...;
instead of
sprintfn $format, \%hash, ...;
=head2 Alternative hashes
You have several hashes (%h1, %h2, %h3) which should be consulted for values.
You can either merge the hash first:
%h = (%h1, %h2, %h3); # or use some hash merging module
printfn $format, \%h, ...;
or create a tied hash which can consult hashes for you:
tie %h, 'Your::Module', \%h1, \%h2, \%h3;
printfn $format, \%h, ...;
=head1 SEE ALSO
sprintf() section on L<perlfunc>
L<String::Formatter>
L<Text::Sprintf::Named>
=head1 HOMEPAGE
Please visit the project's homepage at L<https://metacpan.org/release/Text-sprintfn>.
=head1 SOURCE
Source repository is at L<https://github.com/perlancar/perl-Text-sprintfn>.
=head1 BUGS
Please report any bugs or feature requests on the bugtracker website L<https://rt.cpan.org/Public/Dist/Display.html?Name=Text-sprintfn>
When submitting a bug or request, please include a test-file or a
patch to an existing test-file that illustrates the bug or desired
feature.
=head1 AUTHOR
perlancar <perlancar@cpan.org>
=head1 COPYRIGHT AND LICENSE
This software is copyright (c) 2015 by perlancar@cpan.org.
This is free software; you can redistribute it and/or modify it under
the same terms as the Perl 5 programming language system itself.
=cut
|