File Coverage

blib/lib/Bit/Compare.pm
Criterion Covered Total %
statement 29 29 100.0
branch 10 12 83.3
condition 1 3 33.3
subroutine 5 5 100.0
pod 2 2 100.0
total 47 51 92.1


line stmt bran cond sub pod time code
1 2     2   47194 use strict;
  2         4  
  2         79  
2 2     2   9 use warnings;
  2         4  
  2         142  
3             package Bit::Compare;
4             {
5             $Bit::Compare::VERSION = '0.001';
6             }
7             #ABSTRACT: Compare two "bit strings", returning differing bits
8              
9 2         23 use Sub::Exporter -setup => {
10             exports => [ qw/bitcompare bit/ ],
11             groups => {
12             default => [ qw/bitcompare/ ],
13             },
14 2     2   2216 };
  2         33705  
15              
16              
17             sub bitcompare {
18 3 100   3 1 19 if (@_ == 3) {
19 1         3 shift; # remove class/package to support old calling forms
20             }
21 3         9 my ($s1, $s2) = @_;
22 3         8 $s1 = bit($s1);
23 3         7 $s2 = bit($s2);
24 3 50 33     18 return unless ($s1 and $s2);
25 3         102 my $v = "" . join("", unpack("c*", $s1 ^ $s2));
26 3         77 $v =~ s/0//g;
27              
28 3         22 return length($v);
29             }
30              
31              
32             sub bit {
33 23     23 1 5683 my ($s) = @_;
34 23 50       60 return unless defined $s;
35 23         24 my @a;
36 23         134 foreach (split(/(.{2})/, $s)) {
37 216 100       459 next unless length("$_") > 0;
38 116         157 my $v = hex($_);
39 116         284 my $b = unpack("B*", pack("C",$v));
40 116 100       251 if (length("$_") == 1) {
41 16         28 $b = substr($b, -4);
42             }
43 116         539 push(@a, split(//, $b));
44             }
45 23 100       251 return (wantarray ? @a : join("", @a));
46             }
47              
48             1;
49              
50             __END__