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. |