如何在Perl脚本中重复使用提示代码

时间:2015-05-04 09:07:00

标签: perl

sub prompt {
  my ($query) = @_; # take a prompt string as argument
  local $| = 1; # activate autoflush to immediately show the prompt
  print $query;
  chomp(my $answer = <STDIN>);
  return $answer;
}


sub prompt_yn {
  my ($query) = @_;
  my $answer = prompt("$query (Y/N): ");
  return lc($answer) eq 'y';
}


if (prompt_yn("Do you want to start a process")){
    my $list1 = prompt("Enter ID:\n");
    my $list2 = prompt("Enter Name:\n");

    print $list1;
    print $list2;
  ## $list1 and $list2 will be used in the code here..
  #...
  #.....
}

我想一次又一次地重复if块的内容,如果我从(Y/N)按y,则每次完成时再次提示。

1 个答案:

答案 0 :(得分:2)

替换

if (prompt_yn("Do you want to start a process")){

while (prompt_yn("Do you want to start a process")){