our @EXPORT = qw(Complete);
# @(#)complete.pl,v1.2 (me@anywhere.EBay.Sun.COM) 09/23/91
Term::Complete - Perl word completion module
$input = Complete('prompt_string', \@completion_list);
$input = Complete('prompt_string', @completion_list);
This routine provides word completion on the list of words in
the array (or array ref).
The tty driver is put into raw mode and restored using an operating
system specific command, in UNIX-like environments C<stty>.
The following command characters are defined:
Attempts word completion.
Defined by I<$Term::Complete::complete>.
Erases the current input.
Defined by I<$Term::Complete::kill>.
=item E<lt>delE<gt>, E<lt>bsE<gt>
Defined by I<$Term::Complete::erase1> and I<$Term::Complete::erase2>.
Bell sounds when word completion fails.
The completion character E<lt>tabE<gt> cannot be changed.
our($complete, $kill, $erase1, $erase2, $tty_raw_noecho, $tty_restore, $stty, $tty_safe_restore);
our($tty_saved_state) = '';
foreach my $s (qw(/bin/stty /usr/bin/stty)) {
$tty_raw_noecho = "$s raw -echo";
$tty_restore = "$s -raw echo";
$tty_safe_restore = $tty_restore;
my($prompt, @cmp_lst, $cmp, $test, $l, @match);
my ($return, $r) = ("", 0);
if (ref $_[0] || $_[0] =~ /^\*/) {
@cmp_lst = sort @
{$_[0]};
# Attempt to save the current stty state, to be restored later
if (defined $stty && defined $tty_saved_state && $tty_saved_state eq '') {
$tty_saved_state = qx($stty -g
2>/dev/null
);
$tty_saved_state = undef;
$tty_saved_state =~ s/\s+$//g;
$tty_restore = qq($stty "$tty_saved_state" 2>/dev/null
);
system $tty_raw_noecho if defined $tty_raw_noecho;
while (($_ = getc(STDIN
)) ne "\r") {
# (TAB) attempt completion
@match = grep(/^\Q$return/, @cmp_lst);
$l = length($test = shift(@match));
until (substr($cmp, 0, $l) eq substr($test, 0, $l)) {
print($test = substr($test, $r, $l - $r));
$r = length($return .= $test);
print(join("\r\n", '', grep(/^\Q$return/, @cmp_lst)), "\r\n");
($_ eq $erase1 || $_ eq $erase2) && do {
# system $tty_restore if defined $tty_restore;
if (defined $tty_saved_state && defined $tty_restore && defined $tty_safe_restore)
# tty_restore caused error
system $tty_safe_restore;