summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorTels <nospam-abuse@bloodgate.com>2007-06-16 16:33:47 +0200
committerRafael Garcia-Suarez <rgarciasuarez@gmail.com>2007-06-17 13:28:00 +0000
commitd1a15766ff5cdfaf84d91442a68bc2a05880bf12 (patch)
tree050d2d413b2850d26d0aee67b05c542490fad92a /lib
parent3006a4e1991c29fe722177d0ddb5a525c7ce8822 (diff)
downloadperl-d1a15766ff5cdfaf84d91442a68bc2a05880bf12.tar.gz
bignum 0.22 take 4 (hex()/oct() overloading)
Message-Id: <200706161433.47489@bloodgate.com> p4raw-id: //depot/perl@31403
Diffstat (limited to 'lib')
-rw-r--r--lib/bigint.pm112
-rw-r--r--lib/bignum.pm139
-rwxr-xr-xlib/bignum/t/bigint.t27
-rwxr-xr-xlib/bignum/t/bignum.t27
-rwxr-xr-xlib/bignum/t/bigrat.t27
-rw-r--r--lib/bignum/t/in_effect.t2
-rw-r--r--lib/bignum/t/scope_f.t15
-rw-r--r--lib/bignum/t/scope_i.t16
-rw-r--r--lib/bignum/t/scope_r.t15
-rw-r--r--lib/bigrat.pm122
10 files changed, 433 insertions, 69 deletions
diff --git a/lib/bigint.pm b/lib/bigint.pm
index 26b8b2f1a4..c64116a427 100644
--- a/lib/bigint.pm
+++ b/lib/bigint.pm
@@ -117,12 +117,55 @@ sub in_effect
$hinthash->{bigint};
}
+#############################################################################
+# the following two routines are for "use bigint qw/hex oct/;":
+
+sub _hex_global
+ {
+ my $i = $_[0];
+ $i = '0x'.$i unless $i =~ /^0x/;
+ Math::BigInt->new($i);
+ }
+
+sub _oct_global
+ {
+ my $i = $_[0];
+ return Math::BigInt->from_oct($i) if $i =~ /^0[0-7]/;
+ Math::BigInt->new($i);
+ }
+
+#############################################################################
+# the following two routines are for Perl 5.9.4 or later and are lexical
+
+sub _hex
+ {
+ return CORE::hex($_[0]) unless in_effect(1);
+ my $i = $_[0];
+ $i = '0x'.$i unless $i =~ /^0x/;
+ Math::BigInt->new($i);
+ }
+
+sub _oct
+ {
+ return CORE::oct($_[0]) unless in_effect(1);
+ my $i = $_[0];
+ return Math::BigInt->from_oct($i) if $i =~ /^0[0-7]/;
+ Math::BigInt->new($i);
+ }
+
sub import
{
my $self = shift;
$^H{bigint} = 1; # we are in effect
+ # for newer Perls always override hex() and oct() with a lexical version:
+ if ($] > 5.009004)
+ {
+ no warnings 'redefine';
+ *CORE::GLOBAL::oct = \&_oct;
+ *CORE::GLOBAL::hex = \&_hex;
+ }
# some defaults
my $lib = ''; my $lib_kind = 'try';
@@ -162,6 +205,18 @@ sub import
$trace = 1;
splice @a, $j, 1; $j --;
}
+ elsif ($_[$i] eq 'hex')
+ {
+ splice @a, $j, 1; $j --;
+ no warnings 'redefine';
+ *CORE::GLOBAL::hex = \&_hex_global;
+ }
+ elsif ($_[$i] eq 'oct')
+ {
+ splice @a, $j, 1; $j --;
+ no warnings 'redefine';
+ *CORE::GLOBAL::oct = \&_oct_global;
+ }
else { die "unknown option $_[$i]"; }
}
my $class;
@@ -236,12 +291,18 @@ bigint - Transparent BigInteger support for Perl
print 2 ** 512,"\n"; # really is what you think it is
print inf + 42,"\n"; # inf
print NaN * 7,"\n"; # NaN
+ print hex("0x1234567890123490"),"\n"; # Perl v5.9.4 or later
{
no bigint;
print 2 ** 256,"\n"; # a normal Perl scalar now
}
+ # Note that this will be global:
+ use bigint qw/hex oct/;
+ print hex("0x1234567890123490"),"\n";
+ print oct("01234567890123490"),"\n";
+
=head1 DESCRIPTION
All operators (including basic math operations) are overloaded. Integer
@@ -272,9 +333,9 @@ some functions. C<bigint> truncates these results to integer too:
2
# perl -Mbigint -wle 'print exp(1)'
2
- # perl -Mbigint -wle 'print exp(1)'
+ # perl -Minteger -wle 'print exp(1)'
2.71828182845905
- # perl -Mbigint -wle 'print exp(1) + 0'
+ # perl -Minteger -wle 'print exp(1) + 0'
2
In practice this makes seldom a difference as B<parts and results> of
@@ -322,6 +383,18 @@ Note that setting precision and accurary at the same time is not possible.
This enables a trace mode and is primarily for debugging bigint or
Math::BigInt.
+=item hex
+
+Override the build-in hex() method with a version that can handle big
+integers. Note that under Perl v5.9.4 or ealier, this will be global
+and cannot be disabled with "no bigint;".
+
+=item oct
+
+Override the build-in oct() method with a version that can handle big
+integers. Note that under Perl v5.9.4 or ealier, this will be global
+and cannot be disabled with "no bigint;".
+
=item l, lib, try or only
Load a different math lib, see L<Math Library>.
@@ -472,6 +545,41 @@ Using methods that do not modify, but testthe contents works:
See the documentation about the copy constructor and C<=> in overload, as
well as the documentation in BigInt for further details.
+=head1 CAVAETS
+
+=over 2
+
+=item in_effect()
+
+This method only works on Perl v5.9.4 or later.
+
+=item hex()/oct()
+
+C<bigint> overrides these routines with versions that can also handle
+big integer values. Under Perl prior to version v5.9.4, however, this
+will not happen unless you specifically ask for it with the two
+import tags "hex" and "oct" - and then it will be global and cannot be
+disabled inside a scope with "no bigint":
+
+ use bigint qw/hex oct/;
+
+ print hex("0x1234567890123456");
+ {
+ no bigint;
+ print hex("0x1234567890123456");
+ }
+
+The second call to hex() will warn about a non-portable constant.
+
+Compare this to:
+
+ use bigint;
+
+ # will warn only under Perl older than v5.9.4
+ print hex("0x1234567890123456");
+
+=back
+
=head1 MODULES USED
C<bigint> is just a thin wrapper around various modules of the Math::BigInt
diff --git a/lib/bignum.pm b/lib/bignum.pm
index 3d79c52eeb..43233566bd 100644
--- a/lib/bignum.pm
+++ b/lib/bignum.pm
@@ -3,15 +3,22 @@ use 5.006002;
$VERSION = '0.22';
use Exporter;
+@ISA = qw( bigint );
@EXPORT_OK = qw( );
@EXPORT = qw( inf NaN );
-@ISA = qw( Exporter );
use strict;
use overload;
+require bigint; # no "use" to avoid import being called
##############################################################################
+BEGIN
+ {
+ *inf = \&bigint::inf;
+ *NaN = \&bigint::NaN;
+ }
+
# These are all alike, and thus faked by AUTOLOAD
my @faked = qw/round_mode accuracy precision div_scale/;
@@ -47,24 +54,6 @@ sub AUTOLOAD
Carp::croak ("Can't call bignum\-\>$name, not a valid method");
}
-sub upgrade
- {
- $Math::BigInt::upgrade;
- }
-
-sub _binary_constant
- {
- # this takes a binary/hexadecimal/octal constant string and returns it
- # as string suitable for new. Basically it converts octal to decimal, and
- # passes every thing else unmodified back.
- my $string = shift;
-
- return Math::BigInt->new($string) if $string =~ /^0[bx]/;
-
- # so it must be an octal constant
- Math::BigInt->from_oct($string);
- }
-
sub unimport
{
$^H{bignum} = undef; # no longer in effect
@@ -78,12 +67,39 @@ sub in_effect
$hinthash->{bignum};
}
+#############################################################################
+# the following two routines are for Perl 5.9.4 or later and are lexical
+
+sub _hex
+ {
+ return CORE::hex($_[0]) unless in_effect(1);
+ my $i = $_[0];
+ $i = '0x'.$i unless $i =~ /^0x/;
+ Math::BigInt->new($i);
+ }
+
+sub _oct
+ {
+ return CORE::oct($_[0]) unless in_effect(1);
+ my $i = $_[0];
+ return Math::BigInt->from_oct($i) if $i =~ /^0[0-7]/;
+ Math::BigInt->new($i);
+ }
+
sub import
{
my $self = shift;
$^H{bignum} = 1; # we are in effect
+ # for newer Perls override hex() and oct() with a lexical version:
+ if ($] > 5.009003)
+ {
+ no warnings 'redefine';
+ *CORE::GLOBAL::oct = \&_oct;
+ *CORE::GLOBAL::hex = \&_hex;
+ }
+
# some defaults
my $lib = ''; my $lib_kind = 'try';
my $upgrade = 'Math::BigFloat';
@@ -139,6 +155,20 @@ sub import
$trace = 1;
splice @a, $j, 1; $j --;
}
+ elsif ($_[$i] eq 'hex')
+ {
+ splice @a, $j, 1; $j --;
+ no warnings 'redefine';
+ # override with a global version
+ *CORE::GLOBAL::hex = \&bigint::_hex_global;
+ }
+ elsif ($_[$i] eq 'oct')
+ {
+ splice @a, $j, 1; $j --;
+ no warnings 'redefine';
+ # override with a global version
+ *CORE::GLOBAL::oct = \&bigint::_oct_global;
+ }
else { die "unknown option $_[$i]"; }
}
my $class;
@@ -193,7 +223,7 @@ sub import
}
# Take care of octal/hexadecimal constants
- overload::constant binary => sub { _binary_constant(shift) };
+ overload::constant binary => sub { bigint::_binary_constant(shift) };
# if another big* was already loaded:
my ($package) = caller();
@@ -205,9 +235,6 @@ sub import
}
}
-sub inf () { Math::BigInt->binf(); }
-sub NaN () { Math::BigInt->bnan(); }
-
1;
__END__
@@ -230,6 +257,11 @@ bignum - Transparent BigNumber support for Perl
print 2 ** 256,"\n"; # a normal Perl scalar now
}
+ # for older Perls, note that this will be global:
+ use bignum qw/hex oct/;
+ print hex("0x1234567890123490"),"\n";
+ print oct("01234567890123490"),"\n";
+
=head1 DESCRIPTION
All operators (including basic math operations) are overloaded. Integer and
@@ -274,16 +306,16 @@ Since numbers are actually objects, you can call all the usual methods from
BigInt/BigFloat on them. This even works to some extent on expressions:
perl -Mbignum -le '$x = 1234; print $x->bdec()'
- perl -Mbignum -le 'print 1234->binc();'
- perl -Mbignum -le 'print 1234->binc->badd(6);'
- perl -Mbignum -le 'print +(1234)->binc()'
+ perl -Mbignum -le 'print 1234->copy()->binc();'
+ perl -Mbignum -le 'print 1234->copy()->binc->badd(6);'
+ perl -Mbignum -le 'print +(1234)->copy()->binc()'
(Note that print doesn't do what you expect if the expression starts with
'(' hence the C<+>)
You can even chain the operations together as usual:
- perl -Mbignum -le 'print 1234->binc->badd(6);'
+ perl -Mbignum -le 'print 1234->copy()->binc->badd(6);'
1241
Under bignum (or bigint or bigrat), Perl will "upgrade" the numbers
@@ -308,9 +340,7 @@ There is also C<use bigrat;> which gives you big rationals:
12381/10
The entire upgrading/downgrading is still experimental and might not work
-as you expect or may even have bugs.
-
-You might get errors like this:
+as you expect or may even have bugs. You might get errors like this:
Can't use an undefined value as an ARRAY reference at
/usr/local/lib/perl5/5.8.0/Math/BigInt/Calc.pm line 864
@@ -377,6 +407,18 @@ line. This means the following does not work:
This will be hopefully fixed soon ;)
+=item hex
+
+Override the build-in hex() method with a version that can handle big
+integers. Note that under Perl older than v5.9.4, this will be global
+and cannot be disabled with "no bigint;".
+
+=item oct
+
+Override the build-in oct() method with a version that can handle big
+integers. Note that under Perl older than v5.9.4, this will be global
+and cannot be disabled with "no bigint;".
+
=item v or version
This prints out the name and version of all modules used and then exits.
@@ -424,7 +466,7 @@ B<both> the original and the copy being destroyed:
$x = 9; $y = $x;
print $x->bmul(2), " ", $y,"\n"; # prints 18 18
-Using methods that do not modify, but testthe contents works:
+Using methods that do not modify, but test the contents works:
$x = 9; $y = $x;
$z = 9 if $x->is_zero(); # works fine
@@ -515,6 +557,41 @@ numbers or as a result of 0/0. '+inf' and '-inf' represent plus respectively
minus infinity. You will get '+inf' when dividing a positive number by 0, and
'-inf' when dividing any negative number by 0.
+=head1 CAVAETS
+
+=over 2
+
+=item in_effect()
+
+This method only works on Perl v5.9.4 or later.
+
+=item hex()/oct()
+
+C<bigint> overrides these routines with versions that can also handle
+big integer values. Under Perl prior to version v5.9.4, however, this
+will not happen unless you specifically ask for it with the two
+import tags "hex" and "oct" - and then it will be global and cannot be
+disabled inside a scope with "no bigint":
+
+ use bigint qw/hex oct/;
+
+ print hex("0x1234567890123456");
+ {
+ no bigint;
+ print hex("0x1234567890123456");
+ }
+
+The second call to hex() will warn about a non-portable constant.
+
+Compare this to:
+
+ use bigint;
+
+ # will warn only under older than v5.9.4
+ print hex("0x1234567890123456");
+
+=back
+
=head1 MODULES USED
C<bignum> is just a thin wrapper around various modules of the Math::BigInt
diff --git a/lib/bignum/t/bigint.t b/lib/bignum/t/bigint.t
index 03fb11bebe..baf76a3005 100755
--- a/lib/bignum/t/bigint.t
+++ b/lib/bignum/t/bigint.t
@@ -10,10 +10,10 @@ BEGIN
$| = 1;
chdir 't' if -d 't';
unshift @INC, '../lib';
- plan tests => 36;
+ plan tests => 51;
}
-use bigint;
+use bigint qw/hex oct/;
###############################################################################
# _constant tests
@@ -88,6 +88,29 @@ ok (bigint->round_mode('odd'),'odd');
ok (bigint->round_mode(),'odd');
###############################################################################
+# hex() and oct()
+
+my $c = 'Math::BigInt';
+
+ok (ref(hex(1)), $c);
+ok (ref(hex(0x1)), $c);
+ok (ref(hex("af")), $c);
+ok (hex("af"), Math::BigInt->new(0xaf));
+ok (ref(hex("0x1")), $c);
+
+ok (ref(oct("0x1")), $c);
+ok (ref(oct("01")), $c);
+ok (ref(oct("0b01")), $c);
+ok (ref(oct("1")), $c);
+ok (ref(oct(" 1")), $c);
+ok (ref(oct(" 0x1")), $c);
+
+ok (ref(oct(0x1)), $c);
+ok (ref(oct(01)), $c);
+ok (ref(oct(0b01)), $c);
+ok (ref(oct(1)), $c);
+
+###############################################################################
###############################################################################
# Perl 5.005 does not like ok ($x,undef)
diff --git a/lib/bignum/t/bignum.t b/lib/bignum/t/bignum.t
index ee5b8d4375..fe299a2e24 100755
--- a/lib/bignum/t/bignum.t
+++ b/lib/bignum/t/bignum.t
@@ -10,10 +10,10 @@ BEGIN
$| = 1;
chdir 't' if -d 't';
unshift @INC, '../lib';
- plan tests => 20;
+ plan tests => 35;
}
-use bignum;
+use bignum qw/oct hex/;
###############################################################################
# general tests
@@ -59,6 +59,29 @@ ok (bignum->round_mode('odd'),'odd');
ok (bignum->round_mode(),'odd');
###############################################################################
+# hex() and oct()
+
+my $c = 'Math::BigInt';
+
+ok (ref(hex(1)), $c);
+ok (ref(hex(0x1)), $c);
+ok (ref(hex("af")), $c);
+ok (hex("af"), Math::BigInt->new(0xaf));
+ok (ref(hex("0x1")), $c);
+
+ok (ref(oct("0x1")), $c);
+ok (ref(oct("01")), $c);
+ok (ref(oct("0b01")), $c);
+ok (ref(oct("1")), $c);
+ok (ref(oct(" 1")), $c);
+ok (ref(oct(" 0x1")), $c);
+
+ok (ref(oct(0x1)), $c);
+ok (ref(oct(01)), $c);
+ok (ref(oct(0b01)), $c);
+ok (ref(oct(1)), $c);
+
+###############################################################################
###############################################################################
# Perl 5.005 does not like ok ($x,undef)
diff --git a/lib/bignum/t/bigrat.t b/lib/bignum/t/bigrat.t
index c8162fc719..972b83cde2 100755
--- a/lib/bignum/t/bigrat.t
+++ b/lib/bignum/t/bigrat.t
@@ -10,10 +10,10 @@ BEGIN
$| = 1;
chdir 't' if -d 't';
unshift @INC, '../lib';
- plan tests => 25;
+ plan tests => 40;
}
-use bigrat;
+use bigrat qw/oct hex/;
###############################################################################
# general tests
@@ -63,6 +63,29 @@ ok (bigrat->round_mode('odd'),'odd');
ok (bigrat->round_mode(),'odd');
###############################################################################
+# hex() and oct()
+
+my $c = 'Math::BigInt';
+
+ok (ref(hex(1)), $c);
+ok (ref(hex(0x1)), $c);
+ok (ref(hex("af")), $c);
+ok (hex("af"), Math::BigInt->new(0xaf));
+ok (ref(hex("0x1")), $c);
+
+ok (ref(oct("0x1")), $c);
+ok (ref(oct("01")), $c);
+ok (ref(oct("0b01")), $c);
+ok (ref(oct("1")), $c);
+ok (ref(oct(" 1")), $c);
+ok (ref(oct(" 0x1")), $c);
+
+ok (ref(oct(0x1)), $c);
+ok (ref(oct(01)), $c);
+ok (ref(oct(0b01)), $c);
+ok (ref(oct(1)), $c);
+
+###############################################################################
###############################################################################
# Perl 5.005 does not like ok ($x,undef)
diff --git a/lib/bignum/t/in_effect.t b/lib/bignum/t/in_effect.t
index d2545e48cf..b163f12b8f 100644
--- a/lib/bignum/t/in_effect.t
+++ b/lib/bignum/t/in_effect.t
@@ -23,7 +23,7 @@ can_ok ('bignum', qw/in_effect/);
can_ok ('bigrat', qw/in_effect/);
SKIP: {
- skip ('Need at least Perl v5.9.4', 3) unless $] > 5.009004;
+ skip ('Need at least Perl v5.9.4', 3) if $] < "5.009005";
is (bigint::in_effect(), 1, 'bigint in effect');
is (bignum::in_effect(), 1, 'bignum in effect');
diff --git a/lib/bignum/t/scope_f.t b/lib/bignum/t/scope_f.t
index e2d4417484..dd748e11c8 100644
--- a/lib/bignum/t/scope_f.t
+++ b/lib/bignum/t/scope_f.t
@@ -1,7 +1,7 @@
#!/usr/bin/perl -w
###############################################################################
-# Test no bignum;
+# Test "no bignum;" and overloading of hex()/oct() for newer Perls
use Test::More;
use strict;
@@ -11,20 +11,31 @@ BEGIN
$| = 1;
chdir 't' if -d 't';
unshift @INC, '../lib';
- plan tests => 6;
+ plan tests => 10;
}
+# no :hex and :oct means these do not get overloaded for older Perls:
use bignum;
isnt (ref(1), '', 'is in effect');
isnt (ref(2.0), '', 'is in effect');
isnt (ref(0x20), '', 'is in effect');
+SKIP: {
+ skip ('Need at least Perl v5.9.4', 2) if $] < 5.009004;
+
+ is (ref(hex(9)), 'Math::BigInt', 'hex is overloaded');
+ is (ref(oct(07)), 'Math::BigInt', 'oct is overloaded');
+ }
+
{
no bignum;
is (ref(1), '', 'is not in effect');
is (ref(2.0), '', 'is not in effect');
is (ref(0x20), '', 'is not in effect');
+
+ isnt (ref(hex(9)), 'Math::BigInt', 'hex is not overloaded');
+ isnt (ref(oct(07)), 'Math::BigInt', 'oct is not overloaded');
}
diff --git a/lib/bignum/t/scope_i.t b/lib/bignum/t/scope_i.t
index d663401659..4e2f1e6e7f 100644
--- a/lib/bignum/t/scope_i.t
+++ b/lib/bignum/t/scope_i.t
@@ -1,7 +1,7 @@
#!/usr/bin/perl -w
###############################################################################
-# Test no bigint;
+# Test "no bigint;" and overloading of hex()/oct() for newer Perls
use Test::More;
use strict;
@@ -11,20 +11,32 @@ BEGIN
$| = 1;
chdir 't' if -d 't';
unshift @INC, '../lib';
- plan tests => 6;
+ plan tests => 10;
}
+# no :hex and :oct means these do not get overloaded for older Perls:
use bigint;
isnt (ref(1), '', 'is in effect');
isnt (ref(2.0), '', 'is in effect');
isnt (ref(0x20), '', 'is in effect');
+SKIP: {
+ skip ('Need at least Perl v5.9.4', 2) if $] < "5.009004"; # quote due to "use bigint;"
+
+ is (ref(hex(9)), 'Math::BigInt', 'hex is overloaded');
+ is (ref(oct(07)), 'Math::BigInt', 'oct is overloaded');
+ }
+
{
no bigint;
is (ref(1), '', 'is not in effect');
is (ref(2.0), '', 'is not in effect');
is (ref(0x20), '', 'is not in effect');
+
+ isnt (ref(hex(9)), 'Math::BigInt', 'hex is not overloaded');
+ isnt (ref(oct(07)), 'Math::BigInt', 'oct is not overloaded');
+
}
diff --git a/lib/bignum/t/scope_r.t b/lib/bignum/t/scope_r.t
index 888398867e..784fe0e6b0 100644
--- a/lib/bignum/t/scope_r.t
+++ b/lib/bignum/t/scope_r.t
@@ -1,7 +1,7 @@
#!/usr/bin/perl -w
###############################################################################
-# Test no bigint;
+# Test "no bigrat;" and overloading of hex()/oct() for newer Perls
use Test::More;
use strict;
@@ -11,20 +11,31 @@ BEGIN
$| = 1;
chdir 't' if -d 't';
unshift @INC, '../lib';
- plan tests => 6;
+ plan tests => 10;
}
+# no :hex and :oct means these do not get overloaded for older Perls:
use bigrat;
isnt (ref(1), '', 'is in effect');
isnt (ref(2.0), '', 'is in effect');
isnt (ref(0x20), '', 'is in effect');
+SKIP: {
+ skip ('Need at least Perl v5.9.4', 2) if $] < 5.009004;
+
+ is (ref(hex(9)), 'Math::BigInt', 'hex is overloaded');
+ is (ref(oct(07)), 'Math::BigInt', 'oct is overloaded');
+ }
+
{
no bigrat;
is (ref(1), '', 'is not in effect');
is (ref(2.0), '', 'is not in effect');
is (ref(0x20), '', 'is not in effect');
+
+ isnt (ref(hex(9)), 'Math::BigInt', 'hex is not overloaded');
+ isnt (ref(oct(07)), 'Math::BigInt', 'oct is not overloaded');
}
diff --git a/lib/bigrat.pm b/lib/bigrat.pm
index 7888aa4481..884e9dab31 100644
--- a/lib/bigrat.pm
+++ b/lib/bigrat.pm
@@ -3,15 +3,22 @@ use 5.006002;
$VERSION = '0.22';
require Exporter;
-@ISA = qw( Exporter );
+@ISA = qw( bigint );
@EXPORT_OK = qw( );
@EXPORT = qw( inf NaN );
use strict;
use overload;
+require bigint; # no "use" to avoid callind import
##############################################################################
+BEGIN
+ {
+ *inf = \&bigint::inf;
+ *NaN = \&bigint::NaN;
+ }
+
# These are all alike, and thus faked by AUTOLOAD
my @faked = qw/round_mode accuracy precision div_scale/;
@@ -48,24 +55,6 @@ sub AUTOLOAD
Carp::croak ("Can't call bigrat\-\>$name, not a valid method");
}
-sub upgrade
- {
- $Math::BigInt::upgrade;
- }
-
-sub _binary_constant
- {
- # this takes a binary/hexadecimal/octal constant string and returns it
- # as string suitable for new. Basically it converts octal to decimal, and
- # passes every thing else unmodified back.
- my $string = shift;
-
- return Math::BigInt->new($string) if $string =~ /^0[bx]/;
-
- # so it must be an octal constant
- Math::BigInt->from_oct($string);
- }
-
sub unimport
{
$^H{bigrat} = undef; # no longer in effect
@@ -79,6 +68,25 @@ sub in_effect
$hinthash->{bigrat};
}
+#############################################################################
+# the following two routines are for Perl 5.9.4 or later and are lexical
+
+sub _hex
+ {
+ return CORE::hex($_[0]) unless in_effect(1);
+ my $i = $_[0];
+ $i = '0x'.$i unless $i =~ /^0x/;
+ Math::BigInt->new($i);
+ }
+
+sub _oct
+ {
+ return CORE::oct($_[0]) unless in_effect(1);
+ my $i = $_[0];
+ return Math::BigInt->from_oct($i) if $i =~ /^0[0-7]/;
+ Math::BigInt->new($i);
+ }
+
sub import
{
my $self = shift;
@@ -87,6 +95,13 @@ sub import
$^H{bigrat} = 1; # we are in effect
+ # for newer Perls always override hex() and oct() with a lexical version:
+ if ($] > 5.009004)
+ {
+ no warnings 'redefine';
+ *CORE::GLOBAL::oct = \&_oct;
+ *CORE::GLOBAL::hex = \&_hex;
+ }
# some defaults
my $lib = ''; my $lib_kind = 'try'; my $upgrade = 'Math::BigFloat';
@@ -133,6 +148,18 @@ sub import
$trace = 1;
splice @a, $j, 1; $j --;
}
+ elsif ($_[$i] eq 'hex')
+ {
+ splice @a, $j, 1; $j --;
+ no warnings 'redefine';
+ *CORE::GLOBAL::hex = \&bigint::_hex_global;
+ }
+ elsif ($_[$i] eq 'oct')
+ {
+ splice @a, $j, 1; $j --;
+ no warnings 'redefine';
+ *CORE::GLOBAL::oct = \&bigint::_oct_global;
+ }
else
{
die ("unknown option $_[$i]");
@@ -184,7 +211,7 @@ sub import
}
# Take care of octal/hexadecimal constants
- overload::constant binary => sub { _binary_constant(shift) };
+ overload::constant binary => sub { bigint::_binary_constant(shift) };
# if another big* was already loaded:
my ($package) = caller();
@@ -196,9 +223,6 @@ sub import
}
}
-sub inf () { Math::BigInt->binf(); }
-sub NaN () { Math::BigInt->bnan(); }
-
1;
__END__
@@ -219,6 +243,11 @@ bigrat - Transparent BigNumber/BigRational support for Perl
print 1/3,"\n"; # 0.33333...
}
+ # Note that this will make hex() and oct() be globally overriden:
+ use bigrat qw/hex oct/;
+ print hex("0x1234567890123490"),"\n";
+ print oct("01234567890123490"),"\n";
+
=head1 DESCRIPTION
All operators (including basic math operations) are overloaded. Integer and
@@ -406,6 +435,18 @@ line. This means the following does not work:
This will be hopefully fixed soon ;)
+=item hex
+
+Override the build-in hex() method with a version that can handle big
+integers. Note that under Perl v5.9.4 or ealier, this will be global
+and cannot be disabled with "no bigint;".
+
+=item oct
+
+Override the build-in oct() method with a version that can handle big
+integers. Note that under Perl v5.9.4 or ealier, this will be global
+and cannot be disabled with "no bigint;".
+
=item v or version
This prints out the name and version of all modules used and then exits.
@@ -414,6 +455,41 @@ This prints out the name and version of all modules used and then exits.
=back
+=head1 CAVAETS
+
+=over 2
+
+=item in_effect()
+
+This method only works on Perl v5.9.4 or later.
+
+=item hex()/oct()
+
+C<bigint> overrides these routines with versions that can also handle
+big integer values. Under Perl prior to version v5.9.4, however, this
+will not happen unless you specifically ask for it with the two
+import tags "hex" and "oct" - and then it will be global and cannot be
+disabled inside a scope with "no bigint":
+
+ use bigint qw/hex oct/;
+
+ print hex("0x1234567890123456");
+ {
+ no bigint;
+ print hex("0x1234567890123456");
+ }
+
+The second call to hex() will warn about a non-portable constant.
+
+Compare this to:
+
+ use bigint;
+
+ # will warn only under Perl older than v5.9.4
+ print hex("0x1234567890123456");
+
+=back
+
=head1 EXAMPLES
perl -Mbigrat -le 'print sqrt(33)'