#!/usr/bin/env perl use strict; use warnings; use Finance::Quote; my $q = Finance::Quote->new(); my %trans = (); @trans{qw(from to)} = @ARGV; # Copied this patter from Finance::Quote::currency to be consistent $trans{from} =~ s/^\s*(\d*\.?\d*)\s*//; my $amount = $1 || 1; # Search out currencies for my $key (qw( from to )) { # Is it a code or a name? my $currencies; if ( $trans{$key} =~ /^[A-Z]{2,}$/ ) { # Guess code $currencies = $q->currency_lookup( code => qr/$trans{$key}/ ); } else { # Guess name $currencies = $q->currency_lookup( name => qr/$trans{$key}/i ); } if ( scalar keys %{$currencies} == 1 ) { my $real_key = "real_${key}"; ($trans{$real_key}) = keys %{$currencies}; printf "%s : %s (%s)\n", $key , $trans{$real_key} , $currencies->{$trans{$real_key}}->{name}; } elsif ( scalar keys %{$currencies} > 1 ) { print "Multiple currency matches for ${key}:\n"; print " * $_ (" . $currencies->{$_}->{name} . ")\n" for keys %{$currencies}; } else { print "No currency matches for ${key}: " . $trans{$key} . "!\n"; } } # If real from and to exist run the conversion if ( exists $trans{real_from} && exists $trans{real_to} ) { print "${amount} $trans{real_from} => " . $q->currency("${amount}$trans{real_from}", $trans{real_to}) . " $trans{real_to}\n"; }