Perl 向用户提示多个问题(是/否和文件名输入)

Perl 向用户提示多个问题(是/否和文件名输入),perl,Perl,我想问用户多个问题。我有两类问题:是/否还是文件名输入。我不知道如何将这一切放到一个好的if结构中。我不确定我是否也应该使用“else”语句。有人能帮我们吗?这就是我到目前为止所做的: print "Do you want to import a list (Y/N)?"; # first question yes/no my $input = <STDIN>; chomp $input; if ($input =~ m/^[Y]$/i){ #match Y or y

我想问用户多个问题。我有两类问题:是/否还是文件名输入。我不知道如何将这一切放到一个好的
if
结构中。我不确定我是否也应该使用“else”语句。有人能帮我们吗?这就是我到目前为止所做的:

print "Do you want to import a list (Y/N)?"; # first question yes/no
my $input = <STDIN>;
chomp $input;
    if ($input =~ m/^[Y]$/i){ #match Y or y
        print "Give the name of the first list file:\n";
        my $list1 = <STDIN>; 
        chomp $list1;
        print "Do you want to import another gene list file (Y/N)?";
            if ($input =~ m/^[Y]$/i){
                 print "Give the name of the second list file:\n" # can I use $input or do I need to define another variable?;
                 $list2 = <STDIN>;
                 chomp $list2;
                 print "Do you want to import another gene list file (Y/N)?";
            }
    }
打印“是否要导入列表(Y/N)?”第一个问题是/否
我的$input=;
chomp$输入;
如果($input=~m/^[Y]$/i){#匹配Y或Y
打印“给出第一个列表文件的名称:\n”;
我的$list1=;
chomp$list1;
打印“是否要导入另一个基因列表文件(Y/N)”;
如果($input=~m/^[Y]$/i){
打印“给出第二个列表文件的名称:\n”#我可以使用$input还是需要定义另一个变量?;
$list2=;
chomp$list2;
打印“是否要导入另一个基因列表文件(Y/N)”;
}
}
一个词:抽象

您当前选择的解决方案不能很好地扩展,并且包含太多重复代码。我们将编写一个子例程
prompt
,它对我们隐藏了许多复杂性:

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;
}
我们现在可以用一种实际可行的方式编写代码:

if (prompt_yn("Do you want to import a list")){
    my $list1 = prompt("Give the name of the first list file:\n");
    if (prompt_yn("Do you want to import another gene list file")){
         my $list2 = prompt("Give the name of the second list file:\n");
         # if (prompt_yn("Do you want to import another gene list file")){
         # ...
    }
}
哦,看来你真的想要一个
while
循环:

if (prompt_yn("Do you want to import a list")){
    my @list = prompt("Give the name of the first list file:\n");
    while (prompt_yn("Do you want to import another gene list file")){
        push @list, prompt("Give the name of the next list file:\n");
    }
    ...; # do something with @list
}
@list
是一个数组。我们可以通过
push

附加元素。您可以使用子例程。 这有助于您在视觉上和逻辑上保持一切一致。 比如说


    &main();

    sub main {
        print "Do you want to import a list(Y/N)";
        my $input = ;
        chomp $input;
        if($input =~ m/^[Y]$/i) {
            &importfile();
        } elsif ($input =~ m/^[N]$/i) {
            print "you said no";
        } else {
           print "Invalid option";
        }
    }
    sub importfile
    {
        print "file name please ";
        my $file = STDIN;
        # import and process the file here.....
        &main();
    } 

因此,您可以通过这种方式导入多个文件。

不久前,我总结了以下内容:

#!/usr/bin/perl
use warnings;
use strict;
use Data::Dumper;


if (&prompt_yn("CONTINUE")){
  my @res = split(" ",&prompt("ENTER INPUT")) ;
  print Dumper @res;
}
else{
  print "EXIT\n";
}

sub prompt_yn{
  my ($query) = @_;
  $query = $query . " (Y/N): ";
  print "$query";
  while (<>) {
    $_ =~ s/^\s+|\s+$//g;
    $_ =~ s/\r|\n//g;
    if ($_ =~ /\S/){
      if ($_ =~ /^y$|^yes$/i){
        # if you want information message about entered value uncomment this
        # print "You have entered Y\n";
        return 1;
      }
      elsif ($_ =~ /^n$|^no$/i){
        # if you want information message about entered value uncomment this
        # print "You have entered N\n";
        return 0;
      }
      else{
        # if you want information message about entered value uncomment this
        # print "You have entered wrong value try again: \n";
      }
    }
    print "$query";
  }
}

