perlpal has asked for the wisdom of the Perl Monks concerning the following question:

Hi, Im writing a generic code for a control module. This module calls different modules and depending on the return values (0 or 1) decides whether to continue to the next module or gracefully exit. Inputs/Advice/Code would be greatly appreciated! Thanks in advance!

Replies are listed 'Best First'.
Re: Code for Control Module
by tilly (Archbishop) on Jan 15, 2009 at 10:24 UTC
    Doing this with separate modules would not be the normal way to go in Perl. For one thing by tradition a module that returns 0 raises an exception. Instead I would suggest that you call a series of functions. That could be coded as simply as this:
    sub do_each_until_false { for (@_) { return 0 unless $_->(); } return 1; }
    and then called like this:
    if ( do_each_until_false( \&function1, \&function2, sub {require Foo; Foo::bar()}, \&function3, # etc. ) ) { print "All done\n"; } else { print "FAILED\n"; }
    However this is not really a significant win over just writing straight line code using a bare block:
    my $is_success = 0; BLOCK: { function1() or last BLOCK; function2() or last BLOCK; require Foo; Foo::bar() or last BLOCK; function3() or last BLOCK; # etc; $is_success = 1; } if ($is_success) { print "All done\n"; } else { print "FAILED\n"; }
    Particularly if you discover that some of your functions need to pass parameters in and out, or sometimes you need to store information from one function call to the next.
Re: Code for Control Module
by Anonymous Monk on Jan 15, 2009 at 10:14 UTC
    Stop and think about what problem you're trying to solve, what you're hoping to accomplish, and how. Then communicate your ideas to someone else, then refine your ideas (repeat this until you're satisfied with the plan). Then if your plan makes sense, write some code.