| 1 | #!perl | ||
| 2 | |||
| 3 | package Math::BigInt::Named; | ||
| 4 | |||
| 5 | use 5.006001; | ||
| 6 | use strict; | ||
| 7 | use warnings; | ||
| 8 | |||
| 9 | use Carp qw( carp croak ); | ||
| 10 | |||
| 11 | use Math::BigInt 1.97; | ||
| 12 | our @ISA = qw(Math::BigInt); | ||
| 13 | |||
| 14 | our $VERSION = '0.07'; | ||
| 15 | |||
| 16 | # Globals. | ||
| 17 | |||
| 18 | our ($accuracy, $precision, $round_mode, $div_scale); | ||
| 19 | $accuracy = undef; | ||
| 20 | $precision = undef; | ||
| 21 | $round_mode = 'even'; | ||
| 22 | $div_scale = 40; | ||
| 23 | |||
| 24 | # Not all of them exist yet. | ||
| 25 | my $LANGUAGE = { | ||
| 26 | en => 'english', | ||
| 27 | de => 'german', | ||
| 28 | sp => 'spanish', | ||
| 29 | fr => 'french', | ||
| 30 | ro => 'romana', | ||
| 31 | it => 'italian', | ||
| 32 | no => 'norwegian', | ||
| 33 | }; | ||
| 34 | |||
| 35 | # Index of languages that have been loaded. | ||
| 36 | |||
| 37 | my $LOADED = { }; | ||
| 38 | |||
| 39 | sub name { | ||
| 40 | # output the name of the number | ||
| 41 | my $x = shift; | ||
| 42 | |||
| 43 | # make Math::BigInt::Name -> name(123) work | ||
| 44 | $x = $x -> new(shift) unless ref($x); | ||
| 45 | |||
| 46 | return 'NaN' if $x -> is_nan(); | ||
| 47 | |||
| 48 | my @args = (); | ||
| 49 | if (@_) { | ||
| 50 | if (ref($_[0]) eq 'HASH') { | ||
| 51 | carp "When the options are given as a hash ref, additional", | ||
| 52 | " arguments are ignored" if @_ > 1; | ||
| 53 | @args = %{ $_[0] }; | ||
| 54 | } else { | ||
| 55 | @args = @_; | ||
| 56 | } | ||
| 57 | } | ||
| 58 | |||
| 59 | my $lang; | ||
| 60 | while (@args) { | ||
| 61 | my $param = shift @args; | ||
| 62 | croak "Parameter name can not be undefined" unless defined $param; | ||
| 63 | croak "Parameter name can not be an empty string" unless length $param; | ||
| 64 | |||
| 65 | if ($param =~ /^lang(uage)?$/) { | ||
| 66 | $lang = shift @args; | ||
| 67 | croak "Language can not be undefined" unless defined $lang; | ||
| 68 | croak "Language can not be an empty string" unless length $lang; | ||
| 69 | next; | ||
| 70 | } | ||
| 71 | |||
| 72 | croak "Invalid parameter '$param'"; | ||
| 73 | } | ||
| 74 | |||
| 75 | $lang = 'english' unless defined $lang; | ||
| 76 | $lang = $LANGUAGE -> {$lang} if exists $LANGUAGE -> {$lang}; # en => english | ||
| 77 | |||
| 78 | $lang = 'Math::BigInt::Named::' . ucfirst($lang); | ||
| 79 | |||
| 80 | if (!defined $LOADED -> {$lang}) { | ||
| 81 | my $file = $lang; | ||
| 82 | $file =~ s|::|/|g; | ||
| 83 | $file .= ".pm"; | ||
| 84 | eval { require $file; }; | ||
| 85 | croak $@ if $@; | ||
| 86 | $LOADED -> {$lang} = 1; | ||
| 87 | } | ||
| 88 | |||
| 89 | my $y = $lang -> new($x); | ||
| 90 | $y -> name(); | ||
| 91 | } | ||
| 92 | |||
| 93 | sub from_name { | ||
| 94 | # Create a Math::BigInt::Name from a name string. Not implemented. | ||
| 95 | |||
| 96 | my $x = Math::BigInt -> bnan(); | ||
| 97 | } | ||
| 98 | |||
| 99 | 1; | ||
| 100 | |||
| 101 | __END__ |