Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why do Perl control statements require braces?

Tags:

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 either referred to the syntax documentation that says that braces are required, which I think is just begging the question, or ignored the question and simply gave braceless alternatives.

Why does Perl require braces for control statements like if and for? Put another way, why does Perl require blocks rather than statements, like some other popular languages allow?

like image 729
Rob Kennedy Avatar asked Dec 09 '09 21:12

Rob Kennedy


People also ask

Why do we use block of statements with braces?

Braces are used around all statements, even single statements, when they are part of a control structure, such as an if-else or for statement. This makes it easier to add statements without accidentally introducing bugs due to forgetting to add braces.

What loops require curly braces?

If the number of statements following the for/if is single you don't have to use curly braces. But if the number of statements is more than one, then you need to use curly braces.

Why do we use curly brackets in Python?

In Python, curly braces are used to define a data structure called a dictionary (a key/value mapping), while white space indentation is used to define program blocks.

What do curly brackets do in C?

In programming, curly braces (the { and } characters) are used in a variety of ways. In C/C++, they are used to signify the start and end of a series of statements. In the following expression, everything between the { and } are executed if the variable mouseDOWNinText is true. See event loop.


2 Answers

One reason could be that some styles dictate that you should always use braces with control structures, even for one liners, in order to avoid breaking them later, e.g.:

if (condition)     myObject.doSomething(); else     myObject.doSomethingElse(); 

Then someone adds something more to the first part:

if (condition)    myObject.doSomething();    myObject.doSomethingMore(); // Syntax error next line else     myObject.doSomethingElse(); 

Or worse:

if (condition)    myObject.doSomething(); else     myObject.doSomethingElse();    myObject.doSomethingMore(); // Compiles, but not what you wanted. 

In Perl, these kinds of mistakes are not possible, because not using braces with control structures is always a syntax error. In effect, a style decision has been enforced at the language syntax level.

Whether that is any part of the real reason, only Larry's moustache knows.

like image 129
Adam Bellaire Avatar answered Sep 23 '22 07:09

Adam Bellaire


One reason could be that some constructs would be ambiguous without braces :

foreach (@l) do_something unless $condition; 

Does unless $condition apply to the whole thing or just the do_something statement?

Of course this could have been worked out with priority rules or something, but it would have been yet another way to create confusing Perl code :-)

like image 40
Jérémie Koenig Avatar answered Sep 24 '22 07:09

Jérémie Koenig