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

Hi wise Monks,

I am pretty new to perl and here is what I am trying to do:

I want to prompt the user for the path of a file but I would like to give him a default editable value, for example I give him
C:/Data/toload.txt
as a default value and he can modify it directly, just changing the name of the file for example, without having to re-type the whole thing...

I have spent some time looking for an answer but no success so far so here I am, at your gates... but maybe I am asking for too much

Oh and yes, and I am stuck in Windows land...

Thanks for any help

Replies are listed 'Best First'.
Re: Simulate "editable" keyboard input
by shmem (Chancellor) on Dec 11, 2007 at 17:45 UTC
    For *nixes, there's Term::ReadLine and Term::ReadLine::Gnu for that, but you're on Windows... maybe Win32::Console serves as a starting point.

    --shmem

    _($_=" "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}
Re: Simulate "editable" keyboard input
by webfiend (Vicar) on Dec 11, 2007 at 22:31 UTC

    It doesn't do you much good unless you want to be real adventurous, but in Perl 5.10 (available as a beta download on ActiveState) you'll be able to say:

    use warnings; use strict; use 5.010; use Term::UI; use Term::ReadLine; my $term = Term::ReadLine->new(); my $filename = $term->get_reply( prompt => "Where is the file? ", default => "C:/Data/toload.txt", ); say "Filename: $filename";

    The beta is available for Windows, but whether to use it or not is completely up to you.

      Other than say, which is easily replaced by print, why does that need 5.10?

        Term::UI is listed as part of Core for 5.10, that's all. I just now had the silly realization that it could just be installed from CPAN as well.
Re: Simulate "editable" keyboard input
by moklevat (Priest) on Dec 11, 2007 at 18:58 UTC
    Update: Nothing really to see here. Please move along.

    Is this meant to be run from the command line in a console window, or over a web page? If it's the console, why not just prompt the user for input by showing the default value that will be accepted with a CR ? Is it that there may be a really long path to type in, and you want to save the user some keystrokes? I suppose I don't understand the need to provide an editable prompt, even though it does sound kind of cool.

Re: Simulate "editable" keyboard input
by Erez (Priest) on Dec 11, 2007 at 21:20 UTC

    I want to prompt the user for the path of a file but I would like to give him a default editable value, for example I give him C:/Data/toload.txt as a default value(...)just changing the name of the file for example, without having to re-type the whole thing...

    For that you can use something like

    print 'type your path [c:\data\toload.txt]: '; chomp (my $inputFile = <STDIN>); $inputFile ||= 'c:\data\toload.txt';
    or even just ask for the file name and append it to the path.

    and he can modify it directly,

    Well, that might need some Win32::Console-fu. The question is whether this is going to save any time; the user will still need to backspace it and type in something. Tab completion might help here, but I've no idea what gives you that on Win32.

    Software speaks in tongues of man; I debug, therefore I code.