in reply to Simulating Command Line History in Perl

I've looked at the Term::Readline::* modules, but none of them seem to do this part of the task.

They do — at least on *nix  (preferably install Term::ReadLine::Gnu, which has quite a few more features)

#!/usr/bin/perl use Term::ReadLine; my $term = new Term::ReadLine 'Demo'; my $prompt = 'Perl> '; while ( (my $cmd = $term->readline($prompt)) ne 'quit') { print "cmd: '$cmd'\n"; }

You can supply a second argument to readline() in case you want to suggest some default input (which can be edited).

Replies are listed 'Best First'.
Re^2: Simulating Command Line History in Perl
by Anonymous Monk on Apr 23, 2010 at 21:23 UTC
    I tried your code but it doesn't give the capabilities to do up and down for the history which is what the op wanted.

      Term::ReadLine will pick from a number of different backends depending on what's available. My custom built perl finds Term::ReadLine::Gnu and works just fine. My system perl, which I never touch, found Term::ReadLine::Stub which does not work correctly, because it's just a stub.

      Add this line to almut's code to see your backend.

      print "Using ", $term->ReadLine, "\n";

      Works for me.  I.e., type "foo" at the prompt, followed by <Enter>. New empty prompt appears. Hit cursor up, and "foo" is back for you to edit.  This is what I understood the OP wanted...

      If that doesn't work, you're likely either on Windows, or don't have Term::ReadLine::Gnu installed.

        It still doesn't work for me... I have Term::ReadLine::Gnu installed.
Re^2: Simulating Command Line History in Perl
by Only1KW (Sexton) on Apr 23, 2010 at 21:22 UTC
    Like I said, I understand that ReadLine can get the history...that's the easy part. What methods on, say, Term::ReadLine::Gnu, will put the output back on the screen for the user to edit?
      Nm, I see about the second argument now. It seems to be working now. Thanks.

      Just wish the ReadLine help text were more verbose about this.

        I see about the second argument now

        Just to be clear, the second argument is not for implementing a command history; it's for prompting with some default input as, for example, in install/configure scripts

        ... In which directory would you like the package to be installed? > /usr/local/foo

        where the editable input "/usr/local/foo" would appear at the prompt immediately, as a suggested default choice, without the user having to hit any cursor up/down keys etc.  Implementing a history via that feature would be quite cumbersome, as you'd have to read out the respective history entries yourself and present a new prompt for each entry...

        With GNU readline the history is basic functionality, which should work without further ado, if you have the right backend module installed.