root/misc/currency

Revision 2125 (checked in by miyagawa, 14 years ago)

allow 3000 JPY to mean --to USD

  • Property svn:executable set to *
Line 
1 #!/usr/bin/perl
2 use strict;
3 use warnings;
4 use Getopt::Long;
5 use Finance::Currency::Convert::Yahoo;
6 use Number::Format;
7 use Pod::Usage;
8
9 my $from;
10 my $to   = "JPY";
11 GetOptions('help|?' => \my $help, '--to=s', \$to, '--from=s', \$from);
12 pod2usage(1) if $help;
13
14 push @ARGV, $from if $from; # --from JPY 100 = 100 JPY
15
16 no warnings 'uninitialized';
17 my $stuff = join ' ', @ARGV;
18 my($quant, $currency) = split /\s+/, $stuff;
19 if ($quant =~ /^\w{3}$/ && $currency =~ /^[\d\,\.]*$/) {
20     ($quant, $currency) = ($currency, $quant);
21     $quant ||= 1;
22 }
23
24 if ($currency !~ /^\w{3}$/) {
25     pod2usage("Unknown currency: $currency\n");
26 }
27
28 $quant =~ tr/,//d;
29
30 $currency = uc($currency);
31 $to       = uc($to);
32
33 if ($currency eq $to && $currency eq 'JPY') {
34     $to = "USD";
35 }
36
37 my $how = Finance::Currency::Convert::Yahoo::convert($quant, $currency, $to) or exit;
38 print comma($quant), " $currency = ", comma($how), " $to\n";
39
40 sub comma {
41     Number::Format->new->format_number($_[0]);
42 }
43
44 __END__
45
46 =head1 NAME
47
48 currency - Convert currency from one to another
49
50 =head1 SYNOPSIS
51
52   currency 100 USD
53   currency -t GBP 1000 THB
54
55 =cut
Note: See TracBrowser for help on using the browser.