Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?

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]
[marto]: I really should have stayed in bed today
[choroba]: exceptions to this rule are rare

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (6)
As of 2018-03-20 10:44 GMT
Find Nodes?
    Voting Booth?
    When I think of a mole I think of:

    Results (250 votes). Check out past polls.