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

Hi!

I wish to read user input and the problem that I was facing was that pressing almost anything non-text on the keyboard produces these escape sequences on screen ^[[A etc that were interfering with my output.

To get around that, I used Term::Readline. Now I wish to maintain and access a history of past inputs. I read this post at http://stackoverflow.com/questions/14671797/how-to-use-termreadline-to-retrieve-command-history, which suggests that I install Term::Readline::Gnu.

Unfortunately, I don't have root access and so I can't install that module. I still keep getting ^[[A etc on my output. Any way to get the same functionality without installing stuff?

  • Comment on Simulating input history without Term::Readline::Gnu

Replies are listed 'Best First'.
Re: Simulating input history without Term::Readline::Gnu
by mtmcc (Hermit) on Jul 15, 2013 at 18:58 UTC
    What code are you using to read user input? Using <STDIN>, usually only the arrow characters give ^[[A on my system.

    The modules mentioned above can be useful, and Term::UI::History might be worth looking at.

    A simple method of recording history might be to just push the input to an array, for example:

    #!/usr/bin/perl use strict; use warnings; my @history; my $text; my $x; print STDERR "Enter text ('q' to quit): "; $text = <STDIN>; chomp $text; push (@history, "$text\n"); while ($text ne 'q') { print STDERR "Enter text ('q' to quit): "; $text = <STDIN>; chomp $text; push (@history, "$text\n"); } my $numberOfHistoryItems = @history; print STDERR "For history, press 'h', else quit:"; my $history = <STDIN>; chomp $history; if ($history eq "h") { for($x = 0; $x < $numberOfHistoryItems; $x += 1) { print STDOUT "$history[$x]" if $numberOfHistoryItems > +0; } print STDOUT "No history items\n" if $numberOfHistoryItems == +0; }

    Although admittedly, that might get tedious and unwieldy if your script is long.

    -Michael
      Yes, those characters appear when I press the arrow keys. I'll look into Term::UI::History, thanks! Your idea is great, but I wish I could do something like that without requiring the user to press enter before the input is interpreted. Ideally, it should work like how a terminal works.
Re: Simulating input history without Term::Readline::Gnu
by rjt (Curate) on Jul 15, 2013 at 18:10 UTC
      Thanks! I'll look into this. It's mostly that I don't have write access to those library folders and I was hoping to just get around it. Besides, in a testing environment on many systems, it's much easier to have my scripts portable and transferable onto the existing framework than to have to install CPAN modules every time I need to run a script...