4 ms·
I didn't know ~- and -~ had a name, I have been using them in many (non-production) places where I wanted to save 2 bytes. But looks like Perl's implementation
by omoikane 9mo ago
I didn't know ~- and -~ had a name, I have been using them in many (non-production) places where I wanted to save 2 bytes.
But looks like Perl's implementation is more limited compared to other languages:
% perl -e 'print ~-(0), "\n"'
18446744073709551615
% ruby -e 'print ~-(0), "\n"'
-1
https://metacpan.org/dist/perlsecret/view/lib/perlsecret.pod#Inchworm-on-a-stick https://metacpan.org/dist/perlsecret/view/lib/perlsecret.pod...
- shawn_w 9mo agoYou're not using `integer` in the perl example like you're supposed to. Try perl -Minteger -E 'say ~-0' and it should work as expected.
- omoikane 9mo agoThat is true, but for the kind of code where I might use this trick, there usually aren't enough `~-$x` for `use integer;` to be worthwhile, I would just do `($x-1)` instead.