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.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Rethinking Your Program's If-Thens
by Steve_p (Priest) on Oct 11, 2002 at 23:16 UTC | |
by runrig (Abbot) on Oct 12, 2002 at 00:28 UTC | |
by Steve_p (Priest) on Oct 12, 2002 at 16:05 UTC | |
|
Re: Rethinking Your Program's If-Thens
by Aristotle (Chancellor) on Oct 12, 2002 at 13:56 UTC | |
by Anonymous Monk on Oct 12, 2002 at 17:31 UTC | |
by Aristotle (Chancellor) on Oct 12, 2002 at 18:15 UTC | |
by Anonymous Monk on Oct 12, 2002 at 19:06 UTC | |
|
(jeffa) Re: Rethinking Your Program's If-Thens
by jeffa (Bishop) on Oct 12, 2002 at 16:25 UTC | |
|
Re: Rethinking Your Program's If-Thens
by Abigail-II (Bishop) on Oct 14, 2002 at 12:25 UTC | |
|
Re: Rethinking Your Program's If-Thens
by rir (Vicar) on Oct 12, 2002 at 02:01 UTC |