in reply to Re: Avoiding if/else knots
in thread Avoiding if/else knots

I have handled this problem before by having an array of pairs of functions - the first tests whether this is the right case, the other does it. You dispatch something like this:
for my $case (@cases) { if ($case->[0]->(@data)) { $case->[1]->(@data; last; } }
and then you just put whatever you want into @cases.

This is for more complex code cleanup. For instance suppose one might have a number of possibilities which need to be included only if some global condition is true. The standard if/elsif/else construct would require testing that condition in lots of elsifs. Lots of repeated code. This approach allows you to replace that with testing that condition once, then shoving a bunch of stuff into @cases if it is true.

Note that this is not a common need. It should not be a normal part of your toolbox. I think I've only resorted to the technique 2 or 3 times.