File Coverage

blib/lib/String/Pad.pm
Criterion Covered Total %
statement 26 26 100.0
branch 8 8 100.0
condition 4 5 80.0
subroutine 5 5 100.0
pod 1 1 100.0
total 44 45 97.7


line stmt bran cond sub pod time code
1             package String::Pad;
2              
3             our $DATE = '2014-12-10'; # DATE
4             our $VERSION = '0.01'; # VERSION
5              
6 1     1   27955 use 5.010001;
  1         3  
  1         45  
7 1     1   5 use strict;
  1         2  
  1         166  
8 1     1   7 use warnings;
  1         1  
  1         44  
9              
10 1     1   5 use Exporter;
  1         2  
  1         339  
11             our @ISA = qw(Exporter);
12             our @EXPORT_OK = qw(
13             pad
14             );
15              
16             sub pad {
17 7     7 1 888 my ($text, $width, $which, $padchar, $is_trunc) = @_;
18 7 100       18 if ($which) {
19 3         8 $which = substr($which, 0, 1);
20             } else {
21 4         7 $which = "r";
22             }
23 7   100     28 $padchar //= " ";
24              
25 7         8 my $w = length($text);
26 7 100 66     21 if ($is_trunc && $w > $width) {
27 1         4 $text = substr($text, 0, $width, 1);
28             } else {
29 6 100       20 if ($which eq 'l') {
    100          
30 1         4 $text = ($padchar x ($width-$w)) . $text;
31             } elsif ($which eq 'c') {
32 2         8 my $n = int(($width-$w)/2);
33 2         8 $text = ($padchar x $n) . $text . ($padchar x ($width-$w-$n));
34             } else {
35 3         8 $text .= ($padchar x ($width-$w));
36             }
37             }
38 7         30 $text;
39             }
40              
41             1;
42             # ABSTRACT: String padding routines
43              
44             __END__