If statement 单词';如果';解释为';if()';函数调用。不允许使用Parens

If statement 单词';如果';解释为';if()';函数调用。不允许使用Parens,if-statement,parentheses,raku,If Statement,Parentheses,Raku,因此,我发现在Perl 6中编写带有括号的if语句会导致它向我抛出以下错误: ===SORRY!=== Word 'if' interpreted as 'if()' function call; please use whitespace instead of parens at C:/test.p6:8 ------> if<HERE>(True) { Unexpected block in infix position (two terms in a row) at C:

因此,我发现在Perl 6中编写带有括号的if语句会导致它向我抛出以下错误:

===SORRY!===
Word 'if' interpreted as 'if()' function call; please use whitespace instead of parens
at C:/test.p6:8
------> if<HERE>(True) {
Unexpected block in infix position (two terms in a row)
at C:/test.p6:8
------> if(True)<HERE> {
那是怎么回事

我在这里读到,paren是可选的,但对我来说似乎不是这样

我的if语句确实可以在没有paren的情况下工作,只是想知道为什么它会阻止我使用它们,或者为什么它会因为它们而认为
if
是一个子程序

编辑:我是不是有点傻。。。看起来我在我链接的链接上读得不够好,我想这就是你感到困惑的原因。我链接的链接指出了以下几点,这基本上就是我要问的:

if($x > 5) {   # Calls subroutine "if"
}

if ($x > 5) {  # An if conditional
}

我接受了下面的答案,因为它确实提供了一些见解。

您确定创建了名为“if”的sub吗?如果是这样,(没有双关语的意思),如果你在文字“If”后面使用空格,你就会得到关键字,否则如果你在文字“If”后面使用paren,你就会得到预先声明的函数,也就是说,如果你对术语的使用看起来像一个函数调用,而你已经声明了这样一个函数,它就会调用它

use@localhost:~$ perl6
> sub if(Str $s) { say "if sub says: arg = $s" };
sub if (Str $s) { #`(Sub|95001528) ... }
> if "Hello World";
===SORRY!=== Error while compiling <unknown file>
Missing block
at <unknown file>:1
------> if "Hello World"⏏;
    expecting any of:
        block or pointy block
> if("Hello World");
if sub says: arg = Hello World
>
> if 12 < 16 { say "Excellent!" }
Excellent!
>
use@localhost:~$perl6
>sub-if(Str$s){say“if-sub-says:arg=$s”};
sub if(Str$s){#`(sub | 95001528)…}
>如果“你好,世界”;
对不起编译时出错
缺失块
时间:1
------>如果“你好,世界”⏏;
预期有以下情况之一:
块或尖块
>如果(“你好世界”);
如果sub说:arg=Hello World
>
>如果12<16{说“好极了!”}
杰出的
>
您可以在上面看到,我声明了一个名为“if”的函数

如果是“你好世界”错误,因为空格表示我正在使用关键字,因此在尝试使用if关键字时出现语法错误

if(“Hello World”)
成功调用预先声明的函数

if 12<18{say“Excellent!”}
工作正常,因为空格表示“if”被解释为关键字,这次没有语法错误

那么,你确定你有(或者更好——你能粘贴在这里)你预先声明的“if”函数吗


关键字和空格的引用(附带使用关键字“if”作为示例!)如下:

我没有尝试创建名为
if
的子例程,我尝试使用关键字
if
作为关键字,但括号除外。就像在
if(True){#do something}
中,与
if True{#do somethin}
相反。问题是Perl 6似乎不允许我这样做,因为它认为我正在尝试调用一个名为
if
的函数,尽管显然不存在这样一个函数。我的问题基本上可以归结为Perl 6是否允许您使用括号来包围if语句,还是它总是假定您试图调用函数?我明白了。我并不是说迂腐,但在你的最后一个问题中,我认为你的意思是“…围绕一个if条件”,而不是“…if语句”。答案是“是”,你可以用括号括住你的条件,但是关键字和括号内的条件之间必须有空格。上面提到的前两句话是:;“另一个空格起作用的地方是在各种关键字后面,如控制流或其他面向语句的关键字。这些关键字后面需要空格。+@phyreproph如果
关键字后面需要一个空格。”
use@localhost:~$ perl6
> sub if(Str $s) { say "if sub says: arg = $s" };
sub if (Str $s) { #`(Sub|95001528) ... }
> if "Hello World";
===SORRY!=== Error while compiling <unknown file>
Missing block
at <unknown file>:1
------> if "Hello World"⏏;
    expecting any of:
        block or pointy block
> if("Hello World");
if sub says: arg = Hello World
>
> if 12 < 16 { say "Excellent!" }
Excellent!
>