|Think about Loose Coupling|
Choosing between multiple closuresby oakb (Scribe)
|on Jun 12, 2014 at 07:22 UTC||Need Help??|
oakb has asked for the
wisdom of the Perl Monks concerning the following question:
I'm trying to build a large application with the main functionality abstracted out to subroutines. There are two ways of doing an important job, and four different structures against which the job may be run — meaning that there are eight possible job/structure combinations from which the program must choose, based on configuration settings gathered earlier. Also, each of the eight combinations needs to accept many arguments.
Each of the eight combination subroutines accepts a bunch of arguments, structures internal data, performs initial setup actions, and then returns an iterator in the form of an anonymous sub (a big shout-out and thank you to MJD!).
The problem lies in choosing the correct subroutine. I can get each of the subroutines to work by calling it directly from my main code, but every way I've tried to interject the selection process — including dispatch tables, switch statements, etc. — results in the anonymous subroutine (i.e. iterator) not being returned and a "strict refs" error based on calling a sub with an empty string as the only identifier; with "strict refs" turned off, it gives an undefined subroutine error.
Before you complain, yes, I know that this will print an infinite list of odd numbers; please rest assured that my actual application returns an exhaustible iterator.
This (and many other permutations of it I've tried) does not work, produces error "Can't use string ("") as a subroutine ref while "strict refs" in use at....":
How can I select the correct job/structure subroutine, send all necessary arguments, and get back a usable iterator?
This is my most desperate hour. Help me, Perl Monks. You're my only hope.