The Perl Toolchain Summit needs more sponsors. If your company depends on Perl, please support this very important event.

NAME

String::Sprintf - Custom overloading of sprintf

SYNOPSIS

    use String::Sprintf;
    my $f = String::Sprintf->formatter(
      N => sub {
        my($width, $value, $values, $letter) = @_;
        return commify(sprintf "%${width}f", $value);
      }
    );

    my $out = $f->sprintf('(%10.2N, %10.2N)', 12345678.901, 87654.321);
    print "Formatted result: $out\n";

    sub commify {
        my $n = shift;
        $n =~ s/(\.\d+)|(?<=\d)(?=(?:\d\d\d)+\b)/$1 || ','/ge;
        return $n;
    }

DESCRIPTION

How often has it happened that you wished for a format that (s)printf just doesn't support? Have you ever wished you could overload sprintf with custom formats? Well, I know I have. And this module provides a way to do just that.

USAGE

So what is a formatter? Think of it as a "thing" that contains custom settings and behaviour for sprintf. Any formatting style that you don't set ("overload") falls back to the built-in keyword sprintf.

You can make a minimal formatter that behaves just like sprintf (and that is actually using sprintf internally) with:

  # nothing custom, all default:
  my $default = String::Sprintf->formatter();
  print $default->sprintf("%%%02X\n", 35);

  # which produces the same result as:
  print sprintf("%%%02X\n", 35);   # built-in

Because of the explicit use of these formatters, you can, of course, use several different formatters at the same time, even in the same expression. That is why it's better that it doesn't actually really overload the built-in sprintf. Plus, it was far easier to implement this way.

The syntax used is OO Perl, though I don't really consider this as an object-oriented module. For example, I foresee no reason for subclassing, and all formatters behave differently. That's what they're for.

METHODS

class method:

formatter( 'A' => \&formatter_A, 'B' => \&formatter_B, ... )

This returns a formatter object that holds custom formatting definitions, each associated with a letter, for its method sprintf. Its arguments consist of hash-like pairs of each a formatting letter (case sensitive) and a sub ref that is used for callbacks, and that is expected to return the formatted substring.

A key of * is the default format definition which will be used if no other definition matches. If you don't specify a * format, the formatter uses Perl's builtin sprintf.

callback API

A callback is supposed to behave like this:

  sub callback {
      my($width, $value, $values, $letter) = @_;
      ...
      return $formatted_string;
  }

Arguments: my($width, $value, $values, $letter) = @_;

There are 4 arguments passed to the callback functions, in order of descending importance. So the more commonly used parameters come first - and yes, that's my mnemonic. They are:

$width

The part that got put between the '%' and the letter.

$value

The current value from the arguments list, the one you're supposed to format.

$values = \@value

An array ref containing the whole list of all passed arguments, in case you want to support positional indexed values by default, as is done in strftime

$letter

The letter that caused the callback to be invoked. This is only provided for the cases where you use a common callback sub, for more than one letter, so you can still distinguish between them.

return value: a string

The return value in scalar context of this sub is inserted into the final, composed result, as a string.

instance method:

sprintf($formatstring, $value1, $value2, ...)

This method inserts the values you pass to it into the formatting string, and returns the constructed string. Just like the built-in sprintf does.

If you're using formatting letters that are not provided when you built the formatter, then it will fall back to the native formatter: "sprintf" in perlfunc. So you need only to provide formatters for which you're not happy with the built-ins.

EXPORTS

Nothing. What did you expect?

TODO

  • overload strftime too

  • proper support for position indexed values, like "%2$03X"

SEE ALSO

"sprintf" in perlfunc, sprintf(3), "strftime" in POSIX

BUGS

You tell me...?

SUPPORT

Currently maintained by brian d foy <briandfoy@pobox.com> and hosted on GitHub (https://github.com/briandfoy/string-sprintf).

AUTHOR

    Bart Lateur
    CPAN ID: BARTL
    Me at home, eating a hotdog
    bart.lateur@pandora.be
    L<http://perlmonks.org/?node=bart>
    L<http://users.pandora.be/bartl/>

REPOSITORY

https://github.com/briandfoy/string-sprintf

LICENSE AND COPYRIGHT

Copyright © Bart Lateur 2006.

This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.

My personal terms are like this: you can do whatever you want with this software: bundle it with any software, be it for free, released under the GPL, or commercial; you may redistribute it by itself, fix bugs, add features, and redistribute the modified copy. I would appreciate being informed in case you do the latter.

What you may not do, is sell the software, as a standalone product.