blib/lib/Array/OneOf.pm

filter: Statementstmt 100% 22 / 22 Branchbran 100% 6 / 6 Conditioncond 100% 6 / 6 MC/DCmcdc 100% 4 / 4 Subroutinesub 100% 4 / 4 Podpod 0.0% 0 / 1 totaltotal 97.6% 42 / 43 CC 7 SCAR 16.5
CC
7
Cov
100%
oneof
7.0
BEGIN
1.0
BEGIN
1.0
CRAP 5.2
↑ Summary
1 package Array::OneOf;
2 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3  
4 # version
5 1 our $VERSION = '1.04';
6  
7 # export
8 1 ▶use base 'Exporter';
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use vars qw[@EXPORT_OK %EXPORT_TAGS];
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 @EXPORT_OK = qw[ oneof ];
11 1 %EXPORT_TAGS = ('all' =>[@EXPORT_OK]);
12  
13  
14 =head1 NAME
15  
16 Array::OneOf -- checks if an element is in an array
17  
18 =head1 SYNOPSIS
19  
20 use Array::OneOf ':all';
21
22 # this test will pass
23 if (oneof 'a', 'a', 'b', 'c') {
24 # do stuff
25 }
26
27 # this test will not pass
28 if (oneof 'x', 'a', 'b', 'c') {
29 # do stuff
30 }
31  
32 =head1 DESCRIPTION
33  
34 Array::OneOf provides one simple utility, the oneof function. Its use is
35 simple: if the first param is equal to any of the remaining params (in a
36 string comparison), it returns true. Otherwise it returns false.
37  
38 In this module, undef is considered the same as undef, and not the same as any
39 defined value. This is different than how most Perl programmers usually expect
40 comparisons to work, so caveat programmer.
41  
42 =head1 ALTERNATIVES
43  
44 Array::OneOf is not a particularly efficient way to test if a value is in an
45 array. If efficiency is an important goal you may want to look at
46 List::MoreUtils or Syntax::Keyword::Junction. You may also want to
47 investigate using grep and/or the smart match operator (~~). I use
48 Array::OneOf because it compares values the way my projects need them compared,
49 its simple syntax, and small footprint.
50  
51 =head1 INSTALLATION
52  
53 Array::OneOf can be installed with the usual routine:
54  
55 perl Makefile.PL
56 make
57 make test
58 make install
59  
60 =cut
61  
62 #------------------------------------------------------------------------------
63 # oneof
64 #
65 4 ▶sub oneof {
Subroutine oneof: called CC=7 SCAR=19.5
Pod undocumented
66 4 my ($base, @remaining) = @_;
67
68 COMPARE_LOOP:
69 4 foreach my $rem (@remaining) {
70 # if both are undef, return true
71 10 ▶ if ( (! defined $base) && (! defined $rem) )
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $base or defined $rem) 1 9 10
Condition100%3/3
defined $base or defined $rem
l!l&&r!l&&!r
541
MC/DC100%2/2
defined $base or defined $rem
defined $base defined $rem
Truth table100%3/3
A or B
A= defined $baseB= defined $rem
ABresult
1X1
011
000
72 1 { return 1 }
73
74 # if just one is undef, go to next loop
75 9 ▶ if ( (! defined $base) || (! defined $rem) )
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $base and defined $rem) 5 4 9
Condition100%3/3
defined $base and defined $rem
!ll&&!rl&&r
414
MC/DC100%2/2
defined $base and defined $rem
defined $base defined $rem
Truth table100%3/3
A and B
A= defined $baseB= defined $rem
ABresult
0X0
100
111
76 5 { next COMPARE_LOOP }
77
78 # if they're the same, return true
79 4 ▶ if ($base eq $rem)
Branch: 1 branch
BranchTrueFalseTotal
if ($base eq $rem) 1 3 4
80 1 { return 1 }
81 }
82
83 # not found, return false
84 2 return 0;
85 }
86 #
87 # oneof
88 #------------------------------------------------------------------------------
89  
90  
91 # return true
92 1 1;
93  
94 __END__
↑ Summary