Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why must 'if' clause in Perl come with either curly brackets or nothing?

This is ok:

if ($some_var==$some_value) {}

This is ok too:

print "hello" if ($some_var==$some_value);

But this raises an error:

if ($some_var==$some_value) print "some_message";

Why must 'if' clause in Perl come with either curly brackets or nothing?

like image 974
nicola Avatar asked Apr 03 '11 10:04

nicola


2 Answers

To avoid the well-known problem in compiler construction known as a "dangling else". See http://en.wikipedia.org/wiki/Dangling_else

like image 195
tadmc Avatar answered Nov 15 '22 07:11

tadmc


Perl has a rather complex syntax and is rather difficult to parse. I gather that curly brackets were made mandatory following an if clause so as to remove an ambiguity and make Perl code easier to parse.

like image 22
swestrup Avatar answered Nov 15 '22 07:11

swestrup