blib/lib/Text/Abbrev.pm

filter: Statementstmt 96.7% 30 / 31 Branchbran 93.7% 15 / 16 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 1 / 1 Podpod 0.0% 0 / 1 totaltotal 93.8% 46 / 49 CC 11 SCAR 24.0
CC
11
Cov
96%
abbrev
11.0
CRAP 11.0
↑ Summary
1 package Text::Abbrev;
2 1 require 5.005; # Probably works on earlier versions too.
3 1 require Exporter;
4  
5 1 our $VERSION = '1.02';
6  
7 =head1 NAME
8  
9 Text::Abbrev - abbrev - create an abbreviation table from a list
10  
11 =head1 SYNOPSIS
12  
13 use Text::Abbrev;
14 abbrev $hashref, LIST
15  
16  
17 =head1 DESCRIPTION
18  
19 Stores all unambiguous truncations of each element of LIST
20 as keys in the associative array referenced by C<$hashref>.
21 The values are the original list elements.
22  
23 =head1 EXAMPLE
24  
25 $hashref = abbrev qw(list edit send abort gripe);
26  
27 %hash = abbrev qw(list edit send abort gripe);
28  
29 abbrev $hashref, qw(list edit send abort gripe);
30  
31 abbrev(*hash, qw(list edit send abort gripe));
32  
33 =cut
34  
35 1 @ISA = qw(Exporter);
36 1 @EXPORT = qw(abbrev);
37  
38 # Usage:
39 # abbrev \%foo, LIST;
40 # ...
41 # $long = $foo{$short};
42  
43 5 ▶sub abbrev {
Subroutine abbrev: called CC=11 SCAR=24.0
Pod undocumented
44 5 my ($word, $hashref, $glob, %table, $returnvoid);
45  
46 5 ▶ @_ or return; # So we don't autovivify onto @_ and trigger warning
Branch: 1 branch
BranchTrueFalseTotal
@_ or return 4 1 5
47 4 ▶ if (ref($_[0])) { # hash reference preferably
Branch: 2 branches
BranchTrueFalseTotal
if (ref $_[0]) { } 1 3 4
elsif (ref \$_[0] eq 'GLOB') { } 1 2 3
48 1 $hashref = shift;
49 1 $returnvoid = 1;
50 } elsif (ref \$_[0] eq 'GLOB') { # is actually a glob (deprecated)
51 1 $hashref = \%{shift()};
52 1 $returnvoid = 1;
53 }
54 4 %{$hashref} = ();
55  
56 4 WORD: foreach $word (@_) {
57 24 ▶ for (my $len = (length $word) - 1; $len > 0; --$len) {
Branch: 1 branch
BranchTrueFalseTotal
if ($len > 0) 88 24 112
58 88 my $abbrev = substr($word,0,$len);
59 88 my $seen = ++$table{$abbrev};
60 88 ▶ if ($seen == 1) { # We're the first word so far to have
Branch: 2 branches
BranchTrueFalseTotal
if ($seen == 1) { } 76 12 88
elsif ($seen == 2) { } 12 0 12
61 # this abbreviation.
62 76 $hashref->{$abbrev} = $word;
63 } elsif ($seen == 2) { # We're the second word to have this
64 # abbreviation, so we can't use it.
65 12 delete $hashref->{$abbrev};
66 } else { # We're the third word to have this
67 # abbreviation, so skip to the next word.
68 0 next WORD;
69 }
70 }
71 }
72 # Non-abbreviations always get entered, even if they aren't unique
73 4 foreach $word (@_) {
74 24 $hashref->{$word} = $word;
75 }
76 4 ▶ return if $returnvoid;
Branch: 1 branch
BranchTrueFalseTotal
if $returnvoid 2 2 4
77 2 ▶ if (wantarray) {
Branch: 1 branch
BranchTrueFalseTotal
if (wantarray) { } 1 1 2
78 1 %{$hashref};
79 } else {
80 1 $hashref;
81 }
82 }
83  
84 1 1;
↑ Summary