| line | stmt | bran | cond | sub | pod | time | code | 
| 1 |  |  |  |  |  |  | package Array::OneOf; | 
| 2 | 1 |  |  | 1 |  | 468 | use strict; | 
|  | 1 |  |  |  |  | 2 |  | 
|  | 1 |  |  |  |  | 42 |  | 
| 3 |  |  |  |  |  |  |  | 
| 4 |  |  |  |  |  |  | # version | 
| 5 |  |  |  |  |  |  | our $VERSION = '1.04'; | 
| 6 |  |  |  |  |  |  |  | 
| 7 |  |  |  |  |  |  | # export | 
| 8 | 1 |  |  | 1 |  | 5 | use base 'Exporter'; | 
|  | 1 |  |  |  |  | 1 |  | 
|  | 1 |  |  |  |  | 117 |  | 
| 9 | 1 |  |  | 1 |  | 5 | use vars qw[@EXPORT_OK %EXPORT_TAGS]; | 
|  | 1 |  |  |  |  | 5 |  | 
|  | 1 |  |  |  |  | 176 |  | 
| 10 |  |  |  |  |  |  | @EXPORT_OK = qw[ oneof ]; | 
| 11 |  |  |  |  |  |  | %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 |  |  |  |  |  |  | sub oneof { | 
| 66 | 4 |  |  | 4 | 0 | 192 | my ($base, @remaining) = @_; | 
| 67 |  |  |  |  |  |  |  | 
| 68 |  |  |  |  |  |  | COMPARE_LOOP: | 
| 69 | 4 |  |  |  |  | 9 | foreach my $rem (@remaining) { | 
| 70 |  |  |  |  |  |  | # if both are undef, return true | 
| 71 | 10 | 100 | 66 |  |  | 31 | if ( (! defined $base) && (! defined $rem) ) | 
| 72 | 1 |  |  |  |  | 4 | { return 1 } | 
| 73 |  |  |  |  |  |  |  | 
| 74 |  |  |  |  |  |  | # if just one is undef, go to next loop | 
| 75 | 9 | 100 | 66 |  |  | 28 | if ( (! defined $base) || (! defined $rem) ) | 
| 76 | 5 |  |  |  |  | 14 | { next COMPARE_LOOP } | 
| 77 |  |  |  |  |  |  |  | 
| 78 |  |  |  |  |  |  | # if they're the same, return true | 
| 79 | 4 | 100 |  |  |  | 11 | if ($base eq $rem) | 
| 80 | 1 |  |  |  |  | 4 | { return 1 } | 
| 81 |  |  |  |  |  |  | } | 
| 82 |  |  |  |  |  |  |  | 
| 83 |  |  |  |  |  |  | # not found, return false | 
| 84 | 2 |  |  |  |  | 6 | return 0; | 
| 85 |  |  |  |  |  |  | } | 
| 86 |  |  |  |  |  |  | # | 
| 87 |  |  |  |  |  |  | # oneof | 
| 88 |  |  |  |  |  |  | #------------------------------------------------------------------------------ | 
| 89 |  |  |  |  |  |  |  | 
| 90 |  |  |  |  |  |  |  | 
| 91 |  |  |  |  |  |  | # return true | 
| 92 |  |  |  |  |  |  | 1; | 
| 93 |  |  |  |  |  |  |  | 
| 94 |  |  |  |  |  |  | __END__ |