METHODS

The Object Oriented interface

See functions printi(), sprinti(), printp(), and sprintp(): you can also call them as method.

  use String::Print 'oo';
  my $f = String::Print->new(%config);
  $f->printi($format, @params);
  # exactly the same functionality:
  use String::Print 'printi', %config;
  printi $format, @params;

The Object Oriented interface wins when you need the same configuration in multiple source files, or when you need different configurations within one program. In these cases, the hassle of explicitly using the object has some benefits.

Constructors

$class->new( %options )
Option Default

encode_for

undef

missing_key

<warning>

modifiers

[ qr/^%\S+/ = \&format_printf]>

serializers

<useful defaults>

encode_for => HASH|'HTML'
[0.91] The format string and the inserted values will get encoded according to some syntax rules. For instance, encode_entities() of HTML::Entities when you specify the predefined string HTML. See encodeFor().
missing_key => CODE
[0.91] During interpolation, it may be discovered that a key is missing from the parameter list. In that case, a warning is produced and undef inserted. May can overrule that behavior.
modifiers => ARRAY
Add one or more modifier handlers to power of the formatter. They will get preference over the predefined modifiers, but lower than the modifiers passed to print[ip] itself.
serializers => HASH|ARRAY
How to serialize data elements.
» Example:
  my $f = String::Print->new
    ( modifiers   => [ EUR   => sub {sprintf "%5.2f e", $_[0]} ]
    , serializers => [ UNDEF => sub {'-'} ]
    , encode_for  => 'HTML'
    );

  $f->printi("price: {p EUR}", p => 3.1415); # price: ␣␣3.14 e
  $f->printi("count: {c}", c => undef);      # count: -

Attributes

$obj->addModifiers( PAIRS )

The PAIRS are a combination of an selector and a CODE which processes the value when the modifier matches. The selector is a string or (preferred) a regular expression. Later modifiers with the same name overrule earlier definitions. You may also specify an ARRAY of modifiers per print.

See section Interpolation: Modifiers about the details.

$obj->encodeFor( HASH|undef|($predefined, %overrule) )

[0.91] Enable/define the output encoding. Read section Output encoding about the details.

Printing

The following are provided as method and as function. You find their explanation further down on this page.

$obj->printi([$fh], $format, PAIRS|HASH);

$obj->printp([$fh], $format, PAIRS|HASH);

$obj->sprinti($format, PAIRS|HASH);

$obj->sprintp($format, LIST, PAIRS);