in reply to Weird behavior of int()

Found a new idiom :-)
my $y= do { use integer; $x+0 };

I agree it would be nice if int() did that already.

Replies are listed 'Best First'.
Re^2: Weird behavior of int()
by ikegami (Patriarch) on May 21, 2024 at 23:19 UTC

    No, it wouldn't.

    It silently converts some values to -1. This is only useful if you want to check if the number is representable by an IV.

    It silently converts some positive values to negative values. This is only useful if you want to cast from unsigned to signed.

    In both cases, it would be better if you used an approach that made it clear what you wanted to do instead.

      Hm, good catch. Even more undesirable edge cases than the original.