From: Tels Date: Wed, 23 May 2007 21:35:58 +0000 (+0000) Subject: Re: perl pragma [PATCH] X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=4440d13a03087bebaa0394cf532fe7ebcda34a06;p=p5sagit%2Fp5-mst-13.2.git Re: perl pragma [PATCH] Message-Id: <200705232135.59546@bloodgate.com> p4raw-id: //depot/perl@31269 --- diff --git a/MANIFEST b/MANIFEST index cef3d60..a481c6b 100644 --- a/MANIFEST +++ b/MANIFEST @@ -1456,7 +1456,7 @@ lib/bigfloat.pl An arbitrary precision floating point package lib/bigfloatpl.t See if bigfloat.pl works lib/bigint.pl An arbitrary precision integer arithmetic package lib/bigintpl.t See if bigint.pl works -lib/bigint.pm bignum +lib/bigint.pm bigint lib/bignum.pm bignum lib/bignum/t/bigexp.t See if bignum works lib/bignum/t/bigint.t See if bigint works @@ -1467,13 +1467,17 @@ lib/bignum/t/bninfnan.t See if bignum works lib/bignum/t/bn_lite.t See if bignum with Math::BigInt::Lite works lib/bignum/t/brinfnan.t See if bignum works lib/bignum/t/br_lite.t See if bigrat with Math::BigInt::Lite works -lib/bignum/t/infnan.inc See if bignum works +lib/bignum/t/infnan.inc See if bignum with inf/NaN works +lib/bignum/t/in_effect.t See if in_effect() works lib/bignum/t/option_a.t See if bignum a => X works lib/bignum/t/option_l.t See if bignum l => X works lib/bignum/t/option_p.t See if bignum p => X works lib/bignum/t/ratopt_a.t See if bigrat a => X works +lib/bignum/t/scope_i.t See if no bigint works +lib/bignum/t/scope_f.t See if no bignum works +lib/bignum/t/scope_r.t See if no bigrat works lib/bigrat.pl An arbitrary precision rational arithmetic package -lib/bigrat.pm bignum +lib/bigrat.pm bigrat lib/blib.pm For "use blib" lib/blib.t blib.pm test lib/bytes_heavy.pl Support routines for byte pragma diff --git a/lib/bigint.pm b/lib/bigint.pm index 7b5f150..5f25144 100644 --- a/lib/bigint.pm +++ b/lib/bigint.pm @@ -1,7 +1,7 @@ package bigint; use 5.006002; -$VERSION = '0.10'; +$VERSION = '0.22'; use Exporter; @ISA = qw( Exporter ); @EXPORT_OK = qw( ); @@ -48,12 +48,6 @@ sub AUTOLOAD sub upgrade { - my $self = shift; - no strict 'refs'; -# if (defined $_[0]) -# { -# $Math::BigInt::upgrade = $_[0]; -# } $Math::BigInt::upgrade; } @@ -110,10 +104,25 @@ sub _float_constant $sign.$$miv.$mfv; # 123.45e+1 => 1234 } +sub unimport + { + $^H{bigint} = undef; # no longer in effect + overload::remove_constant('binary','','float','','integer'); + } + +sub in_effect + { + my $level = shift || 0; + my $hinthash = (caller($level))[10]; + $hinthash->{bigint}; + } + sub import { my $self = shift; + $^H{bigint} = 1; # we are in effect + # some defaults my $lib = ''; my $lib_kind = 'try'; @@ -198,7 +207,14 @@ sub import # Take care of octal/hexadecimal constants overload::constant binary => sub { _binary_constant(shift) }; - $self->export_to_level(1,$self,@a); # export inf and NaN + # if another big* was already loaded: + my ($package) = caller(); + + no strict 'refs'; + if (!defined *{"${package}::inf"}) + { + $self->export_to_level(1,$self,@a); # export inf and NaN + } } sub inf () { Math::BigInt->binf(); } @@ -221,6 +237,11 @@ bigint - Transparent BigInteger support for Perl print inf + 42,"\n"; # inf print NaN * 7,"\n"; # NaN + { + no bigint; + print 2 ** 256,"\n"; # a normal Perl scalar now + } + =head1 DESCRIPTION All operators (including basic math operations) are overloaded. Integer @@ -361,6 +382,20 @@ handle bareword C properly. Return the class that numbers are upgraded to, is in fact returning C<$Math::BigInt::upgrade>. +=item in_effect() + + use bigint; + + print "in effect\n" if bigint::in_effect; # true + { + no bigint; + print "in effect\n" if bigint::in_effect; # false + } + +Returns true or false if C is in effect in the current scope. + +This method only works on Perl v5.9.4 or later. + =back =head2 MATH LIBRARY diff --git a/lib/bignum.pm b/lib/bignum.pm index d9d73bb..3d79c52 100644 --- a/lib/bignum.pm +++ b/lib/bignum.pm @@ -1,7 +1,7 @@ package bignum; use 5.006002; -$VERSION = '0.21_02'; +$VERSION = '0.22'; use Exporter; @EXPORT_OK = qw( ); @EXPORT = qw( inf NaN ); @@ -49,13 +49,6 @@ sub AUTOLOAD sub upgrade { - my $self = shift; - no strict 'refs'; -# if (defined $_[0]) -# { -# $Math::BigInt::upgrade = $_[0]; -# $Math::BigFloat::upgrade = $_[0]; -# } $Math::BigInt::upgrade; } @@ -72,10 +65,25 @@ sub _binary_constant Math::BigInt->from_oct($string); } +sub unimport + { + $^H{bignum} = undef; # no longer in effect + overload::remove_constant('binary','','float','','integer'); + } + +sub in_effect + { + my $level = shift || 0; + my $hinthash = (caller($level))[10]; + $hinthash->{bignum}; + } + sub import { my $self = shift; + $^H{bignum} = 1; # we are in effect + # some defaults my $lib = ''; my $lib_kind = 'try'; my $upgrade = 'Math::BigFloat'; @@ -185,9 +193,16 @@ sub import } # Take care of octal/hexadecimal constants - overload::constant 'binary' => sub { _binary_constant(shift) }; + overload::constant binary => sub { _binary_constant(shift) }; - $self->export_to_level(1,$self,@a); # export inf and NaN + # if another big* was already loaded: + my ($package) = caller(); + + no strict 'refs'; + if (!defined *{"${package}::inf"}) + { + $self->export_to_level(1,$self,@a); # export inf and NaN + } } sub inf () { Math::BigInt->binf(); } @@ -210,6 +225,11 @@ bignum - Transparent BigNumber support for Perl print inf * inf,"\n"; # prints inf print NaN * 3,"\n"; # prints NaN + { + no bignum; + print 2 ** 256,"\n"; # a normal Perl scalar now + } + =head1 DESCRIPTION All operators (including basic math operations) are overloaded. Integer and @@ -374,7 +394,7 @@ the BigInt or BigFloat API. It is wise to use only the bxxx() notation, and not the fxxx() notation, though. This makes it possible that the underlying object might morph into a different class than BigFloat. -=head2 Caveat +=head2 Caveats But a warning is in order. When using the following to make a copy of a number, only a shallow copy will be made. @@ -429,6 +449,20 @@ handle bareword C properly. Return the class that numbers are upgraded to, is in fact returning C<$Math::BigInt::upgrade>. +=item in_effect() + + use bignum; + + print "in effect\n" if bignum::in_effect; # true + { + no bignum; + print "in effect\n" if bignum::in_effect; # false + } + +Returns true or false if C is in effect in the current scope. + +This method only works on Perl v5.9.4 or later. + =back =head2 Math Library diff --git a/lib/bignum/t/in_effect.t b/lib/bignum/t/in_effect.t new file mode 100644 index 0000000..d2545e4 --- /dev/null +++ b/lib/bignum/t/in_effect.t @@ -0,0 +1,42 @@ +#!/usr/bin/perl -w + +############################################################################### +# Test in_effect() + +use Test::More; +use strict; + +BEGIN + { + $| = 1; + chdir 't' if -d 't'; + unshift @INC, '../lib'; + plan tests => 9; + } + +use bigint; +use bignum; +use bigrat; + +can_ok ('bigint', qw/in_effect/); +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; + + is (bigint::in_effect(), 1, 'bigint in effect'); + is (bignum::in_effect(), 1, 'bignum in effect'); + is (bigrat::in_effect(), 1, 'bigrat in effect'); + } + +{ + no bigint; + no bignum; + no bigrat; + + is (bigint::in_effect(), undef, 'bigint not in effect'); + is (bignum::in_effect(), undef, 'bignum not in effect'); + is (bigrat::in_effect(), undef, 'bigrat not in effect'); +} + diff --git a/lib/bignum/t/scope_f.t b/lib/bignum/t/scope_f.t new file mode 100644 index 0000000..e2d4417 --- /dev/null +++ b/lib/bignum/t/scope_f.t @@ -0,0 +1,30 @@ +#!/usr/bin/perl -w + +############################################################################### +# Test no bignum; + +use Test::More; +use strict; + +BEGIN + { + $| = 1; + chdir 't' if -d 't'; + unshift @INC, '../lib'; + plan tests => 6; + } + +use bignum; + +isnt (ref(1), '', 'is in effect'); +isnt (ref(2.0), '', 'is in effect'); +isnt (ref(0x20), '', 'is in effect'); + +{ + no bignum; + + is (ref(1), '', 'is not in effect'); + is (ref(2.0), '', 'is not in effect'); + is (ref(0x20), '', 'is not in effect'); +} + diff --git a/lib/bignum/t/scope_i.t b/lib/bignum/t/scope_i.t new file mode 100644 index 0000000..d663401 --- /dev/null +++ b/lib/bignum/t/scope_i.t @@ -0,0 +1,30 @@ +#!/usr/bin/perl -w + +############################################################################### +# Test no bigint; + +use Test::More; +use strict; + +BEGIN + { + $| = 1; + chdir 't' if -d 't'; + unshift @INC, '../lib'; + plan tests => 6; + } + +use bigint; + +isnt (ref(1), '', 'is in effect'); +isnt (ref(2.0), '', 'is in effect'); +isnt (ref(0x20), '', 'is in effect'); + +{ + no bigint; + + is (ref(1), '', 'is not in effect'); + is (ref(2.0), '', 'is not in effect'); + is (ref(0x20), '', 'is not in effect'); +} + diff --git a/lib/bignum/t/scope_r.t b/lib/bignum/t/scope_r.t new file mode 100644 index 0000000..8883988 --- /dev/null +++ b/lib/bignum/t/scope_r.t @@ -0,0 +1,30 @@ +#!/usr/bin/perl -w + +############################################################################### +# Test no bigint; + +use Test::More; +use strict; + +BEGIN + { + $| = 1; + chdir 't' if -d 't'; + unshift @INC, '../lib'; + plan tests => 6; + } + +use bigrat; + +isnt (ref(1), '', 'is in effect'); +isnt (ref(2.0), '', 'is in effect'); +isnt (ref(0x20), '', 'is in effect'); + +{ + no bigrat; + + is (ref(1), '', 'is not in effect'); + is (ref(2.0), '', 'is not in effect'); + is (ref(0x20), '', 'is not in effect'); +} + diff --git a/lib/bigrat.pm b/lib/bigrat.pm index a7a2c43..7888aa4 100644 --- a/lib/bigrat.pm +++ b/lib/bigrat.pm @@ -1,7 +1,7 @@ package bigrat; use 5.006002; -$VERSION = '0.10'; +$VERSION = '0.22'; require Exporter; @ISA = qw( Exporter ); @EXPORT_OK = qw( ); @@ -50,13 +50,6 @@ sub AUTOLOAD sub upgrade { - my $self = shift; - no strict 'refs'; -# if (defined $_[0]) -# { -# $Math::BigInt::upgrade = $_[0]; -# $Math::BigFloat::upgrade = $_[0]; -# } $Math::BigInt::upgrade; } @@ -73,12 +66,27 @@ sub _binary_constant Math::BigInt->from_oct($string); } +sub unimport + { + $^H{bigrat} = undef; # no longer in effect + overload::remove_constant('binary','','float','','integer'); + } + +sub in_effect + { + my $level = shift || 0; + my $hinthash = (caller($level))[10]; + $hinthash->{bigrat}; + } + sub import { my $self = shift; # see also bignum->import() for additional comments + $^H{bigrat} = 1; # we are in effect + # some defaults my $lib = ''; my $lib_kind = 'try'; my $upgrade = 'Math::BigFloat'; @@ -178,7 +186,14 @@ sub import # Take care of octal/hexadecimal constants overload::constant binary => sub { _binary_constant(shift) }; - $self->export_to_level(1,$self,@a); # export inf and NaN + # if another big* was already loaded: + my ($package) = caller(); + + no strict 'refs'; + if (!defined *{"${package}::inf"}) + { + $self->export_to_level(1,$self,@a); # export inf and NaN + } } sub inf () { Math::BigInt->binf(); } @@ -196,9 +211,14 @@ bigrat - Transparent BigNumber/BigRational support for Perl use bigrat; - $x = 2 + 4.5,"\n"; # BigFloat 6.5 + print 2 + 4.5,"\n"; # BigFloat 6.5 print 1/3 + 1/4,"\n"; # produces 7/12 + { + no bigrat; + print 1/3,"\n"; # 0.33333... + } + =head1 DESCRIPTION All operators (including basic math operations) are overloaded. Integer and @@ -282,6 +302,20 @@ handle bareword C properly. Return the class that numbers are upgraded to, is in fact returning C<$Math::BigInt::upgrade>. +=item in_effect() + + use bigrat; + + print "in effect\n" if bigrat::in_effect; # true + { + no bigrat; + print "in effect\n" if bigrat::in_effect; # false + } + +Returns true or false if C is in effect in the current scope. + +This method only works on Perl v5.9.4 or later. + =back =head2 MATH LIBRARY