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

Hello,
Here is part of code:
print "Podaj sciezke do pliku bazy, ktory archiwizujemy: "; chomp( $question2 = <STDIN> );
As a reply to this question user need to specify full file path e.g. /opt/databases/demo/file.abc I`m looking for some way to have interactive shell just like in linux shell - is there any way to have autoexpanded paths after tab pressing?
In Linux we just type e.g /opt/dat , pressing TAB and it prompt us the full path /opt/databases/, but when it is typed under running Perl script the TAB don`t work.
I`ve tried a bit with Term::ReadLine but i didn`t give me what I need.
The Term:ReadLine way:
$| = 1; use Term::ReadLine; my $Term = new Term::ReadLine 'Installer'; my $file = $Term->readline('Enter a file path: '); print "$file = '$file'n";

Will be nice to have it working in Linux and ActivePerl on Windows - all of used by me modules works on both Win and Lin platforms so i think some module will be the best way.

Replies are listed 'Best First'.
Re: Active STDIN for user typed filepath
by temporal (Pilgrim) on Mar 29, 2012 at 15:49 UTC
    I suppose you could have an input loop that would be checking to see if you've typed a '\t', which would then iterate through your opendir list with a little regex for filtering what you've already got... Probably use Term::ReadKey.
    #! /usr/bin/perl -w use Term::ReadKey; # make STDOUT hot $| = 1; $string = ''; $done = 0; # get your dir listing opendir($dh, '.'); @dir = sort readdir($dh); closedir $dh; # input loop while (!$done) { # read a key while (defined ($key = ReadKey(-1))) { # if it's a tab, get the next thing in the directory if ($key =~ /\t/) { # treat dir as circular list $cur = shift(@dir); # 'clear' the input print "\b" x (length($string)); print " " x (length($string)); print "\b" x (length($string)); $string = $cur; push(@dir, $cur); } # return means we're done elsif ($key =~ /\r/) { $done = 1; last; } # otherwise add to input string else { $string .= $key; # don't forget to clear to keep things looking normal print "\b" x (length($string) - 1); } print $string; } } print "\nyou have selected: $string\n";
    Yeah, it's a little funky. But I'm pretty sure it meets your requirements =D
Re: Active STDIN for user typed filepath
by Anonymous Monk on Mar 29, 2012 at 19:10 UTC
Re: Active STDIN for user typed filepath
by furry_marmot (Pilgrim) on Mar 30, 2012 at 06:11 UTC
    Works fine for me. It's not in Perl, it's in the shell. I've been doing this with Windows since Win95. Edit your registry and add the following:
    [HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Command Processor] "CompletionChar"=dword:00000009 "EnableExtensions"=dword:00000001 "PathCompletionChar"=dword:00000009

    CompletionChar refers to a file. PathCompletionChar refers to a path (duh...). 9 is the ASCII code for <tab>. Change as you like. Enabling Extensions gives you some additional shell functionality. It is limited compared to bash, etc., but the shell can be made to be bearable.

    You can save that block of text to a file with a .reg extension and just run it. If you have admin rights on your box, it will update the registry. Then run this and see if it works.

    perl -e "$x = <STDIN>; print $x"

    Press tab at the command prompt and it will cycle through files and paths.

    --marmot
Re: Active STDIN for user typed filepath
by Anonymous Monk on Mar 30, 2012 at 01:49 UTC