I have been thinking about writing an article called "Rethinking Your Program's If-Thens. With the idea being that often times when people useif-thens, they might have a more succinct way of handling the problem. For example, one might consider object-oriented polymorphism or AI::DecisionTree as options in certain cases. Now, here is a third case where I think if-thens should be rethought (I wrote this):
# 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; } }
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.:
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]); } } }
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:
sub division_service { my ($dcr_type, @row) = ( uc $_[0], @{$_[1]} ); $division_service{$dcr_type}->(@row); }
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.

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

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.