in reply to return if 0

return if 0; is interpreted as if (0) { return; } which will never happen.

-- Time flies when you don't know what you're doing

Replies are listed 'Best First'.
Re^2: return if 0
by ikegami (Patriarch) on Jun 22, 2009 at 19:40 UTC
    Not true. The return is never executed, but like the OP noticed and we've been explaining, the statement *is* executed. How else can it be known whether the return should be executed or not?

      To be fair, it wouldn't be insane for the compiler to know that the block can never execute, and just remove it completely.

      Except for the part that it has unexpected consequences, as we've been discussing.

      -- zigdon

        It does remove the if, but it keeps the constant to maintain the behaviour.
        >perl -MO=Concise,-exec,f -e"sub f { return 1 if 0 }" main::f: 1 <;> nextstate(main 1 -e:1) v 2 <$> const[IV 0] s/SHORT 3 <1> leavesub[1 ref] K/REFC,1 -e syntax OK

        Compared to:

        >perl -MO=Concise,-exec,f -e"sub f { return 1 if $c }" main::f: 1 <;> nextstate(main 1 -e:1) v 2 <#> gvsv[*c] s 3 <|> and(other->4) K/1 4 <0> pushmark s 5 <$> const[IV 1] s 6 <@> return K 7 <1> leavesub[1 ref] K/REFC,1 -e syntax OK

        Note the and operator (used to implement if) is missing from the first snippet.

      Perhaps I was a bit unclear; I meant to say the "return" will never happen because 0 will never be true.

      -- Time flies when you don't know what you're doing
        In that case, your post simply didn't answer anything. The OP knew the return wasn't being executed. He was wondering why the following didn't return 1, print's return value.
        sub test { print "in test!"; return if 0; }