File Coverage

blib/lib/String/CommonPrefix.pm
Criterion Covered Total %
statement 19 21 90.4
branch 4 6 66.6
condition n/a
subroutine 5 5 100.0
pod 1 1 100.0
total 29 33 87.8


line stmt bran cond sub pod time code
1             package String::CommonPrefix;
2              
3             our $DATE = '2014-12-10'; # DATE
4             our $VERSION = '0.01'; # VERSION
5              
6 1     1   22154 use 5.010001;
  1         3  
  1         48  
7 1     1   7 use strict;
  1         2  
  1         47  
8 1     1   6 use warnings;
  1         2  
  1         54  
9              
10 1     1   7 use Exporter;
  1         2  
  1         208  
11             our @ISA = qw(Exporter);
12             our @EXPORT_OK = qw(
13             common_prefix
14             );
15              
16             sub common_prefix {
17 6 50   6 1 640 return undef unless @_;
18 6         7 my $i;
19             L1:
20 6         17 for ($i=0; $i < length($_[0]); $i++) {
21 6         16 for (@_[1..$#_]) {
22 9 50       13 if (length($_) < $i) {
23 0         0 $i--; last L1;
  0         0  
24             } else {
25 9 100       32 last L1 if substr($_, $i, 1) ne substr($_[0], $i, 1);
26             }
27             }
28             }
29 6         25 substr($_[0], 0, $i);
30             }
31              
32             1;
33             # ABSTRACT: Find prefix common to all strings
34              
35             __END__