require 5.005; # Probably works on earlier versions too.
abbrev - create an abbreviation table from a list
Stores all unambiguous truncations of each element of LIST
as keys in the associative array referenced by C<$hashref>.
The values are the original list elements.
$hashref = abbrev qw(list edit send abort gripe);
%hash = abbrev qw(list edit send abort gripe);
abbrev $hashref, qw(list edit send abort gripe);
abbrev(*hash, qw(list edit send abort gripe));
my ($word, $hashref, $glob, %table, $returnvoid);
if (ref($_[0])) { # hash reference preferably
} elsif (ref \
$_[0] eq 'GLOB') { # is actually a glob (deprecated)
WORD
: foreach $word (@_) {
for (my $len = (length $word) - 1; $len > 0; --$len) {
my $abbrev = substr($word,0,$len);
my $seen = ++$table{$abbrev};
if ($seen == 1) { # We're the first word so far to have
$hashref->{$abbrev} = $word;
} elsif ($seen == 2) { # We're the second word to have this
# abbreviation, so we can't use it.
delete $hashref->{$abbrev};
} else { # We're the third word to have this
# abbreviation, so skip to the next word.
# Non-abbreviations always get entered, even if they aren't unique
$hashref->{$word} = $word;