Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

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

by rsFalse (Chaplain)
on Dec 18, 2019 at 23:26 UTC ( [id://11110364]=note: print w/replies, xml ) Need Help??


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

I thought that '1' is also expression.
  • Comment on Re^9: printing unitialized value of the 'do BLOCK'

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

    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)

      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

      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://11110364]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others chilling in the Monastery: (9)
As of 2024-04-23 10:33 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found