Change 31403 by [EMAIL PROTECTED] on 2007/06/17 13:28:00
Subject: bignum 0.22 take 4 (hex()/oct() overloading)
From: Tels <[EMAIL PROTECTED]>
Date: Sat, 16 Jun 2007 14:33:47 +0200
Message-Id: <[EMAIL PROTECTED]>
Affected files ...
... //depot/perl/lib/bigint.pm#16 edit
... //depot/perl/lib/bignum.pm#17 edit
... //depot/perl/lib/bignum/t/bigint.t#4 edit
... //depot/perl/lib/bignum/t/bignum.t#6 edit
... //depot/perl/lib/bignum/t/bigrat.t#4 edit
... //depot/perl/lib/bignum/t/in_effect.t#2 edit
... //depot/perl/lib/bignum/t/scope_f.t#2 edit
... //depot/perl/lib/bignum/t/scope_i.t#2 edit
... //depot/perl/lib/bignum/t/scope_r.t#2 edit
... //depot/perl/lib/bigrat.pm#14 edit
Differences ...
==== //depot/perl/lib/bigint.pm#16 (text) ====
Index: perl/lib/bigint.pm
--- perl/lib/bigint.pm#15~31368~ 2007-06-12 01:39:00.000000000 -0700
+++ perl/lib/bigint.pm 2007-06-17 06:28:00.000000000 -0700
@@ -117,12 +117,55 @@
$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 @@
$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 @@
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 @@
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 @@
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 @@
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
==== //depot/perl/lib/bignum.pm#17 (text) ====
Index: perl/lib/bignum.pm
--- perl/lib/bignum.pm#16~31269~ 2007-05-24 10:18:24.000000000 -0700
+++ perl/lib/bignum.pm 2007-06-17 06:28:00.000000000 -0700
@@ -3,15 +3,22 @@
$VERSION = '0.22';
use Exporter;
[EMAIL PROTECTED] = qw( bigint );
@EXPORT_OK = qw( );
@EXPORT = qw( inf NaN );
[EMAIL PROTECTED] = 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 @@
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 @@
$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 @@
$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 @@
}
# 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 inf () { Math::BigInt->binf(); }
-sub NaN () { Math::BigInt->bnan(); }
-
1;
__END__
@@ -230,6 +257,11 @@
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 @@
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 @@
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 @@
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 @@
$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 @@
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
==== //depot/perl/lib/bignum/t/bigint.t#4 (xtext) ====
Index: perl/lib/bignum/t/bigint.t
--- perl/lib/bignum/t/bigint.t#3~30360~ 2007-02-19 11:27:27.000000000 -0800
+++ perl/lib/bignum/t/bigint.t 2007-06-17 06:28:00.000000000 -0700
@@ -10,10 +10,10 @@
$| = 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');
###############################################################################
+# 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)
==== //depot/perl/lib/bignum/t/bignum.t#6 (xtext) ====
Index: perl/lib/bignum/t/bignum.t
--- perl/lib/bignum/t/bignum.t#5~20000~ 2003-07-04 13:58:05.000000000 -0700
+++ perl/lib/bignum/t/bignum.t 2007-06-17 06:28:00.000000000 -0700
@@ -10,10 +10,10 @@
$| = 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');
###############################################################################
+# 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)
==== //depot/perl/lib/bignum/t/bigrat.t#4 (xtext) ====
Index: perl/lib/bignum/t/bigrat.t
--- perl/lib/bignum/t/bigrat.t#3~20000~ 2003-07-04 13:58:05.000000000 -0700
+++ perl/lib/bignum/t/bigrat.t 2007-06-17 06:28:00.000000000 -0700
@@ -10,10 +10,10 @@
$| = 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');
###############################################################################
+# 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)
==== //depot/perl/lib/bignum/t/in_effect.t#2 (text) ====
Index: perl/lib/bignum/t/in_effect.t
--- perl/lib/bignum/t/in_effect.t#1~31269~ 2007-05-24 10:18:24.000000000
-0700
+++ perl/lib/bignum/t/in_effect.t 2007-06-17 06:28:00.000000000 -0700
@@ -23,7 +23,7 @@
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');
==== //depot/perl/lib/bignum/t/scope_f.t#2 (text) ====
Index: perl/lib/bignum/t/scope_f.t
--- perl/lib/bignum/t/scope_f.t#1~31269~ 2007-05-24 10:18:24.000000000
-0700
+++ perl/lib/bignum/t/scope_f.t 2007-06-17 06:28:00.000000000 -0700
@@ -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 @@
$| = 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');
}
==== //depot/perl/lib/bignum/t/scope_i.t#2 (text) ====
Index: perl/lib/bignum/t/scope_i.t
--- perl/lib/bignum/t/scope_i.t#1~31269~ 2007-05-24 10:18:24.000000000
-0700
+++ perl/lib/bignum/t/scope_i.t 2007-06-17 06:28:00.000000000 -0700
@@ -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 @@
$| = 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');
+
}
==== //depot/perl/lib/bignum/t/scope_r.t#2 (text) ====
Index: perl/lib/bignum/t/scope_r.t
--- perl/lib/bignum/t/scope_r.t#1~31269~ 2007-05-24 10:18:24.000000000
-0700
+++ perl/lib/bignum/t/scope_r.t 2007-06-17 06:28:00.000000000 -0700
@@ -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 @@
$| = 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');
}
==== //depot/perl/lib/bigrat.pm#14 (text) ====
Index: perl/lib/bigrat.pm
--- perl/lib/bigrat.pm#13~31269~ 2007-05-24 10:18:24.000000000 -0700
+++ perl/lib/bigrat.pm 2007-06-17 06:28:00.000000000 -0700
@@ -3,15 +3,22 @@
$VERSION = '0.22';
require Exporter;
[EMAIL PROTECTED] = qw( Exporter );
[EMAIL PROTECTED] = 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 @@
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 @@
$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 @@
$^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 @@
$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 @@
}
# 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 inf () { Math::BigInt->binf(); }
-sub NaN () { Math::BigInt->bnan(); }
-
1;
__END__
@@ -219,6 +243,11 @@
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 @@
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 @@
=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)'
End of Patch.