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

Hi, I'd like to prompt a user for a filename and would like to show a default filename that he/she can modify. What I want is something like this:
Please enter a filename: c:\test.txt
I'd like for the user to be able to change the "c:\test.txt" filename if he wants. Any help would be much appreciated.

Replies are listed 'Best First'.
Re: printing deletable text to stdout
by bpphillips (Friar) on Oct 04, 2004 at 19:38 UTC
    I've used Term::Clui several times and it makes adding an interactive prompt to your script a trivial matter (ask takes two arguements, the first being the question and the second being the default, which is editable by the user):
    use Term::Clui; my $filename = ask("Please enter a filename:","C:\test.txt");
    It also provides several other functions like confirm which automatically interprets a y/n response for you as well as a choose($question,@list) chooser.

    I'm pretty sure it works on W32 but haven't used it there recently enough to know for sure.
      None of the solutions above that worked were really what I wanted. The problem with putting in those backsapce escape sequences is if the filename is long and scrolls down to the next line it is a pain. Also it doesn't allow me to modify the default.

      I am looking for something exactly like Term::Clui but that works on Win32...it didnt' seem to work for me on win32.

        What problems were you having with it? Could you not get it to even install or was it a problem using the package?
Re: printing deletable text to stdout
by TheEnigma (Pilgrim) on Oct 04, 2004 at 19:06 UTC
    This isn't the most elegant, maybe; but it should work if no one comes up with a better solution. Change the prompts to whatever suits you.

    print "Please enter a filename: c:\test.txt"; print "Hit enter to accept, or enter a different filename: "; my $filename = <STDIN> || 'c:\test.txt'; chomp($filename);

    TheEnigma

Re: printing deletable text to stdout
by kutsu (Priest) on Oct 04, 2004 at 19:09 UTC

    I used to do this in ksh all the time (Terminal programs), and it seems it works just as well in perl, I'm sure this isn't the best solution but it does work:

    $file1 = 'c:\file.txt'; print "Enter File Here: $file1\b\b\b\b\b\b\b\b"; chomp($file2 = <STDIN>); $file2 ? $file = $file2 : $file = $file1;

    "Cogito cogito ergo cogito sum - I think that I think, therefore I think that I am." Ambrose Bierce

      You appear to be assuming that $file1 is eight characters long. Wouldn't print "Enter File Here: $file1", "\b" x length $file1; be preferable?

      Also, the OP wanted the user to be able to compose his response by editing the default filename. That's not possible with this solution.

        I knew there was an easier way to print multiple characters, but I don't do command line work much and as such couldn't remember it. It is possible with my solution, esp. if the "\b" x length $file was added, not to edit the default filename but to decide if one was entered or not and therefor have a default filename. Though there have been much better solutions suggested and I would recommend he use one of those.

        "Cogito cogito ergo cogito sum - I think that I think, therefore I think that I am." Ambrose Bierce