| 1 | package String::Sprintf; | ||||||||||||||||||||||||||||||||||||||||||
| 2 | 5 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 3 | 5 | ▶ | use Carp; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||||||||||||||||
| 5 | 5 | ▶ | use vars qw($VERSION); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 6 | 5 | $VERSION = '1.004'; | |||||||||||||||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||||||||||||||||
| 9 | 7 | ▶ | sub formatter { # constructor | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
formatter: called CC=9 SCAR=25.6
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 10 | 7 | my $class = shift; | |||||||||||||||||||||||||||||||||||||||||
| 11 | 7 | ▶ | (@_ % 2) and croak "Odd number of arguments"; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 12 | 7 | my %handler = @_; | |||||||||||||||||||||||||||||||||||||||||
| 13 | 7 | ▶ | $handler{'*'} ||= 'sprintf'; # default | ||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$handler{"*"} ||= "sprintf"
MC/DC0%0/1
$handler{"*"} ||= "sprintf"
$handler{"*"}
Truth table50%1/2
A
A= $handler{"*"}
| |||||||||||||||||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||||||||||||||||
| 15 | # sanity check | ||||||||||||||||||||||||||||||||||||||||||
| 16 | 7 | my @errors; | |||||||||||||||||||||||||||||||||||||||||
| 17 | 7 | ▶ | while(my($k, $v) = each %handler) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 18 | 11 | ▶ | UNIVERSAL::isa($v, 'CODE') | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%4/6
UNIVERSAL::isa($v, "CODE") or not defined $v or $v eq "sprintf"
UNIVERSAL::isa($v, "CODE") or not defined $v
MC/DC0%0/3
UNIVERSAL::isa($v, "CODE") or not defined $v or $v eq "sprintf"
UNIVERSAL::isa($v, "CODE")
not defined $v
$v eq "sprintf"
Truth table50%2/4
A or not(B) or C
A= UNIVERSAL::isa($v, "CODE")B= not defined $vC= $v eq "sprintf"
| |||||||||||||||||||||||||||||||||||||||||||
| 19 | or !defined $v | ||||||||||||||||||||||||||||||||||||||||||
| 20 | or $v eq 'sprintf' | ||||||||||||||||||||||||||||||||||||||||||
| 21 | or push @errors, $k; | ||||||||||||||||||||||||||||||||||||||||||
| 22 | } | ||||||||||||||||||||||||||||||||||||||||||
| 23 | 7 | ▶ | if(@errors) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 24 | 0 | my $errors = join ', ', @errors; | |||||||||||||||||||||||||||||||||||||||||
| 25 | 0 | ▶ | my($s, $have) = @errors == 1 ? ('', 'has') : ('s', 'have'); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 26 | 0 | croak "Format$s $errors $have no CODE ref as a handler"; | |||||||||||||||||||||||||||||||||||||||||
| 27 | } | ||||||||||||||||||||||||||||||||||||||||||
| 28 | 7 | return bless \%handler, $class; | |||||||||||||||||||||||||||||||||||||||||
| 29 | } | ||||||||||||||||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||||||||||||||||
| 31 | 8 | ▶ | sub sprintf { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
sprintf: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 32 | 8 | my($self, $string, @values) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 33 | 8 | my $i = 0; | |||||||||||||||||||||||||||||||||||||||||
| 34 | 8 | $string =~ s(\%(?:\%|([+\-\d.]*)([a-zA-Z]))){ | |||||||||||||||||||||||||||||||||||||||||
| 35 | 11 | ▶ | $2 ? do { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 36 | 10 | ▶ | if(ref(my $handler = $self->{$2} || $self->{'*'})) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$self->{$2} || $self->{'*'}
MC/DC0%0/2
$self->{$2} || $self->{'*'}
$self->{$2}
$self->{'*'}
Truth table66%2/3
A || B
A= $self->{$2}B= $self->{'*'}
| |||||||||||||||||||||||||||||||||||||||||||
| 37 | 6 | $handler->($1, $values[$i++], \@values, $2); | |||||||||||||||||||||||||||||||||||||||||
| 38 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 39 | 4 | CORE::sprintf("%$1$2", $values[$i++]); | |||||||||||||||||||||||||||||||||||||||||
| 40 | } | ||||||||||||||||||||||||||||||||||||||||||
| 41 | } : '%' | ||||||||||||||||||||||||||||||||||||||||||
| 42 | }ge; | ||||||||||||||||||||||||||||||||||||||||||
| 43 | 8 | return $string; | |||||||||||||||||||||||||||||||||||||||||
| 44 | } | ||||||||||||||||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||||||||||||||||
| 46 | 5 | 42; | |||||||||||||||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||||||||||||||||
| 48 | __END__ | ||||||||||||||||||||||||||||||||||||||||||