This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Re: RFC: bigint et. al exporting PI method? [PATCH]
[perl5.git] / lib / bigrat.pm
index 884e9da..e185d4f 100644 (file)
@@ -4,7 +4,7 @@ use 5.006002;
 $VERSION = '0.22';
 require Exporter;
 @ISA           = qw( bigint );
-@EXPORT_OK     = qw( ); 
+@EXPORT_OK     = qw( PI e ); 
 @EXPORT                = qw( inf NaN ); 
 
 use strict;
@@ -95,12 +95,12 @@ sub import
 
   $^H{bigrat} = 1;                                     # we are in effect
 
+  my ($hex,$oct);
   # 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;
+    $oct = \&_oct;
+    $hex = \&_hex;
     }
   # some defaults
   my $lib = ''; my $lib_kind = 'try'; my $upgrade = 'Math::BigFloat';
@@ -151,16 +151,14 @@ sub import
     elsif ($_[$i] eq 'hex')
       {
       splice @a, $j, 1; $j --;
-      no warnings 'redefine';
-      *CORE::GLOBAL::hex = \&bigint::_hex_global;
+      $hex = \&bigint::_hex_global;
       }
     elsif ($_[$i] eq 'oct')
       {
       splice @a, $j, 1; $j --;
-      no warnings 'redefine';
-      *CORE::GLOBAL::oct = \&bigint::_oct_global;
+      $oct = \&bigint::_oct_global;
       }
-    else
+    elsif ($_[$i] !~ /^(PI|e)\z/)
       {
       die ("unknown option $_[$i]");
       }
@@ -221,7 +219,15 @@ sub import
     {
     $self->export_to_level(1,$self,@a);           # export inf and NaN
     }
+  {
+    no warnings 'redefine';
+    *CORE::GLOBAL::oct = $oct if $oct;
+    *CORE::GLOBAL::hex = $hex if $hex;
   }
+  }
+
+sub PI { local $Math::BigFloat::upgrade = undef; Math::BigFloat::bpi(@_); }
+sub e  { local $Math::BigFloat::upgrade = undef; Math::BigFloat->bone()->bexp(@_); }
 
 1;
 
@@ -326,6 +332,14 @@ handle bareword C<inf> properly.
 A shortcut to return Math::BigInt->bnan(). Useful because Perl does not always
 handle bareword C<NaN> properly.
 
+=item e()
+
+Returns Euler's number C<e>, aka exp(1), to the given number of digits.
+
+=item PI()
+
+Returns PI to the given number of digits.
+
 =item upgrade()
 
 Return the class that numbers are upgraded to, is in fact returning