| 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
Condition100%3/3
defined $base or defined $rem
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
| |||||||||||||||||||||||||||||
| 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
Condition100%3/3
defined $base and defined $rem
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
| |||||||||||||||||||||||||||||
| 76 | 5 | { next COMPARE_LOOP } | |||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||
| 78 | # if they're the same, return true | ||||||||||||||||||||||||||||
| 79 | 4 | ▶ | if ($base eq $rem) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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__ | ||||||||||||||||||||||||||||