Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
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":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others lurking in the Monastery: (4)
As of 2024-04-16 16:32 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found