|Keep It Simple, Stupid|
module w/ object-oriented and functional interfaces: best practices?by temporal (Pilgrim)
|on Sep 21, 2012 at 16:27 UTC||Need Help??|
temporal has asked for the wisdom of the Perl Monks concerning the following question:
When I write modules for Perl they typically follow a functional style of programming. I learned Perl from a functional point of view and I feel like Perl best lends itself to this paradigm. Certainly not saying that Perl can't do OO (I have written some OO code in Perl before, enough to know the basics), but just saying this now so that you know where I'm coming from.
That said, I have been running into situations where I want to access some modules in a more OO style. Or extend them so that I can leverage some of the benefits of OO. I'd like to keep all their functional components in place but add on this OO interface as well.
Dug up this post: Module provides both of functional interface and object-oriented one, which was a good read but didn't seem to come to any real conclusions. I know there are some great modules on CPAN that implement this sort of dual interface. The ones that I've code dived look like they go ahead and write separate functional routines which can be exported should the user want a functional interface.
Since this is primarily retrofitting some already written modules I'd rather not have to rewrite all of my functions. So least effort would be a nice plus but it doesn't outweigh keeping things 'best practice'.
My initial thought was to add in OO and then my already written functions can determine intent based on their arguments. My second thought was to just spend a bit more time on this and make everything primarily OO and fake the functional side by having them create self-objects every time they are called functionally. Probably a significant performance hit there for functional implementers.
One final note: I am aware of Moose et al. Seem very handy, but I have not taken the time to learn them. I want to avoid using these until I have a solid grasp of how to implement OO in Perl by hand. So in a way I am approaching this as a bit of a learning experience as well. Besides, I am already fluent in other OO languages that look and feel like what Moose-ish modules do to Perl.
Bit long-winded but I hope that I got my situation and questions across. Any ideas/recommendations/links/rants on best practices for implementing OO + functional interfaces for modules?