Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot

Autocomplete in perl console application

by raghrao (Novice)
on Jul 31, 2007 at 15:13 UTC ( [id://629849]=perlquestion: print w/replies, xml ) Need Help??

raghrao has asked for the wisdom of the Perl Monks concerning the following question:

Hi, I need to implement something like Bash autocomplete in perl cosole application. I am not clear how to get the uncompleted command from the console. Again after the autocomplete I need to restore the state of the application to take few parameters just like bash shell.. Any help on this is highly appreciated...
  • Comment on Autocomplete in perl console application

Replies are listed 'Best First'.
Re: Autocomplete in perl console application
by FunkyMonk (Chancellor) on Jul 31, 2007 at 15:32 UTC

    I've never used it, but I found Term::Complete on CPAN. That sounds like it does what you want.

Re: Autocomplete in perl console application
by whereiskurt (Friar) on Jul 31, 2007 at 15:59 UTC


    The Perl Cookbook, recipe 15.11, discusses how to best use the Term::ReadLine. It mentions looking at the Term::ReadLine::Gnu for other options. I've never used it, but I bet it'll help you out.

    Specifically read through this part about Custom Completion.

    I'd be glad to hear if it works for you...


Re: Autocomplete in perl console application
by shmem (Chancellor) on Aug 01, 2007 at 13:24 UTC
    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


    _($_=" "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}
      Thanks a lot. This is what I was trying to implement. I will add this Idea in the App I am trying to develop. Cheers.. Raghu

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://629849]
Approved by wazoox
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others rifling through the Monastery: (2)
As of 2024-06-12 17:58 GMT
Find Nodes?
    Voting Booth?

    No recent polls found

    erzuuli‥ 🛈The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.