Hello Bad_ptr and welcome to active participation to the monastery!
If i understand your needs, you can accomplish something very similar via Term::ReadLine and glob
Infact that module has autocompletion via TAB key. If you press TAB twice it display all possible valid combinations based on what you entered till now.
I see you use *.pl as pattern; if i understand your needs you can read the pattern enetered at the prompt and use glob to populate an array. You'll use this array to do the autocompletion.
That module is somehow tricky to use correctly and behaves differently depending on the OS.
Here you have a working solution:
use strict;
use warnings;
use Term::ReadLine;
# I need this under windows:
$ENV{TERM}=undef;
my $term=Term::ReadLine->new("test");
print "Please enter a pattern:";
my $pattern = <STDIN>;
chomp $pattern;
my @files = glob($pattern);
print "there are ",scalar @files," different possibilities.\n";
my $choosen = '';
$term->Attribs->{completion_function} = sub {
my ($text, $line, $start) = @_;
return grep { /^$text/i } @files ;
};
while ( defined ( $_ = $term->readline( 'choose>') ) ) {
# a blank bare line is entered? redo the loop
next if /^$/;
if ($_=~/.*$/){
chomp $_;
$choosen = $_;
last;
}
}
print "You choosen [$choosen]\n";
If I call this program in the current directory I see (some space and comments added):
prompt>perl term-readline001.pl
Please enter a pattern:*.jpg
there are 23 different possibilities.
choose> # TAB TAB display all pos
+sibilities
apod20100415.jpg
cat.jpg
city.jpg
earth.jpg
file.jpg
file0.jpg
file3.jpg
file4.jpg
file5.jpg
file6.jpg
file7.jpg
file8.jpg
flowers.jpg
hacking-team-011509362-ff2f66c6-e452-4109-8bfb-6d4499c2bd5e.jpg
kawasaki.jpg
kid.jpg
mandelbrot_set_color_zoom_by_zeno333-d4va5l8.jpg
moon.jpg
PICT0034.JPG
romantic.jpg
serfandolweb.jpg
sunrise.jpg
umbrella.jpg
# if i press just ENTER nothing hap
+pens
choose>
choose>
choose>
choose>
choose>file # i press 'f' and after TAB: it dis
+play all 'file*' files
file.jpg file0.jpg file3.jpg file4.jpg file5.jpg file6.jpg file7.jpg
+file8.jpg
choose>file4.jpg # i enter '4' and TAB completes int
+o the only one matching
You choosen [file4.jpg ] # done! the program exit
prompt>
I suggest you to read Re^3: about Term::ReadLine and it's imported function The Solution and the whole thread because Term::ReadLine is a bit dificult to manage.
HtH
L*
There are no rules, there are no thumbs..
Reinvent the wheel, then learn The Wheel; may be one day you reinvent one of THE WHEELS.
|