File Coverage

blib/lib/String/CommonSuffix.pm
Criterion Covered Total %
statement 19 21 90.4
branch 6 8 75.0
condition n/a
subroutine 5 5 100.0
pod 1 1 100.0
total 31 35 88.5


line stmt bran cond sub pod time code
1             package String::CommonSuffix;
2              
3 2     2   313357 use 5.010001;
  2         8  
4 2     2   32 use strict;
  2         4  
  2         63  
5 2     2   29 use warnings;
  2         10  
  2         252  
6              
7             our $AUTHORITY = 'cpan:PERLANCAR'; # AUTHORITY
8             our $DATE = '2025-08-02'; # DATE
9             our $DIST = 'String-CommonPrefix'; # DIST
10             our $VERSION = '0.021'; # VERSION
11              
12 2     2   14 use Exporter qw(import);
  2         4  
  2         483  
13              
14             our @EXPORT_OK = qw(
15             common_suffix
16             );
17              
18             sub common_suffix {
19 6     6 1 5455 require List::Util;
20              
21 6 50       27 return undef unless @_; ## no critic: Subroutines::ProhibitExplicitReturnUndef
22 6         12 my $i;
23             L1:
24 6         24 for ($i = 0; $i < length($_[0]); $i++) {
25 6         27 for (@_[1..$#_]) {
26 9 50       51 if (length($_) < $i) {
27 0         0 $i--; last L1;
  0         0  
28             } else {
29 9 100       51 last L1 if substr($_, -($i+1), 1) ne substr($_[0], -($i+1), 1);
30             }
31             }
32             }
33 6 100       44 $i ? substr($_[0], -$i) : "";
34             }
35              
36             1;
37             # ABSTRACT: Return suffix common to all strings
38              
39             __END__