in reply to Simulating Command Line History in Perl

Are you on windows by any chance? Cos Term::ReadLine has never worked for me either. Whether my attempts to use it or when it is embedded in things--like the cpan shell--written by people who should know what they are doing with it.

That said, I find that the windows built-in command line history is sufficient for my needs. Albeit not persistant. If I need to recall commands over more than one re-boot, I put them into a cmd files anyway.


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.
RIP an inspiration; A true Folk's Guy
  • Comment on Re: Simulating Command Line History in Perl

Replies are listed 'Best First'.
Re^2: Simulating Command Line History in Perl
by repellent (Priest) on Apr 24, 2010 at 08:21 UTC
    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;

      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.

      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?