I'll give it a shot....

my $checks = [ { test => sub { my $state = shift; my $tok = shift; if ($state->{checkPrefix}) { return $state->{base} = &driveBase($canon{$tok}, {%$Args, check +Prefix=>0})); } return; }, action => sub { my $state = shift; my $tok = shift; &equivWord($tok, $cannon{$tok}, {}); }, }, # Check number 2 { test => sub { ... }, action => sub { ... }, }, # and so on # Make your last test succeed (test => sub {1}), # and have your default logic go there. ]; while (my $tok = getTok(...)) { resetState($state); foreach my $check (@$checks) { if ($check->test($state, $tok)) { $check->action($state, $tok); last; } } }

What is nice, is that you can take this to the next level, and break some of your related tests into modules, and write the above in a manner that would allow those modules to register new checks (put $checks setup into registerCheck() and the loop into runChecks).

registerCheck(test => sub {...}, action => sub {...});

I have used this method for testing to see if lists of documents and attributes meet certain criteria, saving state for the entire run, and reporting not only on individual items, but the results of the entire run. The logic looks very similar to what I have above.

--MidLifeXis


In reply to Re^3: Really Long if/elsif/else blocks by MidLifeXis
in thread Really Long if/elsif/else blocks by throop

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.