Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options

Perl-based command line history?

by xiper (Friar)
on Aug 14, 2003 at 23:53 UTC ( #284049=perlquestion: print w/replies, xml ) Need Help??

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

I would like to replicate the shell-like 'command line history' feature in a perl program, where you can use the up & down arrows to browse, edit, and execute past commands entered.

I have a command line based perl program that currently uses a basic prompt loop, something like:

while( 1 ) { print '> '; chomp( $cmd = <STDIN> ); &do_stuff( $cmd ); }
I realise this may turn out to be overly complex, but i would be interested to find out what would be required. I've had a quick look at Curses, although not sure it's what i'm after. Any pointers & examples would be greatly appreciated.

- ><iper

my JAPH: print"Just another Perl hacker\n"; # ^ look, no space! pretty tricky hey?

Replies are listed 'Best First'.
Re: Perl-based command line history?
by esh (Pilgrim) on Aug 15, 2003 at 00:03 UTC

    Check out Term::ReadLine.

    You should be able to steal from the sample code quite easily.

    Note: I don't know why, but on my setup (RH9.0, Perl 5.6.1) I can press UP once to get to the previous line but have to press UP two more times to get to the one before that. Everything else works like a charm.

    -- Eric Hammond

      *sigh* Is that all?!? Why's everything in perl have to be so simple?!? :)

      esh++ many thanks!

      Possible trap for future readers (who are probably brighter than i): you have to also have installed Term::ReadLine::Perl or, preferably, Term::ReadLine::Gnu (which in turn, needs the gnu readline library ).

      my JAPH: print"Just another Perl hacker\n"; # ^ look, no space! pretty tricky hey?

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://284049]
Approved by graff
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others surveying the Monastery: (4)
As of 2023-03-22 13:54 GMT
Find Nodes?
    Voting Booth?
    Which type of climate do you prefer to live in?

    Results (60 votes). Check out past polls.