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)
| [reply] [d/l] |
The last statement is unless, not until. Of course, both are conditionals, so I wouldn't bet on the return value anyway.
| [reply] [d/l] [select] |
> 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
| [reply] [d/l] [select] |
I'm not sure I understand. But nevermind.
| [reply] |
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.
| [reply] [d/l] [select] |