Why is 'print (52-80)*42' different than 'print 42*(52-80)' in Perl?

后端 未结 3 615
说谎
说谎 2020-12-11 09:48

Perl:: What is:

1. (52-80)*42
2. 42*(52-80)

Ans:

1. -28
2. -1176

Why?

Have fun

3条回答
  •  孤街浪徒
    2020-12-11 10:40

    Thanks to Perl's wacky parsing rules (oh Perl, you kook!), these statements:

    print ((52-80)*42) , "\n";
    print (52-80)*42 , "\n";
    

    Are interpreted as if they were written:

    (print ((52-80)*42)), "\n";
    (print (52-80))*42, "\n";
    

    This is why you end up seeing -1176-28 on one line, and the expected blank line is missing. Perl sees (print-expr), "\n"; and simply throws away the newline instead of printing it.

提交回复
热议问题