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

hi,

here is the deal. :

use strict; my $r; until($r =~ /yea/i){ print "type in something:\n"; chomp(my $i = <>); print "you typed $i , is this correct? [yea or ney]\n"; chomp($r = <>); if ($r =~ /yea/i){ print "yuuuuuupiiiiiiiii !!\n"; } else { print "dough !!!\n" } }
the output of the script isn't the focus here, but the way, is what interests me. so when you run the script it asks you to type something , then you type it in then it asks you to confirm the thing you typed (yea or ney), and here is the question.

on ms-dos if you hit the up arrow key it displays the thing you typed in second ago, when it asks you to type in something, and on the linux machine when you do the same thing you get

^[[A^[[A^[[A^[[A^[[B^[[B

i suppose you understand what this is (up,up,up,up,down,down). this is very unpractical if you are running some interactive cmd prompt application, because if you did something wrong on the first character then you have to delete all the characters from the end of the command, to that first letter to correct it .

so is there a way to make this script run as on the windows machine, so it is possible to go up down right and left, but just in that aspect.

Replies are listed 'Best First'.
Re: windows linux and perl
by kyle (Abbot) on Dec 09, 2008 at 19:29 UTC

    I think Term::ReadLine will give you history and line editing capabilities, but I haven't used it myself.

Re: windows linux and perl
by oko1 (Deacon) on Dec 09, 2008 at 20:29 UTC

    As far as I know, that's only a CLI behavior in both Windows and Linux - and the input line of a Perl script is not a C(ommand)L(ine)I(nterface). As kyle mentioned, Term::ReadLine will provide input history and other features; it's a pretty capable module.

    Here's an example of how you might do something like what you're looking for:

    #!/usr/bin/perl -w use strict; use Term::ReadLine; my $term = new Term::ReadLine 'Yes/No'; my $OUT = $term -> OUT || \*STDOUT; while ( my $answer = $term -> readline( "Yea or Nay? " ) ) { print $answer =~ /^yea$/i ? "Yup.\n" : "Dough!\n"; }

    --
    "Language shapes the way we think, and determines what we can think about."
    -- B. L. Whorf
Re: windows linux and perl
by kennethk (Abbot) on Dec 09, 2008 at 19:26 UTC

    Sure. Since you are working in what amounts to a perl shell, it's just a SMOP to include that functionality in your script.

    I'm actually kinda surprised you get that behavior from the MS command prompt.