[win32] support win32_select(0,0,0,msec) (winsock doesn't)
[p5sagit/p5-mst-13.2.git] / lib / Text / Abbrev.pm
CommitLineData
a0d0e21e 1package Text::Abbrev;
2require 5.000;
3require Exporter;
4
f06db76b 5=head1 NAME
6
7abbrev - create an abbreviation table from a list
8
9=head1 SYNOPSIS
10
ac323e15 11 use Text::Abbrev;
12 abbrev $hashref, LIST
f06db76b 13
14
15=head1 DESCRIPTION
16
17Stores all unambiguous truncations of each element of LIST
ac323e15 18as keys key in the associative array referenced to by C<$hashref>.
f06db76b 19The values are the original list elements.
20
21=head1 EXAMPLE
22
ac323e15 23 $hashref = abbrev qw(list edit send abort gripe);
24
25 %hash = abbrev qw(list edit send abort gripe);
26
27 abbrev $hashref, qw(list edit send abort gripe);
28
29 abbrev(*hash, qw(list edit send abort gripe));
f06db76b 30
31=cut
32
a0d0e21e 33@ISA = qw(Exporter);
34@EXPORT = qw(abbrev);
35
36# Usage:
37# &abbrev(*foo,LIST);
38# ...
39# $long = $foo{$short};
40
41sub abbrev {
ac323e15 42 my (%domain);
43 my ($name, $ref, $glob);
44
45 if (ref($_[0])) { # hash reference preferably
46 $ref = shift;
47 } elsif ($_[0] =~ /^\*/) { # looks like a glob (deprecated)
48 $glob = shift;
49 }
50 my @cmp = @_;
51
a0d0e21e 52 foreach $name (@_) {
ac323e15 53 my @extra = split(//,$name);
54 my $abbrev = shift(@extra);
55 my $len = 1;
56 my $cmp;
8ebc5c01 57 WORD: foreach $cmp (@cmp) {
a0d0e21e 58 next if $cmp eq $name;
59 while (substr($cmp,0,$len) eq $abbrev) {
8ebc5c01 60 last WORD unless @extra;
61 $abbrev .= shift(@extra);
a0d0e21e 62 ++$len;
63 }
64 }
65 $domain{$abbrev} = $name;
66 while (@extra) {
67 $abbrev .= shift(@extra);
68 $domain{$abbrev} = $name;
69 }
70 }
ac323e15 71 if ($ref) {
72 %$ref = %domain;
73 return;
74 } elsif ($glob) { # old style
75 local (*hash) = $glob;
76 %hash = %domain;
77 return;
78 }
79 if (wantarray) {
80 %domain;
81 } else {
82 \%domain;
83 }
a0d0e21e 84}
85
861;
87