#!/usr/bin/perl use URI::URL; use LWP::UserAgent; use Getopt::Long; use Pod::Usage; %Currencies=( 'USD' => 'United States Dollars', 'EUR' => 'Euro', 'CAD' => 'Canada Dollars', 'GBP' => 'United Kingdom Pounds', 'DEM' => 'Germany Deutsche Marks', 'FRF' => 'France Francs', 'JPY' => 'Japan Yen', 'NLG' => 'Netherlands Guilders', 'ITL' => 'Italy Lira', 'CHF' => 'Switzerland Francs', 'DZD' => 'Algeria Dinars', 'ARP' => 'Argentina Pesos', 'AUD' => 'Australia Dollars', 'ATS' => 'Austria Schillings', 'BSD' => 'Bahamas Dollars', 'BBD' => 'Barbados Dollars', 'BEF' => 'Belgium Francs', 'BMD' => 'Bermuda Dollars', 'BRR' => 'Brazil Real', 'BGL' => 'Bulgaria Lev', 'CAD' => 'Canada Dollars', 'CLP' => 'Chile Pesos', 'CNY' => 'China Yuan Renmimbi', 'CYP' => 'Cyprus Pounds', 'CSK' => 'Czech Republic Koruna', 'DKK' => 'Denmark Kroner', 'NLG' => 'Dutch Guilders', 'XCD' => 'Eastern Caribbean Dollars', 'EGP' => 'Egypt Pounds', 'EUR' => 'Euro', 'FJD' => 'Fiji Dollars', 'FIM' => 'Finland Markka', 'FRF' => 'France Francs', 'DEM' => 'Germany Deutsche Marks', 'XAU' => 'Gold Ounces', 'GRD' => 'Greece Drachmas', 'HKD' => 'Hong Kong Dollars', 'HUF' => 'Hungary Forint', 'ISK' => 'Iceland Krona', 'INR' => 'India Rupees', 'IDR' => 'Indonesia Rupiah', 'IEP' => 'Ireland Punt', 'ILS' => 'Israel New Shekels', 'ITL' => 'Italy Lira', 'JMD' => 'Jamaica Dollars', 'JPY' => 'Japan Yen', 'JOD' => 'Jordan Dinar', 'KRW' => 'Korea (South) Won', 'LBP' => 'Lebanon Pounds', 'LUF' => 'Luxembourg Francs', 'MYR' => 'Malaysia Ringgit', 'MXP' => 'Mexico Pesos', 'NLG' => 'Netherlands Guilders', 'NZD' => 'New Zealand Dollars', 'NOK' => 'Norway Kroner', 'PKR' => 'Pakistan Rupees', 'XPD' => 'Palladium Ounces', 'PHP' => 'Philippines Pesos', 'XPT' => 'Platinum Ounces', 'PLZ' => 'Poland Zloty', 'PTE' => 'Portugal Escudo', 'ROL' => 'Romania Leu', 'RUR' => 'Russia Rubles', 'SAR' => 'Saudi Arabia Riyal', 'XAG' => 'Silver Ounces', 'SGD' => 'Singapore Dollars', 'SKK' => 'Slovakia Koruna', 'ZAR' => 'South Africa Rand', 'KRW' => 'South Korea Won', 'ESP' => 'Spain Pesetas', 'XDR' => 'Special Drawing Right (IMF)', 'SDD' => 'Sudan Dinar', 'SEK' => 'Sweden Krona', 'CHF' => 'Switzerland Francs', 'TWD' => 'Taiwan Dollars', 'THB' => 'Thailand Baht', 'TTD' => 'Trinidad and Tobago Dollars', 'TRL' => 'Turkey Lira', 'GBP' => 'United Kingdom Pounds', 'USD' => 'United States Dollars', 'VEB' => 'Venezuela Bolivar', 'ZMK' => 'Zambia Kwacha', 'EUR' => 'Euro', 'XCD' => 'Eastern Caribbean Dollars', 'XDR' => 'Special Drawing Right (IMF)', 'XAG' => 'Silver Ounces', 'XAU' => 'Gold Ounces', 'XPD' => 'Palladium Ounces', 'XPT' => 'Platinum Ounces', ); Getopt::Long::Configure('auto_abbrev','ignore_case_always'); $from = 'USD'; $to = 'FRF'; GetOptions( 'from=s' => \$from, 'to=s' => \$to, 'h|?|help+' => \$help, 'list!' => \$list, ) or pod2usage("Wrong Options"); $from = uc $from; $to = uc $to; pod2usage(1) if ($help); if ($list) { &print_list(); exit 0; } $value = shift @ARGV; $value = 1 unless $value; print "$value $from = $result $to\n" if ($result=&make_exchange($from,$to,$value)); exit; sub print_list{ foreach (keys(%Currencies)) { print "$_ $Currencies{$_}\n"; } } sub make_exchange { my($from,$to,$value)=@_; $ua = new LWP::UserAgent; local $req = new HTTP::Request 'POST', 'http://www.xe.net/ucc/convert.cgi'; $req->content_type('application/x-www-form-urlencoded'); local $curl = url("http:"); %form = ( Amount => $value, From => $from, To => $to); $curl->query_form(%form); $req->content($curl->equery); $_=$ua->request($req)->as_string; if (/((\d*,)*\d*(\.\d*)?)\s*$to/i) { return $1; } return undef; } __END__ =head1 NAME exchangerate - get exchange from a currency to an other. =head1 SYNOPSIS exchangerate [--from=CUR1] [--to=CUR2] [B<-v>|--verbose] [B<-h>|-?|--help] value =head1 DESCRIPTION Exchangerate is a Perl script to get a currency exchange rate from the web and write it to standard output. You must specify the currencies in the standard fromat, for example, Australian Dollars -> AUD, French Francs -> FRF, and the Euro -> EUR. You may specifie a value to transfrom, in this case you will not get the rate but the new value. =head1 OPTIONS =over =item B<--from=CUR1> Tranform from CUR1. Default is USD. =item B<--to=CUR2> Tranform to CUR2. Default is FRF. =item B<--help> B<-h> B<-?> print help =back =head1 AUTHOR This program was written by El Bachir Hamdouni (hamdouni@free.fr) and may be freely distributed under the terms of the GNU General Public License. There is ABSOLUTELY NO WARRANTY for this program. =cut