Introducing Text::sprintfn

Hitting CPAN mirrors as we speak (metacpan link).

Just my approach of doing string formatting that supports named parameter. Instead of creating a new formatter (with a new/different syntax), I just extend sprintf a bit:

sprintfn "%d %(num)d %d", {num=>1}, 2, 3; # prints 2 1 3

Named parameter can also be used in the width and precision field:

sprintfn "%(num)(width).(prec)f", {num=>1, width=>1, prec=>4}; # prints 1.0000

Compared to other modules, this one supports all Perl's sprintf() formats (since it's just sprintf under the hood) and allows mixing of named and positional parameters.

The current implementation is a bit naive, but it'll do for now.

Leave a comment

About Steven Haryanto

user-pic A programmer (mostly Perl 5 nowadays).