in reply to Reopen a class, add methods ?

Either way works fine, though I prefer not to have multiple packages in one file. But that's just a matter of style.

However, it seems to me that defining a derived class would be a more sensible approach. If you don't want to make an actual module, you can define the class inline with your script.

The following is untested but demonstrates the idea.

#!/usr/bin/perl use strict; use Tk; use Tk::Label; use Tk::HList; package Rudif::Tk::HList; @Rudif::Tk::HList::ISA = ( 'Tk::HList' ); sub addrow { my ( $self, $row, $text ) = @_; $self->add($row); $self->itemCreate( $row, 0, -text => $text ); } sub updaterow { my ( $self, $row, $text ) = @_; $self->itemConfigure( $row, 0, -text => $text ); } package main; # your stuff here

Replies are listed 'Best First'.
Re^2: Reopen a class, add methods ?
by Rudif (Hermit) on May 13, 2007 at 15:21 UTC
    friedo,

    Originally I did experiment with inheritance, but I got into trouble with the Tk framework when I tried to instantiate my derived class. It seems that Tk classes are somehow different from typical perl classes.

    This is why I started looking for an alternative along the lines of 'reopen a class, add a method'.

    Rudif