in reply to Re: Caveats when using anonymous subs as subroutine arguments
in thread Caveats when using anonymous subs as subroutine arguments

I like the idea of the common default arguments hash; that would make it easier to update if/when the defaults need to change. And as you suspected, the actual methods involved were not quite as simple. A major factor in deciding to go this route came from the 'if' block contents. Most of subroutines used the same chunk of code with small differences (if any); I put that work into it's own subroutine and passed it directly. Others do mostly the same work, with minor additions and changes. (A side note: there's actually now a common interface to using these methods (eg, DoTask( TASK_1, @args); ) which makes these individual names simply a relic of maintaining the interface.)

Since the setup is reused by maybe 80% of these methods and others use minor tweaks or additions, I'd want to specify that the same setup is used. That's easy to do with an individual setup method for each 'task', but that would create several new subroutines that simply call the common setup subroutine, leading me back to the same initial problem! I could pre-specify which specific setup to use in a constant, and then build the setup methods based on that string using 'can'; from there I'd simply look up a code reference. And in your example, that would happen at the time the module was 'use'd, making the lookup only happen once, right? Does perl resolve that reference right then, or does it still look it up every time the method is called? If it resolves it then, that seems pretty swanky.

I'd have to make several extra minor subroutines that only ever get called from one place for each of the 20% corner cases, though. That's what led me to think of creating a common setup subroutine for most and anonymous subs for the one off's, and pass them in via a code reference. If I understand it right, the way I did it, perl will have to reconstruct that anonymous sub every time that method is called. The ones that simply use a reference to the common setup wouldn't, though.

  • Comment on Re^2: Caveats when using anonymous subs as subroutine arguments