Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Re^10: printing unitialized value of the 'do BLOCK'

by ikegami (Patriarch)
on Dec 19, 2019 at 00:13 UTC ( [id://11110366]=note: print w/replies, xml ) Need Help??


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

But the return value is only defined when the last statement is an expression. The last statement is an unless statement, so the return value is undefined.

We were discussing what value is returned even though it's undefined. But that's always dangerous. As you've pointed out, it doesn't always work.

Update: Fixed typo (until=>unless)

Replies are listed 'Best First'.
Re^11: printing unitialized value of the 'do BLOCK'
by soonix (Canon) on Dec 23, 2019 at 08:11 UTC
    The last statement is unless, not until. Of course, both are conditionals, so I wouldn't bet on the return value anyway.
      > 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

        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.

Re^11: printing unitialized value of the 'do BLOCK'
by rsFalse (Chaplain) on Dec 22, 2019 at 23:29 UTC
    I'm not sure I understand. But nevermind.

      The docs say

      If no return is found and if the last statement is an expression, its value is returned. If the last statement is a loop control structure like a foreach or a while, the returned value is unspecified. The empty sub returns the empty list.

      The last statement in your example is NOT an expression —it's an unless statement— so the value returned by your block is unspecified. That means you can't count on it being anything. I explained what it normally returns, but as you showed, that doesn't always hold.

      So, you can't count on a block ending with an if statement unless statement resulting in something useful or predictable.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (3)
As of 2024-04-26 00:51 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found