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

use strict; use warnings; sub testUnless { my $v = 'Navidson'; unless ( $v ) {}; # returns $v } sub testIfNot { my $v = 'Holloway'; if ( ! $v ) {}; # returns ! $v } sub testIf { my $v = 0; if ( $v ) {} # returns $v } sub testNothing { my $v = 'kyle'; if ( $v ) {} # returns nothing } printf "testUnless -> [%s]\n", testUnless(); printf "testIfNot -> [%s]\n", testIfNot(); printf "testIf -> [%s]\n", testIf(); printf "testNothing -> [%s]\n", testNothing(); __END__ testUnless -> [Navidson] testIfNot -> [] testIf -> [0] Use of uninitialized value in printf ... testNothing -> []

In every case (except testNothing), it's returning the last expression evaluated. In testNothing, it's returning nothing (which is turned to undef and then stringified to an empty string). Perhaps the confusion here is that the false value returned by !$v is a defined empty string, and not zero.

Replies are listed 'Best First'.
Re^2: unless versus if ( ! ) inside a subroutine
by Wheeler (Acolyte) on Jan 15, 2008 at 18:30 UTC
    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."

      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.

      ...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 ]