Welcome to the Monastery | |
PerlMonks |
Re: Perl OOP Function Entranceby zakame (Pilgrim) |
on Aug 29, 2017 at 03:05 UTC ( [id://1198233]=note: print w/replies, xml ) | Need Help?? |
At one point during my development of Hashids for Perl I thought about something like this, especially for some utility methods that I ported from the original JavaScript source. On one hand, there's a "seductive" thought of making all things OO-ish for a "consistent" interface, but ultimately I found that the utility methods were, after all, utility, and could be plausibly used out of the Hashids object context. So in the end, I decided to make a Hashids::Util module that implements subs only, not methods; just add a bit of Exporter magic to provide the subs on by-name basis (or use Hashids::Util ':all' if you want them wholesale) and namespace::clean on both the module and requestors so that those subs don't pollute the namespace. Here's a basic template for such a Util module that you might want to use: I suspect that another possible way for you would be to go implement those utility methods in a Role, and compose this Role in to your other classes; that might be a better fit for your situation, as you mention in another reply ("The base ideas was to create a class 'Object' which all classes inherit from.") We could turn the example above into something like this (assuming using Moo for OO, change needed if its something else:)
Then in your classes you just add with 'Class::Util';. EDIT: I realized you can actually combine the two approaches above rather easily (though at a cost of a bit of conceptual overhead.) Here's one rather naive approach:
Now you can have a regular Util module for standalone subs, and a Class::Util role for objects.
In Section
Seekers of Perl Wisdom
|
|