line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package String::Sprintf; |
2
|
5
|
|
|
5
|
|
293828
|
use strict; |
|
5
|
|
|
|
|
39
|
|
|
5
|
|
|
|
|
132
|
|
3
|
5
|
|
|
5
|
|
22
|
use Carp; |
|
5
|
|
|
|
|
7
|
|
|
5
|
|
|
|
|
297
|
|
4
|
|
|
|
|
|
|
|
5
|
5
|
|
|
5
|
|
28
|
use vars qw($VERSION); |
|
5
|
|
|
|
|
8
|
|
|
5
|
|
|
|
|
1857
|
|
6
|
|
|
|
|
|
|
$VERSION = '1.002_01'; |
7
|
|
|
|
|
|
|
|
8
|
|
|
|
|
|
|
|
9
|
|
|
|
|
|
|
sub formatter { # constructor |
10
|
7
|
|
|
7
|
1
|
1206
|
my $class = shift; |
11
|
7
|
50
|
|
|
|
32
|
(@_ % 2) and croak "Odd number of arguments"; |
12
|
7
|
|
|
|
|
21
|
my %handler = @_; |
13
|
7
|
|
50
|
|
|
42
|
$handler{'*'} ||= 'sprintf'; # default |
14
|
|
|
|
|
|
|
|
15
|
|
|
|
|
|
|
# sanity check |
16
|
7
|
|
|
|
|
10
|
my @errors; |
17
|
7
|
|
|
|
|
31
|
while(my($k, $v) = each %handler) { |
18
|
11
|
50
|
66
|
|
|
93
|
UNIVERSAL::isa($v, 'CODE') |
|
|
|
66
|
|
|
|
|
19
|
|
|
|
|
|
|
or !defined $v |
20
|
|
|
|
|
|
|
or $v eq 'sprintf' |
21
|
|
|
|
|
|
|
or push @errors, $k; |
22
|
|
|
|
|
|
|
} |
23
|
7
|
50
|
|
|
|
23
|
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
|
|
|
|
|
65
|
return bless \%handler, $class; |
29
|
|
|
|
|
|
|
} |
30
|
|
|
|
|
|
|
|
31
|
|
|
|
|
|
|
sub sprintf { |
32
|
8
|
|
|
8
|
1
|
1630
|
my($self, $string, @values) = @_; |
33
|
8
|
|
|
|
|
13
|
my $i = 0; |
34
|
8
|
|
|
|
|
51
|
$string =~ s(\%(?:\%|([+\-\d.]*)([a-zA-Z]))){ |
35
|
11
|
100
|
|
|
|
70
|
$2 ? do { |
36
|
10
|
100
|
66
|
|
|
45
|
if(ref(my $handler = $self->{$2} || $self->{'*'})) { |
37
|
6
|
|
|
|
|
21
|
$handler->($1, $values[$i++], \@values, $2); |
38
|
|
|
|
|
|
|
} else { |
39
|
4
|
|
|
|
|
38
|
CORE::sprintf("%$1$2", $values[$i++]); |
40
|
|
|
|
|
|
|
} |
41
|
|
|
|
|
|
|
} : '%' |
42
|
|
|
|
|
|
|
}ge; |
43
|
8
|
|
|
|
|
114
|
return $string; |
44
|
|
|
|
|
|
|
} |
45
|
|
|
|
|
|
|
|
46
|
|
|
|
|
|
|
42; |
47
|
|
|
|
|
|
|
|
48
|
|
|
|
|
|
|
__END__ |