Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Re: looking for a good idiom: return this if this is true

by mstone (Deacon)
on Mar 05, 2005 at 22:08 UTC ( [id://436964]=note: print w/replies, xml ) Need Help??


in reply to looking for a good idiom: return this if this is true

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]); }

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://436964]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others learning in the Monastery: (5)
As of 2024-04-25 10:24 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found