Yes, a module would be the way to go. You could go writing a generic Tk::History package, but if you want to deal just with entries, this is what a derived HistEntry package looks like, based on your post (and Tk::LabEntry):

package Tk::HistEntry; use vars qw($VERSION); $VERSION = '0.001'; use base qw(Tk::Frame); use Tk::widgets qw(Frame Entry); Construct Tk::Widget 'HistEntry'; sub Populate { require Tk::Entry; # HistEntry constructor. # my($cw, $args) = @_; $cw->SUPER::Populate($args); # Advertised subwidgets: entry. my $e = $cw->Entry(); $e->pack('-expand' => 1, '-fill' => 'both'); $cw->Advertise('entry' => $e ); $cw->ConfigSpecs(DEFAULT => [$e]); $cw->Delegates(DEFAULT => $e); $cw->AddScrollbars($e) if (exists $args->{-scrollbars}); $e->{history} = { hlist => [], place => 0, }; $e->bind("<Key-Return>"=> [ \&addHistory ]); $e->bind("<Key-Up>" => [ \&upHistory ]); $e->bind("<Key-Down>" => [ \&downHistory ]); } sub addHistory { my $entry = shift; my $hist = $entry->{history}; push @{$hist->{hlist}}, $entry->get() ; $hist->{place}++; } sub upHistory { my $entry = shift; my $hist = $entry->{history}; if ($hist->{place} <= 0) { $hist->{place} = $#{$hist->{hlist}} } else { $hist->{place}--; } $entry->delete('0','end'); $entry->insert('1',$hist->{hlist}[$hist->{place}]); } sub downHistory { my $entry = shift; my $hist = $entry->{history}; $entry->delete('0','end'); if ($hist->{place} == $#{$hist->{hlist}}) { $hist->{place} = -1; return } $hist->{place}++; $entry->insert('1',$hist->{hlist}[$hist->{place}]); } 1;

In your Code, you can now use HistEntry instead of Entry.


In reply to Re: What to do with my "Tk entry command-history" function by shmem
in thread What to do with my "Tk entry command-history" function by halfcountplus

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • 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:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.