views:

194

answers:

2

I'm writing a tool that is executed as a shell in Perl. I want it to have a history, so that if you press the up arrow, you go back to the previous command, just like bash or other shells. How should I go about this?

-- EDIT --

Thanks to daxim for pointing me towards Term::ReadLine::Gnu. I was able to get it to work on my Linux box and it's exactly what I was looking for. However, my main dev machine is running OS X. When I try to install the module, I get this error:

~/Downloads/Term-ReadLine-Gnu-1.19 > perl Makefile.PL 
Found `/usr/lib/libtermcap.dylib'.
gcc-4.2  -arch i386 -arch ppc -g -pipe -fno-common -DPERL_DARWIN -fno-strict-aliasing -I/usr/local/include -        DHAVE_STRING_H rlver.c -o rlver   -arch i386 -arch ppc -L/usr/local/lib -lreadline -ltermcap
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
The libreadline you are using is the libedit library.  Use the GNU Readline Library.
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

I am currently making my way through the Google results for this problem, but if anyone has seen this before and has pointers, that would be much appreciated. Thanks!

+3  A: 

Term::ReadLine::Gnu is the best module for this, Term::ReadLine::Perl the fallback.

daxim
The Term::ReadLine::Gnu module is licensed as per Perl, but the GNU readline library that it links to is GPL, so the whole thing is effectively GPL. If you plan on distributing your software, you will have to do so under the terms of the GPL if you use the GNU library. If this works for you, fine. If not, T::R::P is dual licensed, with Artistic/GPL.
daotoad
You overestimate the impact. In practice the vast majority of applications and modules do not redistribute the readline library, but merely point to T::R::G in the distribution meta file. That way it means if one plans on distributing one's software, one is not bound to the GNU licence and may pick something different.
daxim
@daxim, the issue is not distribution of the readline library, it is linking against it. But, if I accept your interpretation of the license terms, it seems to me that it still violates the spirit, if not the letter of the GPL. If I can't or won't share, then I shouldn't be using the resource. Those are pretty reasonable terms and I respect them.
daotoad
+1  A: 

Term::ReadLine interfaces to various readline packages, and as such will support history as you describe.

Brian Agnew
Are you aware you're linking to the Perl 5.005 version of the documentation?
daxim
Not anymore he isn't. :)
brian d foy
@brian d foy The link you posted is incorrect. Since it is part of Core. You should have used one of these: http://search.cpan.org/perldoc/Term::Readline or http://search.cpan.org/perldoc?Term::Readline
Brad Gilbert
Ah rats, thanks for fixing that. We could also link to perldoc.perl.org.
brian d foy