Perl函数参数消失.为什么?

Pyp*_*ros 2 perl undefined function-calls matching

我正在Perl中建立一个压力系统来纠正学生的编程任务.我建立了一个check(<boolean>,<congrats-message>,<blame-message>)应该帮助我的功能.

当我调用它时

check(get_option('content-type') eq "text/html","good type", "bad type");
Run Code Online (Sandbox Code Playgroud)

一切都好.但如果我敢这么做的话

check(get_option('content-type') =~ m:text/html:i, "good type", "bad type");
Run Code Online (Sandbox Code Playgroud)

当正则表达式找不到匹配时它会中断.实际上它等同于a check("good type", "bad type").我只用$_[0],$_[1]等在检查()函数,它不喜欢我了"民主基金"作为第一个参数:我真的可以赶上与错误die unless $#_ == 2.

发生了什么 ?我一起工作

check((get_option('content-type') =~ m:text/html:i && 1), "good type", "bad type");
Run Code Online (Sandbox Code Playgroud)

但我很想知道这种奇怪情况的原因和原因.

-

sub check {
  if ($_[0]) {
    $okay++;
    print STDERR "^_^ $_[1] ($okay)\n";
  } else {
    print STDERR ">_< $_[2]\n";
  }
  return $_[0];
}
Run Code Online (Sandbox Code Playgroud)

Que*_*tin 6

eq始终返回true或false,但=~(在列表上下文中时)返回"由模式中括号匹配的子表达式组成的列表".

当您将列表放在另一个列表中时(例如,子例程的参数列表中的匹配列表),它们将合并.

如果有0个匹配项,则会在列表中获得另外2个参数.

如果有2个匹配,则获得这2个匹配,然后获得2个其他参数,总共4个参数.

您的代码取决于正好有三个参数.