Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

Re^13: printing unitialized value of the 'do BLOCK' (summary)

by ikegami (Patriarch)
on Dec 26, 2019 at 09:07 UTC ( [id://11110629]=note: print w/replies, xml ) Need Help??


in reply to Re^12: printing unitialized value of the 'do BLOCK' (summary)
in thread printing unitialized value of the 'do BLOCK'

So yes if and unless are effectively expressions in the form of statements

They are not.

$ perl -c -e'my $x = if (f()) { g(); } else { h(); }' syntax error at -e line 1, near "= if" -e had compilation errors. $ perl -e'my $x = unless (f()) { g(); } else { h(); }' syntax error at -e line 1, near "= unless" -e had compilation errors.

if statements and unless statements are statements, not expressions.

if (w/o else) and (the always short-circuiting) and are supposed to do the same

They're not, and they don't.

$ perl -c -e'my $x = ( f() and g() );' -e syntax OK $ perl -c -e'my $x = ( if (f()) { g() } );' syntax error at -e line 1, near "( if" -e had compilation errors.

-MO=Concise shows opcodes, not operators. An if statement and an and expression often (but not always) use the and opcode, but that doesn't tell you anything relevant to this conversation.

Would you say an if statement in C and a while statement in C are the same thing because they both use the same branch opcode?

In short, you are begging the question. You are presuming an if statement does the same as an and expression to conclude that an if<c> statement does the same as an <c>and expression.

Replies are listed 'Best First'.
Re^14: printing unitialized value of the 'do BLOCK' (summary)
by rsFalse (Chaplain) on Dec 26, 2019 at 12:14 UTC
    So it looks like they DO the same, except that one of them can't return a value unless enveloped by 'do{}', 'map{}...' etc.
      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...)

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

      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."

        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.

      What an operator returns is a key aspect of what they do.

      But the point was that you can't argue that they must return the same thing because they *otherwise* have a similar effect.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others wandering the Monastery: (5)
As of 2024-03-28 11:36 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found