There's a deeper answer that has to do in part with how far the parser has to "look ahead" to disambiguate a statement. A lookahead of one (token) is easy for a parser to deal with. When the grammar calls for a lookahead of two or more, the mechanics of parsing -- particularly the mechanics of diagnosing an error and "recovering" to continue parsing -- get much more complicated. (Back when parsing was a hot topic, there where many dozen articles on parser error recovery.)
This is one reason why some languages use THEN with IF statements. As soon as the parser looks ahead one token and sees THEN, it knows that it's done parsing the IF condition.
In Perl, the parenthesis serve the same function. When the parser looks ahead one token and sees ')' (having already accounted for nested parenthesis), it knows it has the condition parsed.
Consider a silly (and probably incorrect) example:
if $a == undef $b = 1;
^
If the parser, having recognized that it's dealing with an IF statement and is parsing the condition, is looking ahead at the single token '$b', how can it tell whether it has finished parsing the condition and is now looking at the following statement? The answer is, it can't.
Parsers are fun. Playing with one for a while can give one a much deeper appreciation for language design.
(And now someone who really knows the Perl parser will tell my why I'm full of it. ;-)
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.