ユーザーからの入力を求める Perl サブルーチンがあります。入力された入力が有効な入力であるかどうか、そのサブルーチン自体の内部でチェックを実行します。
そうでない場合は、サブルーチンをもう一度呼び出して、今度はユーザーが有効な入力を入力できるようにします。
私のサブルーチンは次のとおりです。
sub some_routine {
print "Enter a number to select (1) Apple (2) Mango (3) grapes:"
$value=STDIN;
if($value =~ /[^1-3]/ ) {
print "The input is not valid!";
print "Do you want to continue selecting a fruit again (Y or N)?";
$choice = STDIN;
if( $choice eq "y") {
### I want to call the subroutine again to enter input ###
} else {
exit;
}
}
}
では、これでサブルーチンを再帰する方法は?