line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package String::Sprintf; |
2
|
5
|
|
|
5
|
|
274552
|
use strict; |
|
5
|
|
|
|
|
32
|
|
|
5
|
|
|
|
|
119
|
|
3
|
5
|
|
|
5
|
|
21
|
use Carp; |
|
5
|
|
|
|
|
8
|
|
|
5
|
|
|
|
|
233
|
|
4
|
|
|
|
|
|
|
|
5
|
5
|
|
|
5
|
|
23
|
use vars qw($VERSION); |
|
5
|
|
|
|
|
8
|
|
|
5
|
|
|
|
|
1781
|
|
6
|
|
|
|
|
|
|
$VERSION = '1.001'; |
7
|
|
|
|
|
|
|
|
8
|
|
|
|
|
|
|
|
9
|
|
|
|
|
|
|
sub formatter { # constructor |
10
|
7
|
|
|
7
|
1
|
905
|
my $class = shift; |
11
|
7
|
50
|
|
|
|
31
|
(@_ % 2) and croak "Odd number of arguments"; |
12
|
7
|
|
|
|
|
20
|
my %handler = @_; |
13
|
7
|
|
50
|
|
|
38
|
$handler{'*'} ||= 'sprintf'; # default |
14
|
|
|
|
|
|
|
|
15
|
|
|
|
|
|
|
# sanity check |
16
|
7
|
|
|
|
|
11
|
my @errors; |
17
|
7
|
|
|
|
|
31
|
while(my($k, $v) = each %handler) { |
18
|
11
|
50
|
66
|
|
|
89
|
UNIVERSAL::isa($v, 'CODE') |
|
|
|
66
|
|
|
|
|
19
|
|
|
|
|
|
|
or !defined $v |
20
|
|
|
|
|
|
|
or $v eq 'sprintf' |
21
|
|
|
|
|
|
|
or push @errors, $k; |
22
|
|
|
|
|
|
|
} |
23
|
7
|
50
|
|
|
|
20
|
if(@errors) { |
24
|
0
|
|
|
|
|
0
|
my $errors = join ', ', @errors; |
25
|
0
|
0
|
|
|
|
0
|
my($s, $have) = @errors == 1 ? ('', 'has') : ('s', 'have'); |
26
|
0
|
|
|
|
|
0
|
croak "Format$s $errors $have no CODE ref as a handler"; |
27
|
|
|
|
|
|
|
} |
28
|
7
|
|
|
|
|
30
|
return bless \%handler, $class; |
29
|
|
|
|
|
|
|
} |
30
|
|
|
|
|
|
|
|
31
|
|
|
|
|
|
|
sub sprintf { |
32
|
8
|
|
|
8
|
1
|
1658
|
my($self, $string, @values) = @_; |
33
|
8
|
|
|
|
|
13
|
my $i = 0; |
34
|
8
|
|
|
|
|
50
|
$string =~ s(\%(?:\%|([+\-\d.]*)([a-zA-Z]))){ |
35
|
11
|
100
|
|
|
|
69
|
$2 ? do { |
36
|
10
|
100
|
66
|
|
|
49
|
if(ref(my $handler = $self->{$2} || $self->{'*'})) { |
37
|
6
|
|
|
|
|
18
|
$handler->($1, $values[$i++], \@values, $2); |
38
|
|
|
|
|
|
|
} else { |
39
|
4
|
|
|
|
|
36
|
CORE::sprintf("%$1$2", $values[$i++]); |
40
|
|
|
|
|
|
|
} |
41
|
|
|
|
|
|
|
} : '%' |
42
|
|
|
|
|
|
|
}ge; |
43
|
8
|
|
|
|
|
97
|
return $string; |
44
|
|
|
|
|
|
|
} |
45
|
|
|
|
|
|
|
|
46
|
|
|
|
|
|
|
42; |
47
|
|
|
|
|
|
|
|
48
|
|
|
|
|
|
|
__END__ |