File Coverage

blib/lib/String/Palindrome.pm
Criterion Covered Total %
statement 32 32 100.0
branch 20 20 100.0
condition 3 3 100.0
subroutine 8 8 100.0
pod 1 1 100.0
total 64 64 100.0


line stmt bran cond sub pod time code
1 1     1   25762 use warnings;
  1         3  
  1         35  
2 1     1   6 use strict;
  1         2  
  1         35  
3 1     1   977 use utf8;
  1         10  
  1         8  
4              
5             package String::Palindrome;
6             BEGIN {
7 1     1   76 $String::Palindrome::VERSION = '2.000001';
8             }
9             BEGIN {
10 1     1   27 $String::Palindrome::AUTHORITY = 'cpan:LESPEA';
11             }
12              
13             require Exporter;
14              
15 1     1   8 use vars qw(@EXPORT_OK);
  1         1  
  1         55  
16 1     1   6 use base qw(Exporter);
  1         8  
  1         3373  
17             @EXPORT_OK = qw(is_palindrome); # symbols to export on request
18              
19              
20             # ABSTRACT: Determine if a string is a palindrome
21              
22              
23              
24              
25             sub is_palindrome { ## no critic 'Subroutines::RequireArgUnpacking'
26             # Get the args out
27 79 100   79 1 61980 my $arg = @_ > 1 ? \@_
28             : $_[0]
29             ;
30              
31             # If no arg was given, then return undef
32 79 100       287 if ( !defined $arg ) {
    100          
33 1         4 return;
34             }
35              
36             # Check to see if we're dealing with a reference
37             elsif (ref $arg) {
38             # Return immediately if this isn't an array ref or the array ref
39             # contains no values
40 61 100       188 return unless ref $arg eq 'ARRAY';
41 60 100       200 return unless @$arg;
42              
43 59         84 for (my ($i, $j) = (0, $#{$arg}); $i < $j; $i++, $j--) { ## no critic 'ControlStructures::ProhibitCStyleForLoops ValuesAndExpressions::ProhibitCommaSeparatedStatements'
  59         243  
44 116         148 my ($a, $b) = @{$arg}[$i, $j];
  116         271  
45 116 100       397 if (!defined $a) {
    100          
46 6 100       30 return 0 if defined $b;
47             }
48             elsif (!defined $b) {
49 2         11 return 0;
50             }
51             else {
52 108 100       617 return 0 unless $a eq $b;
53             }
54             }
55 33         480 return 1;
56             }
57              
58             else {
59 17 100 100     178 return ($arg ne q{} and $arg eq reverse $arg) ? 1 : 0;
60             }
61             }
62              
63              
64             1; # End of String::Palindrome
65              
66             __END__