NAME
Handel::Currency - Price container to do currency conversion/formatting
SYNOPSIS
use Handel::Currency;
my $curr = Handel::Currenct-new(1.2);
print $curr->format(); # 1.20 USD
print $curr->format('CAD'); # 1.20 CAD
print $curr->format(undef, 'FMT_SYMBOL'); # $1.20
print 'Your price in Canadian Dollars is: ';
print $curr->convert('USD', 'CAD');
DESCRIPTION
The Handel::Currency
module provides basic currency formatting within Handel. It can be used separately to format any number into a more friendly format:
my $price = 1.23;
my $currency = Handel::Currency->new($price);
print $currency->format;
A new Handel::Currency object is automatically returned within the shopping cart when calling subtotal
, total
, and price
as an lvalue:
my $cart = Handel::Cart->load({id => '11111111-1111-1111-1111-111111111111'});
print $cart->subtotal; # 12.9
print $cart->subtotal->format(); # 12.90 USD
By default, a Handel::Currency object will stringify to the original decimal based price.
CONSTRUCTOR
new
The create a new Handel::Currency instance, simply call new
and pass in the price to be formatted:
my $currency = Handel::Currency->new(10.23);
METHODS
format([$code, $options])
The format
method returns the freshly formatted price in a currency and format declared in Locale::Currency::Format
. If no currency code or format are specified, the defaults values from Handel::ConfigReader
are used. Currently those defaults are USD
and FMT_STANDARD
.
It is also acceptable to specify different default values. See "CONFIGURATION" and Handel::ConfigReader
for further details.
In situations where Locale::Currency::Format
isn't installed, format
simply returns the price in it's original format no harm no foul.
format
throws a Handel::Exception::Argument
if code
isn't a valid currency code.
convert($from, $to [, $format, $options])
The convert
method converts the given price from one currency to another using Finance::Currency::Convert::WebserviceX
.
In situations where Finance::Currency::Convert::WebserviceX
isn't installed, convert
simply returns undef.
If no from
is specified, HandelCurrencyCode
below will be used instead.
convert
throws a Handel::Exception::Argument
if from
or to
aren't valid currency codes.
If $format
is true, the result of the conversion will also be formatted using the formatting options
given or the default in HandelCurrencyFormat
.
CONFIGURATION
HandelCurrencyCode
This sets the default currency code used when no code is passed into format
. See Locale::Currency::Format
for all available currency codes. The default code is USD.
HandelCurrencyFormat
This sets the default options used to format the price. See Locale::Currency::Format
for all available currency codes. The default format used is FMT_STANDARD
. Just like in Locale::Currency::Format
, you can combine options using |
.
SEE ALSO
Locale::Currency::Format, Finance::Currency::Convert::WebserviceX
AUTHOR
Christopher H. Laco
CPAN ID: CLACO
claco@chrislaco.com
http://today.icantfocus.com/blog/