I am not clear how to get the uncompleted command from the console.
The following minimal shell might help. You get the current word as the first parameter to the complete() function:
use Term::ReadLine;
my $term = Term::ReadLine->new('console_app');
my $attr = $term->Attribs;
$attr->{attempted_completion_function} = \&complete;
our @keywords;
chop and push @keywords, split while <DATA>;
while (1) {
my $line = $term->readline("perl >");
eval $line;
warn $@ if $@;
};
sub complete {
my ($text, $line, $start, $end) = @_;
# See if current word matches a package symbol
my @matches = grep { /^\Q$text/ } keys %::; # current package
if (@matches) {
return @matches;
}
# nope? then check for builtins.
return $term->completion_matches($text,\&keyword);
};
{
my $i;
sub keyword {
my ($text, $state) = @_;
return unless $text;
if($state) {
$i++;
}
else { # first call
$i = 0;
}
for (; $i<=$#keywords; $i++) {
return $keywords[$i] if $keywords[$i] =~ /^\Q$text/;
};
return undef;
}
};
# builtins (extracted from perlfunc)
__DATA__
chomp chop chr crypt hex index lc lcfirst length oct ord pack
reverse rindex sprintf substr uc ucfirst pos quotemeta split
study abs atan2 cos exp hex int log oct rand sin sqrt srand
pop push shift splice unshift grep join map reverse sort
unpack delete each exists keys values binmode close closedir
dbmclose dbmopen die eof fileno flock format getc print printf
read readdir rewinddir seek seekdir select syscall sysread
sysseek syswrite tell telldir truncate warn write pack read
syscall sysread syswrite unpack vec chdir chmod chown chroot
fcntl glob ioctl link lstat mkdir open opendir readlink rename
rmdir stat symlink sysopen umask unlink utime caller continue
die do dump eval exit goto last next redo return sub wantarray
caller import local my our package use defined dump eval
formline local my our reset scalar undef wantarray alarm exec
fork getpgrp getppid getpriority kill pipe setpgrp setpriority
sleep system times wait waitpid do import no package require
use bless dbmclose dbmopen package ref tie tied untie use
accept bind connect getpeername getsockname listen recv send
setsockopt shutdown socket socketpair msgctl msgget msgrcv
msgsnd semctl semget semop shmctl shmget shmread shmwrite
endgrent endhostent endnetent endpwent getgrent getgrgid
getgrnam getlogin getpwent getpwnam setgrent setpwent
endprotoent endservent gethostbyaddr gethostbyname gethostent
getnetbyaddr getnetbyname getnetent getprotobynumber getprotoent
getservbyname getservbyport getservent sethostent setnetent
setservent gmtime localtime time times abs bless chomp chr
exists formline glob import lc lcfirst map my no our prototype
qx qw readline readpipe ref sysopen tie tied uc ucfirst untie
use sub
--shmem
_($_=" "x(1<<5)."?\n".q·/)Oo. G°\ /
/\_¯/(q /
---------------------------- \__(m.====·.(_("always off the crowd"))."·
");sub _{s./.($e="'Itrs `mnsgdq Gdbj O`qkdq")=~y/"-y/#-z/;$e.e && print}