in reply to Re: unless versus if ( ! ) inside a subroutine
in thread unless versus if ( ! ) inside a subroutine

Here is an answer I received via email. Thoughts?

"The thing about Perl is that (excluding explicit returns), Perl will return the last value seen in the function. With your "unless" example, the unless is explicitly not going to ever evaluate because the condition is predetermined to be false.

So the entire unless statement is not evaluated and the return is the last evaluation (your variable assignment).

'If' works differently because it does no pre-evaluation. It evaluates the if block every time to determine if the conditional is met. So the last "evaluation" in the function is the if block (which returns nothing, just like your function).

The magic happening is the pre-evaluation conditions of the unless block. Unless ONLY fires if the condition is met. If fires every time, but performs an automatic JMP outside of that block (with a null return) as soon as the condition evals to false."

  • Comment on Re^2: unless versus if ( ! ) inside a subroutine

Replies are listed 'Best First'.
Re^3: unless versus if ( ! ) inside a subroutine
by kyle (Abbot) on Jan 15, 2008 at 19:06 UTC

    So the entire unless statement is not evaluated and the return is the last evaluation (your variable assignment).

    This is not exactly true.

    sub t1 { my $v = 'last expression'; my $x = 'last assignment'; unless ( $v ) {} } sub t2 { my $x = 'last assignment'; unless ( 'last expression' ) {} } printf "t1 returns [%s]\n", t1(); printf "t2 returns [%s]\n", t2(); __END__ t1 returns [last expression] t2 returns []

    Looking at the output from B::Deparse, It seems the first unless is left as-is, but the second one (where the condition is a literal) is reduced to !1 (which returns as the empty string).

    If I change each unless to if, the second one reduces the same way, and the first one stays (just like the first unless stays originally).

    So I think the rest of your email response is suspect also. In my testing, if seems to evaluate the same way as unless.

Re^3: unless versus if ( ! ) inside a subroutine
by almut (Canon) on Jan 15, 2008 at 19:02 UTC
    ...the unless is explicitly not going to ever evaluate because the condition is predetermined to be false.

    This doesn't make much sense to me. In both cases (if and unless) the conditional has to be evaluated. It's just that when the value isn't negated, the last thing evaluated is the value as is. When you request the negated value to be tested in the conditional, the negated value is the last thing evaluated. How else would you explain that the following returns 1?

    sub testUnlessNot { my $v = ''; unless ( !$v ) {}; # returns 1 }
      Conditionals are not always evaluated. The compiler can sometimes decide to phrase things differently.
      U:\> perl -MO=Deparse -le "$x = 5; if ($x) { print $x }" $x = 5; if ($x) { print $x; }
      U:\>perl -MO=Deparse -le "$x = 5; if (0) { print $x }" $x = 5; '???';
      U:\>perl -MO=Deparse -le "$x = 5; if (1) { print $x }" $x = 5; print $x;;
      U:\>perl -MO=Deparse -le "$x = 5; if (! 0) { print $x }" $x = 5; print $x;;

      --
      [ e d @ h a l l e y . c c ]

        Sure, but in those cases, constants are being tested in the conditional, which allows the compiler to make optimisations. In the OP's example, though, $v is a regular (mutable) scalar variable, which could change its value at runtime (after the compiler has done its work)...