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

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

Replies are listed 'Best First'.
Re^4: unless versus if ( ! ) inside a subroutine
by halley (Prior) on Jan 16, 2008 at 14:02 UTC
    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)...