in reply to Re: Simulating Command Line History in Perl
in thread Simulating Command Line History in Perl

I've always gotten around the issue for MS consoles by switching the input filehandle to read from con.
use Term::ReadLine; my $cli = Term::ReadLine->new; if ($^O eq "MSWin32" || $^O eq "dos") { my $INPUT; open($INPUT, "<con") and $cli->newTTY($INPUT, $cli->OUT); } my $line1 = $cli->readline; my $line2 = $cli->readline; # hit up arrow for line1 history use Data::Dumper; print Dumper $line1, $line2;

Replies are listed 'Best First'.
Re^3: Simulating Command Line History in Perl
by BrowserUk (Patriarch) on Apr 24, 2010 at 09:13 UTC

    Sorry, but you are being fooled by the cmd.exe shell's history facility, into believing that Term::ReadLine is doing something. It isn't.

    The following is a completely new command shell where I type 'perl', hit enter, and then paste your snippet. When I get to your prompt I hit the up-arrow key twice. See what is displayed?

    Microsoft Windows [Version 6.0.6001] Copyright (c) 2006 Microsoft Corporation. All rights reserved. c:\>perl use Term::ReadLine; my $cli = Term::ReadLine->new; if ($^O eq "MSWin32" || $^O eq "dos") { my $INPUT; open($INPUT, "<con") and $cli->newTTY($INPUT, $cli->OUT); } my $line1 = $cli->readline; my $line2 = $cli->readline; # hit up arrow for line1 history use Data::Dumper; print Dumper $line1, $line2; ^Z INPUT> print Dumper $line1, $line2;

    Uparrow twice recalls the 2nd to last line (the last '^z' is displayed on the first uparrow), of input to the command shell. Term::ReadLine cannot possibly be caching this line as it hasn't started running when that is entered.

    You can get exactly the same recall without using Term::ReadLien at all. This is another new shell window--note the =comment/=cut lines:

    Microsoft Windows [Version 6.0.6001] Copyright (c) 2006 Microsoft Corporation. All rights reserved. c:\>perl =comment use Term::ReadLine; my $cli = Term::ReadLine->new; if ($^O eq "MSWin32" || $^O eq "dos") { my $INPUT; open($INPUT, "<con") and $cli->newTTY($INPUT, $cli->OUT); } my $line1 = $cli->readline; my $line2 = $cli->readline; # hit up arrow for line1 history use Data::Dumper; print Dumper $line1, $line2; =cut printf "PROMPT> "; <STDIN>; ^Z PROMPT> my $line2 = $cli->readline; # hit up arrow for line1 history

    Even though the executable part of the program consists of just printf "PROMPT> "; <STDIN>;, I can cycle back and forth through the entire text entered in this shell session.

    The first problem with Term::ReadLine (on windows) is that it doesn't use the arrow keys. You have to use some arcane ctrl-alt-meta-key combination to activate it. The second is that is does provide half the functionality of the shell facility.

    That's one of the reasons I disable readline support in cpan. It's disrespect for my current/preferred console settings is another


    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.
      I was aware that the history was coming from the console's shell facility, but my post was unclear in this regard :)

      I was merely suggesting a way for Term::ReadLine to co-exist (by yielding to con) in a Windows environment without obliterating all useful edit functions, while still being able to do its thing when executed in a Unix console.

      It's unfortunate that it messes with console settings like you pointed out. MS consoles' limited understanding of keycodes makes it that much harder to do cool shell stuff.
Re^3: Simulating Command Line History in Perl
by ikegami (Patriarch) on Apr 24, 2010 at 21:30 UTC

    I heard it can be fixed by setting the TERM env var.

    That said, command history now works for me cpan. Maybe it's been fixed? Maybe just in ActivePerl?