|Problems? Is your data what you think it is?|
Refactoring old modules to use rolesby Anonymous Monk
|on Aug 13, 2015 at 19:47 UTC||Need Help??|
Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:
Is there any way to reuse roles in packages that are not object-oriented?
I'm working with a large, old web application codebase and trying to integrate it with modern Moo-based (Dancer2) code. I'm having some issues with a particular package, Utility.pm, in the old code; it stores some important variables (the CGI, session, database handles), and is used by just about every other module as it contains everything from file system manipulation to CGI parameter access to print formatting to making cups of tea for the programmer (OK, not yet...). I would like to refactor some of the functionality, and ideally reuse some of the roles from Moo code -- e.g. session and cookie handling, query parameter access, etc. I don't know if this would be possible, given that Utility.pm is written as a set of functions with a few package variables, rather than as an object.
Here is an overview of the code that I'm working with. MyApp does various set up tasks, finds the appropriate module to load based on http params, then loads and runs the code:
ConsumingModule.pm uses Utility.pm, which looks like this:
Utility.pm exports all its functions by default, and modules that use it look like this:
As I understand it, roles can only be applied to objects or classes; I'm wondering if there is any way that I can refactor the existing Utility.pm code to enable me to use roles. There are 100+ modules that rely on Utility.pm and no test suite, so changing its API is going to be tricky at best and suicidal at worst. ;-)