File: 30examp_oo.t

package info (click to toggle)
libstring-print-perl 0.96-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 184 kB
  • sloc: perl: 781; makefile: 2
file content (46 lines) | stat: -rw-r--r-- 1,245 bytes parent folder | download | duplicates (5)
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
#!/usr/bin/env perl
# Demonstrate the OO examples

use warnings;
use strict;

use Test::More tests => 7;

use String::Print 'oo';
use POSIX  qw/strftime/;

#
### currency conversion example
#

my $f = String::Print->new
  ( modifiers   => [ EUR   => sub { sprintf "%5.2f e", $_[2] } ]
  , serializers => [ UNDEF => sub {'-'} ]
  );
isa_ok($f, 'String::Print');

is($f->sprinti("price: {p EUR}#", p => 3.1415), 'price:  3.14 e#');

is($f->sprinti("count: {c}#", c => undef), 'count: -#');

#
### date-time conversion
#

$f->addModifiers( qr/T|DT|D/ =>
   sub { my ($formatter, $modif, $value, $args) = @_;
         # Windows does not support full POSIX, no %T nor %F
         my $time_format
           = $modif eq 'T'  ? '%H:%M:%S'
           : $modif eq 'D'  ? '%Y-%m-%d'
           : $modif eq 'DT' ? '%Y-%m-%dT%H:%M:%SZ'
           :                  'ERROR';
         strftime $time_format, gmtime($value);
       } );

my $now = 1365850757;

is($f->sprinti("time: {t T }", t => $now), 'time: 10:59:17', 'time');
is($f->sprinti("date: {t D }", t => $now), 'date: 2013-04-13', 'date');
is($f->sprinti("both: {t DT}", t => $now), 'both: 2013-04-13T10:59:17Z', 'dateTime');
is($f->sprinti("#{t T%10s}#", t => $now), '#  10:59:17#', 'stacked');