Permalink
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...
1 parent 9f53154 commit b7c968f9586f8ff2e3a6a822b6ed3cc5d14f6b26 @rafl rafl committed Nov 9, 2010
Showing with 26 additions and 1 deletion.
  1. +11 −1 lib/Math/BigRat.pm
  2. +15 −0 t/bitwise.t
View
@@ -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
@@ -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/;

0 comments on commit b7c968f

Please sign in to comment.