The Perldocs are a bit fuzzy about the definitions of
- statement
- expression
- term
My normal understanding is that a program is a list of STATEMENTS.
They start after a semicolon and end with a semicolon, unless:
- neighboring curlies
- file borders
-
anonymous [array]/{hash} brackets °
- ... (Something I forgot, because only Perl can parse Perl)
They mostly correspond to "lines" in speech, but line numbers are only recorded per statement.²
A TERM is a sub-expression inside a statement. That's roughly everything you can put into round parens and terms can be nested. This corresponds to a branch or leaf in the op tree.
In order to use a statement as a term, Perl offers the do {BLOCK} syntax. Other possibilities are sub and eval .
Since statements are otherwise bound to be executed in void context (after a semicolon), we use them for their side effects and don't expect them to return (meaningful) values.
But some do, like if .
You've even cited a passage from one of Larry's³ standard books showing that it's not an accident.
EXPRESSION is a general concept,
- perlsyn explicitly calls statements "expressions"
- perlop lists "expressions in parenthesis" as terms and refers to do "being parsed as term".
So I hope it's clearer now that the difference is mainly in how something is parsed into the syntax tree.
I think part of the confusion comes from Larry using C syntax mixed with Lisp semantics.
Disclaimer: I didn't have time to check all canonical Perldocs and sometimes it's not clear if an english word was used in its general meaning or to denote a Perl concept. ( like "Expressions are expressions", "and and or are operators", and so on...)
footnote
°) I probably misread perlop here, my tests don't allow to write a statement into [] or {}
²) multiple statements per line or multi-line statements break the mental analogy
³) ERRATA: rsFalse informed me that "Intermediate Perl was written by Schwartz and others, not including Larry." |