This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Upgrade to PodParser 1.23; but do not update
[perl5.git] / lib / Text / Abbrev.pm
CommitLineData
a0d0e21e 1package Text::Abbrev;
6d03d463 2require 5.005; # Probably works on earlier versions too.
a0d0e21e
LW
3require Exporter;
4
b75c8c73
MS
5our $VERSION = '1.00';
6
f06db76b
AD
7=head1 NAME
8
9abbrev - create an abbreviation table from a list
10
11=head1 SYNOPSIS
12
ac323e15
UP
13 use Text::Abbrev;
14 abbrev $hashref, LIST
f06db76b
AD
15
16
17=head1 DESCRIPTION
18
19Stores all unambiguous truncations of each element of LIST
6d03d463 20as keys in the associative array referenced by C<$hashref>.
f06db76b
AD
21The values are the original list elements.
22
23=head1 EXAMPLE
24
ac323e15
UP
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));
f06db76b
AD
32
33=cut
34
a0d0e21e
LW
35@ISA = qw(Exporter);
36@EXPORT = qw(abbrev);
37
38# Usage:
6d03d463 39# abbrev \%foo, LIST;
a0d0e21e
LW
40# ...
41# $long = $foo{$short};
42
43sub abbrev {
6d03d463 44 my ($word, $hashref, $glob, %table, $returnvoid);
ac323e15 45
df26a7db 46 @_ or return; # So we don't autovivify onto @_ and trigger warning
ac323e15 47 if (ref($_[0])) { # hash reference preferably
6d03d463
GS
48 $hashref = shift;
49 $returnvoid = 1;
50 } elsif (ref \$_[0] eq 'GLOB') { # is actually a glob (deprecated)
51 $hashref = \%{shift()};
52 $returnvoid = 1;
53 }
54 %{$hashref} = ();
55
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
61 # this abbreviation.
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.
68 next WORD;
a0d0e21e
LW
69 }
70 }
a0d0e21e 71 }
6d03d463
GS
72 # Non-abbreviations always get entered, even if they aren't unique
73 foreach $word (@_) {
74 $hashref->{$word} = $word;
ac323e15 75 }
6d03d463 76 return if $returnvoid;
ac323e15 77 if (wantarray) {
6d03d463 78 %{$hashref};
ac323e15 79 } else {
6d03d463 80 $hashref;
ac323e15 81 }
a0d0e21e
LW
82}
83
841;