For what it's worth, Term::Readline is a perl frontend to the C library, and readline is
definitely the lib that's used by bash (the linux shell) for command history. However, perl modules of this sort do not always provide the same level of functionality (sometimes more, sometimes less) IMO.
In any case, you don't really need to resort to that. I implemented a command history for a perl/Tk app I wrote about 3 years ago. I would do this somewhat differently now, but it's worth noting that this is the source from that and I still use this app on a near daily basis.
A weird and unique thing: this is how I was introduced to OOP (here at perlmonks! ...not bothering to look for thread). If you look at this "function", it's pretty obviously a "class". I know there is some object notation in it, I didn't know that's what I was doing (I understood references). Some perlmonks reg pointed out this should be a package.
Anyway, like I said it still works so is probably a decent source of
ideas (ie, please don't blast me for style :lol:)
sub tkhistory {
my $entry = shift;
if ($entry == 0) { # an "unattached" new history
my %hash;
my @list;
$hash{hlist} = \@list;
my $ref = \%hash;
return $ref;}
my $cmand = shift; my $hist;
if ($cmand eq "new") {
my %hash;
my @list;
$hash{hlist} = \@list;
$hash{$entry} = -1;
my $ref = \%hash;
return $ref;
} else {$hist = shift;}
if ($cmand eq "add") {
my $string = $$entry->get;
foreach my $elem (@{$hist->{hlist}}) { # prevent duplic
+ates
if ($string eq $elem) {return 0}}
push(@{$hist->{hlist}},$string);
$hist->{$entry} == $#{$hist->{hlist}};
} elsif ($cmand eq "up") {
if ($#{$hist->{hlist}} == -1) {return}
if ($hist->{$entry} < 0) {$hist->{$entry} = $#{$hist->{hlist}}
+}
else {$hist->{$entry}--};
$$entry->delete('0','end');
$$entry->insert('1',$hist->{hlist}[$hist->{$entry}]);
} elsif ($cmand eq "down") {
$$entry->delete('0','end');
if ($#{$hist->{hlist}} == -1) {return}
if ($hist->{$entry} == $#{$hist->{hlist}}) {
$hist->{$entry} = -1;
return}
$hist->{$entry}++;
$$entry->insert('1',$hist->{hlist}[$hist->{$entry}]);
} elsif ($cmand eq "join") {
$hist->{$entry} = 0;
}
}
I believe "up" and "down" are linked to the key callbacks for various tk entries. "add" is probably linked to the enter key.
The application it's from is here:
http://tkcodex.sourceforge.net/
I mostly use it on C/C++ source trees now.
Good luck!
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.