Learned monks,

Suppose that I want to adapt an existing class to my immediate needs, by adding some methods, right in my quick and not-too-dirty script.

I can see two ways, illustrated below, with Tk::HList::addrow() and updaterow().

Please comment on pros and cons, and on other possible ways to achieve this.

Rudif

#!/usr/bin/perl use strict; use Tk; use Tk::Label; use Tk::HList; sub Tk::HList::addrow { my ( $self, $row, $text ) = @_; $self->add($row); $self->itemCreate( $row, 0, -text => $text ); } package Tk::HList; sub updaterow { my ( $self, $row, $text ) = @_; $self->itemConfigure( $row, 0, -text => $text ); } package main; my @items; my $selectedrow = 0; my @text = qw ( AAA BBB CCC DDD EEE FFF ); my $mw = MainWindow->new(); my $frm1 = $mw->Frame()->pack( -side => 'top', -anchor => 'w' ); my $frm2 = $mw->Frame()->pack( -side => 'top', -anchor => 'w' ); my $but1 = $frm1->Button( -text => "reverse", -command => \&revers, -width => 20, )->pack( -side => 'left' ); my $label = $frm2->Label( -width => 5, -text => $selectedrow )->pack( -side => 'left' ); my $but2 = $frm2->Button( -text => "incr", -command => \&incr, -width => 15, )->pack( -side => 'left' ); my $hlist = $mw->HList( -itemtype => 'text', -height => 20, -separator => '/', -selectmode => 'single', -browsecmd => sub { $selectedrow = shift; $label->configure( -text => $selectedrow ); print "$selectedrow\n"; } )->pack; add(); MainLoop; sub add { foreach my $i ( 0 .. $#text ) { $hlist->addrow( $i, $text[$i] ); push @items, $i; } } sub refresh { foreach my $i ( 0 .. $#text ) { $hlist->updaterow( $i, $text[$i] ); } } sub incr { ++$text[$selectedrow]; refresh(); } sub revers { @text = reverse @text; refresh(); }

In reply to Reopen a class, add methods ? by Rudif

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.