Why do Perl control statements require braces?

后端 未结 8 1187
挽巷
挽巷 2021-02-07 00:05

This may look like the recent question that asked why Perl doesn\'t allow one-liners to be \"unblocked,\" but I found the answers to that question unsatisfactory because they ei

相关标签:
8条回答
  • 2021-02-07 00:26

    Perhaps not directly relevant to your question about (presumably) Perl 5 and earlier, but…

    In Perl 6, control structures do not require parentheses:

    if $x { say '$x is true' }
    
    for <foo bar baz> -> $s { say "[$s]" }
    

    This would be horrendously ambiguous if the braces were also optional.

    0 讨论(0)
  • 2021-02-07 00:31

    In Programming Perl (which Larry Wall co-authored), 3rd Edition, page 113, compound statements are defined in terms of expressions and blocks, not statements, and blocks have braces.

    Note that unlike in C and Java, [compound statements] are defined in terms of BLOCKS, not statements. This means that the braces are requried--no dangling statements allowed.

    I don't know if that answers your question but it seems like in this case he chose to favor a simple language structure instead of making exceptions.

    0 讨论(0)
提交回复
热议问题