File Coverage

blib/lib/Array/Lookup.pm
Criterion Covered Total %
statement 4 6 66.6
branch n/a
condition n/a
subroutine 2 2 100.0
pod n/a
total 6 8 75.0


line stmt bran cond sub pod time code
1             # Array::Lookup.pm
2              
3             package Array::Lookup;
4              
5             $VERSION = '2.3';
6              
7             @ISA = qw(Exporter);
8             @EXPORT = qw(lookup lookup_error);
9              
10             sub lookup;
11             sub lookup_error;
12              
13 1     1   546 use Carp;
  1         1  
  1         94  
14 1     1   1369 use Array::PrintCols;
  0            
  0            
15              
16             sub lookup {
17             my $key = shift;
18             length($key) or croak "Missing lookup key argument.\n";
19             my $keytab = shift or croak "Missing keyword table argument\n";
20             my $nfsub = shift;
21             my $tmsub = shift;
22              
23             my @keys;
24             if (ref($keytab) eq 'HASH') {
25             @keys = sort(keys %$keytab); # get sorted list of keys
26             } elsif (ref($keytab) eq 'ARRAY') {
27             @keys = sort(@$keytab); # get the sorted list of array items
28             } else {
29             croak "lookup: Second argument must be a HASH or ARRAY ref!\n";
30             }
31             # first check for any *exact* match
32             my @matches = grep(/^\Q$key\E$/i, @keys);
33             if (@matches or # any exact matches?
34             # if not, try abbreviation search
35             ((@matches = grep(/^\Q$key\E/i,@keys)) and
36             $#matches == 0)) { # is there exactly one abbrev?
37             $value = $matches[0]; # yes, either an exact or abbrev
38             $value = $keytab->{$value} if ref($keytab) eq 'HASH';
39             return $value;
40             }
41             if ($#matches < 0) { # no matches?
42             &$nfsub($key, $keytab, '') if ref($nfsub) eq 'CODE';
43             } elsif ($#matches > 0) { # too many matches
44             &$tmsub($key, $keytab, \@matches) if ref($tmsub) eq 'CODE';
45             }
46             undef;
47             }
48              
49             # Standard error handler for "lookup"
50              
51             sub lookup_error {
52             my $key = shift;
53             my $keytab = shift;
54             my $err = shift;
55             my $msg = shift || "lookup failed: '%s' %s; use one of:\n";
56             printf STDERR ($msg, $key, ($err ? 'is ambiguous' : 'not found'));
57             print_cols $keytab,'','',1;
58             undef;
59             }
60              
61             1;
62              
63             __END__