in reply to Simulating input history without Term::Readline::Gnu

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

Replies are listed 'Best First'.
Re^2: Simulating input history without Term::Readline::Gnu
by ackerleytng (Initiate) on Jul 16, 2013 at 19:02 UTC
    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.