Acme-Numbers
view release on metacpan or search on metacpan
lib/Acme/Numbers.pm view on Meta::CPAN
package Acme::Numbers;
use strict;
use Lingua::EN::Words2Nums qw(words2nums);
our $AUTOLOAD;
our $VERSION = '1.2';
=head1 NAME
Acme::Numbers - a fluent numeric interface
=head1 SYNOPSIS
use Acme::Numbers;
print one."\n"; # prints 1
print two.hundred."\n"; # prints 200
print forty.two."\n"; # prints 42
print six.hundred.and.sixty.six."\n"; # prints 666
print one.million."\n"; # prints 1000000
print three.point.one.four."\n"; # prints 3.14
print one.point.zero.two."\n"; # prints 1.02
print zero.point.zero.five."\n"; # prints 0.05
print four.pounds."\n"; # prints "4.00"
print four.pounds.five."\n"; # prints "4.05"
print four.pounds.fifty."\n"; # prints "4.50"
print four.pounds.fifty.five."\n"; # prints "4.55"
print fifty.pence."\n"; # prints "0.50"
print fifty.five.pence."\n"; # prints "0.55"
print four.pounds.fifty.pence."\n"; # prints "4.50"
print four.pounds.and.fifty.p."\n"; # prints "4.50"
print fifty.cents."\n"; # prints "0.50"
print fifty.five.cents."\n"; # prints "0.55"
print four.dollars.fifty.cents."\n"; # prints "4.55"
=head1 DESCRIPTION
Inspired by this post
http://beautifulcode.oreillynet.com/2007/12/the_cardinality_of_a_fluent_in.php
and a burning curiosity. At leats, I hope the burning
was curiosity.
=head1 ONE BIIIIIIIIIIIILLION
By default billion is 10**12 because, dammit, that's right.
If you want it to be an American billion then do
use Acme::Numbers billion => 10**9;
Setting this automatically changes all the larger numbers
(trillion, quadrillion, etc) to match.
=head1 METHODS
You should never really use these methods on the class directly.
All numbers handled by C<Lingua::EN::Words2Nums> are handled by this module.
In addition ...
=cut
sub import {
my $class = shift;
my %opts = @_;
$opts{billion} = 10**12 unless defined $opts{billion};
no strict 'refs';
no warnings 'redefine';
my ($pkg, $file) = caller;
$Lingua::EN::Words2Nums::billion = $opts{billion};
foreach my $num ((keys %Lingua::EN::Words2Nums::nametosub,
'and', 'point', 'zero',
'pound', 'pounds', 'pence', 'p',
'dollars', 'cents'))
{
*{"$pkg\::$num"} = sub { $class->$num };
}
};
=head2 new <value> <operator>
C<operator> can be 'num', 'and' or 'point'
=cut
sub new {
my $class = shift;
$class = ref $class if ref $class;
my $val = shift;
my $op = shift;
my $name = shift || $op;
bless { value => $val, operator => $op, name => $name }, $class;
}
=head2 name
The name of this object (i.e the method that was originally called).
=cut
sub name {
( run in 4.669 seconds using v1.01-cache-2.11-cpan-39bf76dae61 )