File Coverage

blib/lib/Banal/Utils/Array.pm
Criterion Covered Total %
statement 18 27 66.6
branch 0 6 0.0
condition 0 3 0.0
subroutine 6 7 85.7
pod 1 1 100.0
total 25 44 56.8


line stmt bran cond sub pod time code
1             #===============================================
2             package Banal::Utils::Array;
3              
4 1     1   2870 use 5.006;
  1         4  
  1         51  
5 1     1   7 use utf8;
  1         2  
  1         100  
6 1     1   30 use strict;
  1         3  
  1         53  
7 1     1   6 use warnings;
  1         1  
  1         124  
8 1     1   6 no warnings qw(uninitialized);
  1         1  
  1         344  
9              
10             require Exporter;
11              
12             our @ISA = qw(Exporter);
13             our @EXPORT_OK = qw(array1_starts_with_array2);
14              
15             # Returns true (1) if array1 starts with array2 (via element-by-element equality), false (0) otherwise.
16             # The order of elements is important (it ain't a bag). For each compared element, equality can be established either thru string (eq) or numeric (==) comparison.
17             sub array1_starts_with_array2 {
18 0     0 1   my ($array1, $array2) = @_;
19              
20             # if array2 is empty, then the result is undefined.
21 0 0         return unless (scalar($array2));
22            
23             # if array2 has more items than array1, then array1 can't possibly start with array2.
24 0 0         return 0 if (scalar($array2) > scalar($array1));
25            
26 0           my $k = 0;
27 0           foreach my $e2 (@$array2) {
28 1     1   6 no warnings;
  1         2  
  1         206  
29 0           my $e1 = $array1->[$k];
30            
31             # Here, we allow both string and numeric equality. It's kind o a relaxed thing. The "no warnings" pragma (above) comes in handy.
32 0 0 0       return 0 unless (($e1 eq $e2) || ($e1 == $e2));
33 0           $k++;
34             }
35 0           return 1;
36             }
37              
38              
39              
40              
41             1;
42              
43             __END__