2 require 5.005; # Probably works on earlier versions too.
9 abbrev - create an abbreviation table from a list
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.
25 $hashref = abbrev qw(list edit send abort gripe);
27 %hash = abbrev qw(list edit send abort gripe);
29 abbrev $hashref, qw(list edit send abort gripe);
31 abbrev(*hash, qw(list edit send abort gripe));
41 # $long = $foo{$short};
44 my ($word, $hashref, $glob, %table, $returnvoid);
46 @_ or return; # So we don't autovivify onto @_ and trigger warning
47 if (ref($_[0])) { # hash reference preferably
50 } elsif (ref \$_[0] eq 'GLOB') { # is actually a glob (deprecated)
51 $hashref = \%{shift()};
56 WORD: foreach $word (@_) {
57 for (my $len = (length $word) - 1; $len > 0; --$len) {
58 my $abbrev = substr($word,0,$len);
59 my $seen = ++$table{$abbrev};
60 if ($seen == 1) { # We're the first word so far to have
62 $hashref->{$abbrev} = $word;
63 } elsif ($seen == 2) { # We're the second word to have this
64 # abbreviation, so we can't use it.
65 delete $hashref->{$abbrev};
66 } else { # We're the third word to have this
67 # abbreviation, so skip to the next word.
72 # Non-abbreviations always get entered, even if they aren't unique
74 $hashref->{$word} = $word;
76 return if $returnvoid;