sub prompt{
  my ($query) = @_;
  $query = $query . ": ";
  print "$query";
  while (<>) {
    $_ =~ s/^\s+|\s+$//g;
    $_ =~ s/\r|\n//g;
    if ($_ =~ /\S/){
      return $_;
    }
    print "$query";
  }
}
#/usr/bin/perl
使用警告;
严格使用;
使用数据::转储程序;
如果(&prompt_yn(“CONTINUE”)){
my@res=split(“,&提示符(“输入”);
打印转储程序@res;
}
否则{
打印“退出\n”;
}
子提示{
我的($query)=@;
$query=$query.“(是/否):”;
打印“$query”;
而(){
$\uz=~s/^\s+|\s+$//g;
$\uz=~s/\r\n//g;
如果($\ux=~/\S/){
如果($\=~/^y$\^yes$/i){
#如果需要有关输入值的信息消息,请取消对该消息的注释
#打印“您已输入Y\n”;
返回1;
}
elsif($\=~/^n$\^no$/i){
#如果需要有关输入值的信息消息,请取消对该消息的注释
#打印“您已输入N\N”;
返回0;
}
否则{
#如果需要有关输入值的信息消息,请取消对该消息的注释
#打印“您输入了错误的值,请重试:\n”;
}
}
打印“$query”;
}
}
子提示{
我的($query)=@;
$query=$query.“:”;
打印“$query”;
而(){
$\uz=~s/^\s+|\s+$//g;
$\uz=~s/\r\n//g;
如果($\ux=~/\S/){
返回$\;
}
打印“$query”;
}
}

与以前的解决方案相比,这可以处理空输入

你为什么到处使用
&&
。。。。。?这是我见过的最奇怪的事。normal语句以分号结束,而不是AND运算符。另外。。你的问题是什么?如何使用if-else?@TLP我认为&&是必要的,如果你想在彼此之后做多件事不,
&&
操作符只作为逻辑AND,而且它也会短路。见perldoc perlop。您可以将语句链接为if-else结构的形式,例如
/y/i&&print“包含y”
,但这是另外一种形式。@TLP i删除了
&
。现在我仍然不确定我是否可以像我那样把
if
语句放在这里&如果我应该在这里使用
else
,如果答案不是“Y”,那么程序应该继续运行,这确实使代码更干净。因为我是一个perl新手,所以我自己不可能想到这个。但是我在perldoc里查到了所有的东西&我现在明白你所有的命令了。好极了。我只是重复使用了你的代码。我稍微更改了
prompt_yn()
,允许接受默认值
sub-prompt_yn{my($query,$default)=@;my$default_yes=lc$default eq'y';my$yn=$default_yes?”[y/n]:“[y/n]”;my$answer=lc prompt($query$yn:);return$default_yes?!($answer=/^n/):$answer=~/^y/)
提示符
是否未内置?
#!/usr/bin/perl
use warnings;
use strict;
use Data::Dumper;


if (&prompt_yn("CONTINUE")){
  my @res = split(" ",&prompt("ENTER INPUT")) ;
  print Dumper @res;
}
else{
  print "EXIT\n";
}

sub prompt_yn{
  my ($query) = @_;
  $query = $query . " (Y/N): ";
  print "$query";
  while (<>) {
    $_ =~ s/^\s+|\s+$//g;
    $_ =~ s/\r|\n//g;
    if ($_ =~ /\S/){
      if ($_ =~ /^y$|^yes$/i){
        # if you want information message about entered value uncomment this
        # print "You have entered Y\n";
        return 1;
      }
      elsif ($_ =~ /^n$|^no$/i){
        # if you want information message about entered value uncomment this
        # print "You have entered N\n";
        return 0;
      }
      else{
        # if you want information message about entered value uncomment this
        # print "You have entered wrong value try again: \n";
      }
    }
    print "$query";
  }
}

sub prompt{
  my ($query) = @_;
  $query = $query . ": ";
  print "$query";
  while (<>) {
    $_ =~ s/^\s+|\s+$//g;
    $_ =~ s/\r|\n//g;
    if ($_ =~ /\S/){
      return $_;
    }
    print "$query";
  }
}