in reply to Better expression for $x = !!$y||0

Shorter: $x = 1-!$y

Better, IMO, but much depends on what the reader knows about x and y from the surrounding code:

$x = 0; if ($y) { $x = 1; ... }

Replies are listed 'Best First'.
Re^2: Better expression for $x = !!$y||0
by merlyn (Sage) on Apr 19, 2005 at 14:07 UTC
      I said shorter, I didn't say accurate :). But really, if you presume an overloaded ! that returns something other than perl's true and false values (and that's presuming an awful lot) there's nothing you can do to $y besides examine it in boolean context with if or ?:.
        Just to let you know we fixed this, in Perl 6 this is written:
        $x = ?$y;
        And the standard operators like == are expected to return 0 or 1 when a boolean result is expected. (Boolean context will still accept any true value, as in Perl 5, and the short-circuit operators still return one of the input values as the "true" value.),

        Yes, you can still overload these to return something other than 0 or 1, but it would be construed as antisocial.

        I said shorter, I didn't say accurate.
        If it doesn't need to be accurate, $x=1 is pretty short.