Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw

Re^3: unless versus if ( ! ) inside a subroutine

by almut (Canon)
on Jan 15, 2008 at 19:02 UTC ( #662532=note: print w/replies, xml ) Need Help??

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

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://662532]
Jar. Jar!...

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (8)
As of 2018-07-20 12:45 GMT
Find Nodes?
    Voting Booth?
    It has been suggested to rename Perl 6 in order to boost its marketing potential. Which name would you prefer?

    Results (431 votes). Check out past polls.