I thought of creating a hash of closures, but I figured all of the closures would have to have the same aggravating argument copying at the top and decided it would be too wordy, e.g.:# return a list consisting of correct division and service # as a function of $dcr_type and @row sub division_service { my ($dcr_type, @row) = ( uc $_[0], @{$_[1]} ); return ('HQ' , undef) if $dcr_type eq 'CONTACT' ; if ($dcr_type eq 'FAQ') { return (undef, $row[2] eq 'Service' ? $row[3] : $row[4]); } return (undef, $row[4]) if ($dcr_type eq 'LITERATURE') ; if ($dcr_type eq 'ORGANIZATION') { my @ret = ($row[2], undef); $ret[0] = 'HQ' if ($row[0] eq 'Headquarters'); return @ret; } }
Note how the closure for FAQ has to do argument assignment? I think it would be wasteful to write that over and over in each closure. Of course division_service would be more compact:my %division_service = ( CONTACT => sub { return ('HQ', undef) }, FAQ => sub { my (@row) = @_; if ($dcr_type eq 'FAQ') { return (undef, $row[2] eq 'Service' ? $row[3] : $row[4]); } } }
So what I was wondering was if there was a way (without source filtering, which is too terrifying for me) to say each of these closures will be using the same arguments, pass them.sub division_service { my ($dcr_type, @row) = ( uc $_[0], @{$_[1]} ); $division_service{$dcr_type}->(@row); }
One way is to create a small class and have FAQ, ORGANIZATION, etc be methods and have the hash populated with the appropriate data. Then no arguments would have to be passed.
In reply to Rethinking Your Program's If-Thens by princepawn
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |