Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

Re^16: printing unitialized value of the 'do BLOCK' (EXPRESSION vs TERM vs STATEMENT)

by ikegami (Patriarch)
on Dec 27, 2019 at 06:16 UTC ( [id://11110651]=note: print w/replies, xml ) Need Help??


in reply to Re^15: printing unitialized value of the 'do BLOCK' (EXPRESSION vs TERM vs STATEMENT)
in thread printing unitialized value of the 'do BLOCK'

The Perldocs are a bit fuzzy about the definitions of statement, expression, term

A file is a sequence of statements. A block is a sequence of statements in curlies. A statement can be a flow control statement (if, while, etc), a simple statement (an expression used for its side-effects), as well as other things.

An expression defines the syntax of code required to evaluate to one or more values. For example, an if statement's condition must be an expression.

No one used the word "term". It's a parser rule that usually encompasses variables, literals and sub-expressions in parens. Not a very useful word.

A TERM is a sub-expression inside a statement

No. For example, 1+2 is not a term. An expression inside a statement is an expression (which contains terms).

perlsyn explicitly calls statements "expressions"

No, it doesn't. "The only kind of simple statement is an expression evaluated for its side-effects" is equivalent to "an expression evaluated for its side-effects is a simple statement." In other words, expressions are also statements. For example, print("x") is an expression (so you can do if (print("x")) { ... }), but it can also be used as a statement.

So I hope it's clearer now that the difference is mainly in how something is parsed into the syntax tree.

Both "statement" and "expression" are names of parser rules. As parser rules, there are two differences between them: The syntax they allow, and where they can be used.

Replies are listed 'Best First'.
Re^17: printing unitialized value of the 'do BLOCK' (EXPRESSION vs TERM vs STATEMENT)
by LanX (Saint) on Dec 27, 2019 at 17:20 UTC

      So Perl's if is an expression, especially when executed in a scalar or list context due to a surrounding do.

      No.

      Again, the condition of an if statement is defined to be an expression, so

      if ( ... ) { ... }

      isn't an expression because

      if ( if ( ... ) { ... } ) { }

      isn't valid.

      Furthermore, if any statement is an expression because any statement can be found at the end of the block of a do BLOCK, then the paragraph that's been quoted saying that the returned value is only defined for expressions wouldn't make sense.

      Your usage of the terms clearly differs from the documentation's.

        > Your usage of the terms clearly differs from the documentation's.

        Depending on the definition you take

        I'd say according to perlglossary it must be parsable without surrounding do {}

      • expression

        Anything you can legally say in a spot where a value is required. Typically composed of literals, variables, operators, functions, and subroutine calls, not necessarily in that order.

      • But according to WP expression, the defining characteristic is returning a value

        An expression in a programming language is a combination of one or more constants, variables, operators, and functions that the programming language interprets (according to its particular rules of precedence and of association) and computes to produce ("to return", in a stateful environment) another value.

        I'd compromise on "statement which can be used like an expression to produce values".

        Cheers Rolf
        (addicted to the Perl Programming Language :)
        Wikisyntax for the Monastery FootballPerl is like chess, only without the dice

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://11110651]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others having an uproarious good time at the Monastery: (3)
As of 2024-04-19 19:00 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found