in reply to Re: abstraction - reusing OO module subs
in thread abstraction - reusing OO module subs

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.

Replies are listed 'Best First'.
Re: Re: Re: abstraction - reusing OO module subs
by djantzen (Priest) on Nov 21, 2002 at 10:12 UTC

    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.