【发布时间】:2010-05-22 18:15:30
【问题描述】:
可能重复:
Is it possible for a Perl subroutine to force its caller to return?
我想编写一个子程序,使调用者在某些条件下返回。这旨在用作验证函数输入的快捷方式。到目前为止我所拥有的是:
sub needs($$) {
my ($condition, $message) = @_;
if (not $condition) {
print "$message\n";
# would like to return from the *parent* here
}
return $condition;
}
sub run_find {
my $arg = shift @_;
needs $arg, "arg required" or return;
needs exists $lang{$arg}, "No such language: $arg" or return;
# etc.
}
从needs 中的调用者返回的好处是避免在run_find 和类似函数中编写重复的or return。
【问题讨论】:
-
您确实意识到,正如您所写的那样,
needs将永远是return $condition,不是吗?