Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

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

by LanX (Saint)
on Dec 25, 2019 at 18:38 UTC ( [id://11110615]=note: print w/replies, xml ) Need Help??


in reply to Re^11: printing unitialized value of the 'do BLOCK'
in thread printing unitialized value of the 'do BLOCK'

> Of course, both are conditionals, so I wouldn't bet on the return value anyway.

This thread is hard to follow, so my 2 cents to sum it up

  • unless is the negation of if
  • if (w/o else) and (the always short-circuiting) and are supposed to do the same
  • hence unless should act as or
  • an if(constant) is optimized at compile time
Especially the last point might have led to inconsistencies when it comes to return values.

If you doubt it, that's how it shows in the op-tree

So yes if and unless are effectively expressions in the form of statements, but returning values.

Did I miss an aspect of this thread?

C:\WINDOWS\system32>perl -MO=Concise -e"if ($x) { print $x }" 8 <@> leave[1 ref] vKP/REFC ->(end) 1 <0> enter ->2 2 <;> nextstate(main 1 -e:1) v:{ ->3 - <1> null vK/1 ->8 4 <|> and(other->5) vK/1 ->8 # if +-> and - <1> ex-rv2sv sK/1 ->4 3 <#> gvsv[*x] s ->4 - <@> scope vK ->- - <;> ex-nextstate(main 3 -e:1) v ->5 7 <@> print vK ->8 5 <0> pushmark s ->6 - <1> ex-rv2sv sK/1 ->7 6 <#> gvsv[*x] s ->7 -e syntax OK C:\WINDOWS\system32>perl -MO=Concise -e"unless ($x) { print $x }" 8 <@> leave[1 ref] vKP/REFC ->(end) 1 <0> enter ->2 2 <;> nextstate(main 1 -e:1) v:{ ->3 - <1> null vK/1 ->8 4 <|> or(other->5) vK/1 ->8 # unl +ess -> or - <1> ex-rv2sv sK/1 ->4 3 <#> gvsv[*x] s ->4 - <@> scope vK ->- - <;> ex-nextstate(main 3 -e:1) v ->5 7 <@> print vK ->8 5 <0> pushmark s ->6 - <1> ex-rv2sv sK/1 ->7 6 <#> gvsv[*x] s ->7 -e syntax OK C:\WINDOWS\system32>perl -MO=Concise -e"unless (0) { print $x }" 6 <@> leave[1 ref] vKP/REFC ->(end) 1 <0> enter ->2 2 <;> nextstate(main 1 -e:1) v:{ ->3 - <@> scope vK/FOLD ->6 - <;> ex-nextstate(main 3 -e:1) v ->3 5 <@> print vK ->6 # alw +ays true 3 <0> pushmark s ->4 - <1> ex-rv2sv sK/1 ->5 4 <#> gvsv[*x] s ->5 -e syntax OK C:\WINDOWS\system32>perl -MO=Concise -e"unless (1) { print $x }" 3 <@> leave[1 ref] vKP/REFC ->(end) 1 <0> enter ->2 2 <;> nextstate(main 1 -e:1) v:{ ->3 # nev +er true - <0> ex-const v/SHORT ->3 -e syntax OK

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

Replies are listed 'Best First'.
Re^13: printing unitialized value of the 'do BLOCK' (summary)
by ikegami (Patriarch) on Dec 26, 2019 at 09:07 UTC

    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.

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

        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://11110615]
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: (1)
As of 2024-04-25 01:47 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found