blib/lib/String/Pad.pm

filter: Statementstmt 100% 47 / 47 Branchbran 100% 16 / 16 Conditioncond 87.5% 7 / 8 MC/DCmcdc 80.0% 4 / 5 Subroutinesub 100% 7 / 7 Podpod 100% 1 / 1 totaltotal 97.6% 82 / 84 CC 14 SCAR 23.1
CC
14
Cov
99%
pad
14.0
BEGIN
1.0
BEGIN
1.0
CRAP 10.1
↑ Summary
1 package String::Pad;
2  
3 2 our $AUTHORITY = 'cpan:PERLANCAR'; # AUTHORITY
4 2 our $DATE = '2021-08-01'; # DATE
5 2 our $DIST = 'String-Pad'; # DIST
6 2 our $VERSION = '0.021'; # VERSION
7  
8 2 ▶use 5.010001;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 2 ▶use Exporter;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 2 our @ISA = qw(Exporter);
14 2 our @EXPORT_OK = qw(
15 pad
16 );
17  
18 10 ▶sub pad {
Subroutine pad: called CC=14 SCAR=26.4
Pod documented
19 10 my ($text0, $width, $which, $padchar, $is_trunc) = @_;
20 10 ▶ if ($which) {
Branch: 1 branch
BranchTrueFalseTotal
if ($which) { } 3 7 10
21 3 $which = substr($which, 0, 1);
22 } else {
23 7 $which = "r";
24 }
25 10 ▶ $padchar //= " ";
Condition100%2/2
$padchar //= " "
l!l
19
MC/DC100%1/1
$padchar //= " "
$padchar
Truth table100%2/2
A
A= $padchar
Aresult
11
00
26  
27 10 ▶ my $texts = ref $text0 eq 'ARRAY' ? [@$text0] : [$text0];
Branch: 1 branch
BranchTrueFalseTotal
ref $text0 eq 'ARRAY' ? : 3 7 10
28  
29 10 ▶ if (!defined($width) || $width < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $width or $width < 0) 1 9 10
Condition66%2/3
not defined $width or $width < 0
l!l&&r!l&&!r
109
MC/DC50%1/2
not defined $width or $width < 0
not defined $width $width < 0
Truth table66%2/3
not(A) or B
A= not defined $widthB= $width < 0
ABresult
1X1
011
000
30 1 my $longest = 0;
31 1 for (@$texts) {
32 3 my $len = length($_);
33 3 ▶ $longest = $len if $longest < $len;
Branch: 1 branch
BranchTrueFalseTotal
if $longest < $len 2 1 3
34 }
35 1 $width = $longest;
36 }
37  
38 10 for my $text (@$texts) {
39 16 my $w = length($text);
40 16 ▶ if ($is_trunc && $w > $width) {
Branch: 1 branch
BranchTrueFalseTotal
if ($is_trunc and $w > $width) { } 3 13 16
Condition100%3/3
$is_trunc and $w > $width
!ll&&!rl&&r
1213
MC/DC100%2/2
$is_trunc and $w > $width
$is_trunc $w > $width
Truth table100%3/3
A and B
A= $is_truncB= $w > $width
ABresult
0X0
100
111
41 3 $text = substr($text, 0, $width, 1);
42 3 $w = $width;
43 } else {
44 13 ▶ if ($which eq 'l') {
Branch: 2 branches
BranchTrueFalseTotal
if ($which eq 'l') { } 1 12 13
elsif ($which eq 'c') { } 2 10 12
45 2 ▶ no warnings; # negative repeat count
Subroutine BEGIN: called CC=1 SCAR=0.0
46 1 $text = ($padchar x ($width-$w)) . $text;
47 } elsif ($which eq 'c') {
48 2 my $n = int(($width-$w)/2);
49 2 $text = ($padchar x $n) . $text . ($padchar x ($width-$w-$n));
50 } else {
51 2 ▶ no warnings; # negative repeat count
Subroutine BEGIN: called CC=1 SCAR=0.0
52 10 $text .= ($padchar x ($width-$w));
53 }
54 }
55 } # for $text
56  
57 10 ▶ ref $text0 eq 'ARRAY' ? $texts : $texts->[0];
Branch: 1 branch
BranchTrueFalseTotal
ref $text0 eq 'ARRAY' ? : 3 7 10
58 }
59  
60 2 1;
61 # ABSTRACT: String padding routines
62  
63 __END__
↑ Summary