First of all, it helps to characterize the problem: you're building a very simple state machine. Each called subroutine is a new state, and the return value from each called function is the input which selects the next transition. In this case, you want FALSE to trigger a transition to the next state, and TRUE to trigger a transition to the accept state:

(s0)- F ->(s1)- F ->(s2)-[...] | | | +---------+---------+--[...]- T ->(accept)

In this case, you can implement that with a list of function pointers and a loop:

sub outer { my @funcs = (\&f1, \&f2, \&f3, ...); my $result = 0; for $f (@funcs) { $result = $f->(); last if $result; } return ($result); }

And more generally, you can use a hash as a transition table:

sub outer { %trans = _get_transition_table(); my $f = $trans{'start'}; my $result = 0; do { $result = $f1->(); my $state = ($result) ? "$f.true" : "$f.false"; $f = $trans{ $state }; } while ($f); return ($result); } sub _get_transition_table { my $f1 = \&f1; my $f2 = \&f2; my $f3 = \&f3; [...] return ( "start" => $f1, "$f1.true" => 0, "$f1.false" => $f2, [...] ); }

which allows for multiple output values per function, and state diagrams that are more complicated than just a simple list.

If you want to pass parameters to the functions, pass a listref and have the called functions and use it as an argument list, a stack, a queue, or whatever:

sub caller { [yadda yadda] my $args = []; do { $result = $f->($args); [yadda yadda] } while ($f); return ($result); } sub as_list { my $args = shift; my ($arg1, $arg2, $arg3) = @$args; @$args = (); [...] } sub as_stack { my $args = shift; my $item = pop @$args; [...] push @args, $result; return ([true|false]); } sub as_queue { my $args = shift; my $item = shift @$args; [...] push @args, $result; return ([true|false]); }

In reply to Re: looking for a good idiom: return this if this is true by mstone
in thread looking for a good idiom: return this if this is true by merlyn

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.