in reply to abstraction - reusing OO module subs

I suppose you could write a generalized sub that utilizes either an extra parameter to name the the key/array you want to push to, or the calling sub's information available via caller. However, unless there's some important behavior in column and table that you haven't described that would end up in _put_list, I don't really see what the fuss is about. I'd recommend simply streamlining what you've got by dropping the for loop. What's wrong with push(@{$self->{table}}, @_)? This small amount of redundant code is better than a trivial subroutine.

Also, if you mean to use this in an OO manner, exporting your subroutines is not necessary. And the way they're written they expect to be called as methods, not functions.

Replies are listed 'Best First'.
Re: Re: abstraction - reusing OO module subs
by fireartist (Chaplain) on Nov 21, 2002 at 09:57 UTC
    Thanks for your reply.
    Ok, I can see that there would be little benefit, especially when my for loop is reduced to a single line.
    The reason I has wanted to do it was because there's not just 2 subs that do this, but many. There's also another group of subs that do another similar function, again where only the variable name changes.
    However, I'm happy with the sub reduced as it has been now.

    ...And the way they're written they expect to be called as methods, not functions.
    Could you explain this please?
    I'm still working my way (repeatedly) through the perltoot, perltooc tutorials, and only slowly learning this 'OO' thang.
    Many thanks.

      It is quite common in OO programming to have the sort of redundancy among methods that you're worried about. It's a natural consequence of the emphasis on encapsulation that you end up with many similar getters and setters (or accessors/mutators) to mediate interaction with variables. To alleviate the tedium some people do things such as write modules like Class::MethodMaker to automate the process.

      As for functions vs methods, the main difference is how they are called. A function, once defined or imported into the current namespace, may be called directly by name. In contrast, a method must be called on either a package name or an instance of the package. See Re: Perl Prototypes for a fuller explanation.