Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Disable bitwise operators

Some of them used to return wrong results, others just failed horribly. We're
disabling them alltogether, with a useful error message, until maybe someone
figures out how to make them DWIM.
  • Loading branch information...
commit b7c968f9586f8ff2e3a6a822b6ed3cc5d14f6b26 1 parent 9f53154
@rafl rafl authored
Showing with 26 additions and 1 deletion.
  1. +11 −1 lib/Math/BigRat.pm
  2. +15 −0 t/bitwise.t
View
12 lib/Math/BigRat.pm
@@ -16,6 +16,7 @@ package Math::BigRat;
# anythig older is untested, and unlikely to work
use 5.006;
use strict;
+use Carp ();
use Math::BigFloat;
use vars qw($VERSION @ISA $upgrade $downgrade
@@ -26,7 +27,16 @@ use vars qw($VERSION @ISA $upgrade $downgrade
$VERSION = '0.26';
$VERSION = eval $VERSION;
-use overload; # inherit overload from Math::BigFloat
+# inherit overload from Math::BigFloat, but disable the bitwise ops that don't
+# make much sense for rationals unless they're truncated or something first
+
+use overload
+ map {
+ my $op = $_;
+ ($op => sub {
+ Carp::croak("bitwise operation $op not supported in Math::BigRat");
+ });
+ } qw(& | ^ ~ << >> &= |= ^= <<= >>=);
BEGIN
{
View
15 t/bitwise.t
@@ -0,0 +1,15 @@
+use strict;
+use warnings;
+use Test::More tests => 22;
+
+use Math::BigRat;
+
+my $x = Math::BigRat->new('3/7');
+
+for my $op (qw(& | ^ << >> &= |= ^= <<= >>=)) {
+ ok !eval "my \$y = \$x $op 42; 1";
+ like $@, qr/^bitwise operation \Q$op\E not supported in Math::BigRat/;
+}
+
+ok !eval "my \$y = ~\$x; 1";
+like $@, qr/^bitwise operation ~ not supported in Math::BigRat/;
Please sign in to comment.
Something went wrong with that request. Please try again.