CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
Search Posts SEARCH
Who's Online WHO'S
Log in LOG

Home: Perl Programming Help: Intermediate:
autocompleting directory names



Dec 23, 2008, 1:44 PM

Post #1 of 3 (925 views)
autocompleting directory names Can't Post

I'm writing this Linux command-line app, and in one part of the script the user has to input the name of a directory. It would be stinkin' cool if the user could input part of the name, the hit tab, and the program would autocomplete the closest match, like in a bash shell.

I'm looking at term::readline and term::shell, but haven't had any experience with them. Any suggestions or arcane knowledge to share with me?


Dec 25, 2008, 7:45 PM

Post #2 of 3 (917 views)
Re: [hchristopher] autocompleting directory names [In reply to] Can't Post

Yup I had the same problem.

In a bash script you would use the compgen command which is a bash builtin command. The problem I had is that Perl backtics won't work with bash builtin commands. What I ended up doing is writing a small bash script to return the results of compgen. This is not what I would prefer but it is working for now. I would also appreciate any ideas.

Here is the code for


if [ "$2" = "-c" ]; then
compgen -c -o default $1
compgen -o default $1

Oh by the way this one way I get input from the user one character at a time.

use POSIX qw(:termios_h);

while (1) {
my $ch = getchar();
# Do some cool stuff;
last if $ch eq "\n";

sub getchar {
my $fd_stdin = fileno(STDIN);
my $term = POSIX::Termios->new();
my $oterm = $term->getlflag();
my $echo = ECHO | ECHOK | ICANON;
my $noecho = $oterm & ~$echo;
my $key = '';
$term->setcc(VTIME, 1);
$term->setattr($fd_stdin, TCSANOW);
sysread(STDIN, $key, 1);
$term->setcc( VTIME, 0);
$term->setattr($fd_stdin, TCSANOW);
return $key;

Joe Kamphaus
Boise Idaho USA

(This post was edited by joekamphaus on Dec 25, 2008, 8:38 PM)


Dec 25, 2008, 8:49 PM

Post #3 of 3 (911 views)
Re: [hchristopher] autocompleting directory names [In reply to] Can't Post

You may also want to use If you need to control the way text is displayed curses is a pretty good interface. It has a getch() function built in.

Joe Kamphaus
Boise Idaho USA


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives