I just wrote a function for operating seperate "command line histories" in Tk entries (generically). Since I'm chuffed to bits about it and will now be using it in most of my tk apps, I wanted to simplify it's inclusion the way you would #include a header in a C program. Does that mean I have to turn it into a module or something? Where should I start researching this?
For posterity's sake (and, of course, to invite some healthy criticism, etc) here's the code/example:
#!/usr/bin/perl -w
use strict;
use Tk;
my $MW = MainWindow->new();
my $entry1 = $MW->Entry;
my $enhist = history(\$entry1,"new");
$entry1->bind("<Key-Return>"=>sub{history(\$entry1,"add", $enhist)});
$entry1->bind("<Key-Up>"=>sub{history(\$entry1,"up", $enhist)});
$entry1->bind("<Key-Down>"=>sub{history(\$entry1,"down", $enhist)});
my $entry2 = $MW->Entry;
my $enhist2 = history(\$entry2,"new");
$entry2->bind("<Key-Return>"=>sub{history(\$entry2,"add", $enhist2)});
$entry2->bind("<Key-Up>"=>sub{history(\$entry2,"up", $enhist2)});
$entry2->bind("<Key-Down>"=>sub{history(\$entry2,"down", $enhist2)});
##GRID##
$entry1->grid(-row=>0,-column=>0);
$entry2->grid(-row=>2,-column=>0);
MainLoop;##################
sub history {
my $entry = shift;
my $cmand = shift; my $hist;
if ($cmand eq "new") {
my %hash;
my @list;
$hash{hlist} = \@list;
$hash{place} = 0;
my $ref = \%hash;
return $ref;
} else {$hist = shift;}
if ($cmand eq "add") {
my $string = $$entry->get;
$$entry->delete('0','end');
push(@{$hist->{hlist}},$string);
$hist->{place}++;
} elsif ($cmand eq "up") {
if ($hist->{place} <= 0) {$hist->{place} = $#{$hist->{hlist}}}
else {$hist->{place}--};
$$entry->delete('0','end');
$$entry->insert('1',$hist->{hlist}[$hist->{place}]);
} elsif ($cmand eq "down") {
$$entry->delete('0','end');
if ($hist->{place} == $#{$hist->{hlist}}) {
$hist->{place} = -1;
return}
$hist->{place}++;
$$entry->insert('1',$hist->{hlist}[$hist->{place}]);
}
}
history() is the function in question (what else)
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.