| 1 | package Finance::Currency::Convert::XE; | ||||||||||||||||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||||||||||||||||
| 3 | 7 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 4 | 7 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||||||||||||||||
| 6 | 7 | ▶ | use vars qw($VERSION); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 7 | 7 | $VERSION = '0.25'; | |||||||||||||||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||||||||||||||||
| 9 | #-------------------------------------------------------------------------- | ||||||||||||||||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||||||||||||||||
| 11 | =head1 NAME | ||||||||||||||||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||||||||||||||||
| 13 | Finance::Currency::Convert::XE - Currency conversion module. | ||||||||||||||||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||||||||||||||||
| 15 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||||||||||||||||
| 17 | use Finance::Currency::Convert::XE; | ||||||||||||||||||||||||||||||||||||||||||
| 18 | my $obj = Finance::Currency::Convert::XE->new() | ||||||||||||||||||||||||||||||||||||||||||
| 19 | || die "Failed to create object\n" ; | ||||||||||||||||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||||||||||||||||
| 21 | my $value = $obj->convert( | ||||||||||||||||||||||||||||||||||||||||||
| 22 | 'source' => 'GBP', | ||||||||||||||||||||||||||||||||||||||||||
| 23 | 'target' => 'EUR', | ||||||||||||||||||||||||||||||||||||||||||
| 24 | 'value' => '123.45', | ||||||||||||||||||||||||||||||||||||||||||
| 25 | 'format' => 'text' | ||||||||||||||||||||||||||||||||||||||||||
| 26 | ) || die "Could not convert: " . $obj->error . "\n"; | ||||||||||||||||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||||||||||||||||
| 28 | my @currencies = $obj->currencies; | ||||||||||||||||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||||||||||||||||
| 30 | or | ||||||||||||||||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||||||||||||||||
| 32 | use Finance::Currency::Convert::XE; | ||||||||||||||||||||||||||||||||||||||||||
| 33 | my $obj = Finance::Currency::Convert::XE->new( | ||||||||||||||||||||||||||||||||||||||||||
| 34 | 'source' => 'GBP', | ||||||||||||||||||||||||||||||||||||||||||
| 35 | 'target' => 'EUR', | ||||||||||||||||||||||||||||||||||||||||||
| 36 | 'format' => 'text' | ||||||||||||||||||||||||||||||||||||||||||
| 37 | ) || die "Failed to create object\n" ; | ||||||||||||||||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||||||||||||||||
| 39 | my $value = $obj->convert( | ||||||||||||||||||||||||||||||||||||||||||
| 40 | 'value' => '123.45', | ||||||||||||||||||||||||||||||||||||||||||
| 41 | 'format' => 'abbv' | ||||||||||||||||||||||||||||||||||||||||||
| 42 | ) || die "Could not convert: " . $obj->error . "\n"; | ||||||||||||||||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||||||||||||||||
| 44 | $value = $obj->convert('123.45') | ||||||||||||||||||||||||||||||||||||||||||
| 45 | || die "Could not convert: " . $obj->error . "\n"; | ||||||||||||||||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||||||||||||||||
| 47 | my @currencies = $obj->currencies; | ||||||||||||||||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||||||||||||||||
| 49 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||||||||||||||||
| 51 | Currency conversion module using XE.com's Universal Currency Converter (tm) | ||||||||||||||||||||||||||||||||||||||||||
| 52 | site. | ||||||||||||||||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||||||||||||||||
| 54 | WARNING: Do not use this module for any commercial purposes, unless you have | ||||||||||||||||||||||||||||||||||||||||||
| 55 | obtain an explicit license to use the service provided by XE.com. For further | ||||||||||||||||||||||||||||||||||||||||||
| 56 | details please read the Terms and Conditions available at L<http://www.xe.com>. | ||||||||||||||||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||||||||||||||||
| 58 | =over | ||||||||||||||||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||||||||||||||||
| 60 | =item * http://www.xe.com/errors/noautoextract.htm | ||||||||||||||||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||||||||||||||||
| 62 | =back | ||||||||||||||||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||||||||||||||||
| 64 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||||||||||||||||
| 66 | #-------------------------------------------------------------------------- | ||||||||||||||||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||||||||||||||||
| 68 | ########################################################################### | ||||||||||||||||||||||||||||||||||||||||||
| 69 | #Library Modules # | ||||||||||||||||||||||||||||||||||||||||||
| 70 | ########################################################################### | ||||||||||||||||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||||||||||||||||
| 72 | 7 | ▶ | use WWW::Mechanize; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 73 | 7 | ▶ | use HTML::TokeParser; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||||||||||||||||
| 75 | ########################################################################### | ||||||||||||||||||||||||||||||||||||||||||
| 76 | #Constants # | ||||||||||||||||||||||||||||||||||||||||||
| 77 | ########################################################################### | ||||||||||||||||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||||||||||||||||
| 79 | 7 | ▶ | use constant UCC => 'http://www.xe.com/currencyconverter'; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||||||||||||||||
| 81 | ########################################################################### | ||||||||||||||||||||||||||||||||||||||||||
| 82 | #Variables # | ||||||||||||||||||||||||||||||||||||||||||
| 83 | ########################################################################### | ||||||||||||||||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||||||||||||||||
| 85 | 7 | my %currencies; # only need to load once! | |||||||||||||||||||||||||||||||||||||||||
| 86 | 7 | my @defaults = ('source', 'target', 'format'); | |||||||||||||||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||||||||||||||||
| 88 | 7 | my $web = WWW::Mechanize->new(); | |||||||||||||||||||||||||||||||||||||||||
| 89 | 7 | $web->agent_alias( 'Mac Safari' ); | |||||||||||||||||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||||||||||||||||
| 91 | #-------------------------------------------------------------------------- | ||||||||||||||||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||||||||||||||||
| 93 | ########################################################################### | ||||||||||||||||||||||||||||||||||||||||||
| 94 | #Interface Functions # | ||||||||||||||||||||||||||||||||||||||||||
| 95 | ########################################################################### | ||||||||||||||||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||||||||||||||||
| 97 | =head1 METHODS | ||||||||||||||||||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||||||||||||||||
| 99 | =over 4 | ||||||||||||||||||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||||||||||||||||
| 101 | =item new | ||||||||||||||||||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||||||||||||||||
| 103 | Creates a new Finance::Currency::Convert::XE object. Can be supplied with | ||||||||||||||||||||||||||||||||||||||||||
| 104 | default values for source and target currency, and the format required of the | ||||||||||||||||||||||||||||||||||||||||||
| 105 | output. These can be overridden in the convert() method. | ||||||||||||||||||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||||||||||||||||
| 107 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||||||||||||||||
| 109 | 2 | ▶ | sub new { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
new: called CC=2 SCAR=7.1
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 110 | 2 | my ($this, @args) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 111 | 2 | ▶ | my $class = ref($this) || $this; | ||||||||||||||||||||||||||||||||||||||||
|
Condition33%1/3
ref $this || $this
MC/DC0%0/2
ref $this || $this
ref $this
$this
Truth table33%1/3
A || B
A= ref $thisB= $this
| |||||||||||||||||||||||||||||||||||||||||||
| 112 | 2 | my $self = {}; | |||||||||||||||||||||||||||||||||||||||||
| 113 | 2 | bless $self, $class; | |||||||||||||||||||||||||||||||||||||||||
| 114 | 2 | $self->_initialize(@args); | |||||||||||||||||||||||||||||||||||||||||
| 115 | 2 | return $self; | |||||||||||||||||||||||||||||||||||||||||
| 116 | } | ||||||||||||||||||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||||||||||||||||
| 118 | =item currencies | ||||||||||||||||||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||||||||||||||||
| 120 | Returns a plain array of the currencies available for conversion. | ||||||||||||||||||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||||||||||||||||
| 122 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||||||||||||||||
| 124 | 2 | ▶ | sub currencies { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
currencies: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 125 | 2 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||
| 126 | 2 | return sort keys %currencies; | |||||||||||||||||||||||||||||||||||||||||
| 127 | } | ||||||||||||||||||||||||||||||||||||||||||
| 128 | |||||||||||||||||||||||||||||||||||||||||||
| 129 | =item add_currencies | ||||||||||||||||||||||||||||||||||||||||||
| 130 | |||||||||||||||||||||||||||||||||||||||||||
| 131 | Allows the user to add currencies to the internal hash. Currencies can be added | ||||||||||||||||||||||||||||||||||||||||||
| 132 | as per the code below: | ||||||||||||||||||||||||||||||||||||||||||
| 133 | |||||||||||||||||||||||||||||||||||||||||||
| 134 | $self->add_currencies( | ||||||||||||||||||||||||||||||||||||||||||
| 135 | ZZZ => {text => 'An Example', symbol => '$'}, | ||||||||||||||||||||||||||||||||||||||||||
| 136 | ZZY => {text => 'Testing'} | ||||||||||||||||||||||||||||||||||||||||||
| 137 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 138 | |||||||||||||||||||||||||||||||||||||||||||
| 139 | Note that unless otherwise stated, the symbol will be set to '¤'. The code | ||||||||||||||||||||||||||||||||||||||||||
| 140 | used must be 3 characters in length, and a text part must be included. | ||||||||||||||||||||||||||||||||||||||||||
| 141 | |||||||||||||||||||||||||||||||||||||||||||
| 142 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 143 | |||||||||||||||||||||||||||||||||||||||||||
| 144 | 0 | ▶ | sub add_currencies { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
add_currencies: not called CC=6 SCAR=37.4
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 145 | 0 | my ($self,%hash) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 146 | 0 | for my $code (keys %hash) { | |||||||||||||||||||||||||||||||||||||||||
| 147 | 0 | ▶ | if($code =~ /[A-Z]{3}/i) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 148 | 0 | $code = uc $code; | |||||||||||||||||||||||||||||||||||||||||
| 149 | 0 | ▶ | if($hash{$code}->{text}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 150 | 0 | ▶ | $currencies{$code}->{name} = $hash{$code}->{text} || die; | ||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/2
$hash{$code}{'text'} || die()
MC/DC0%0/1
$hash{$code}{'text'} || die()
$hash{$code}{'text'}
Truth table0%0/2
A
A= $hash{$code}{'text'}
| |||||||||||||||||||||||||||||||||||||||||||
| 151 | 0 | ▶ | $currencies{$code}->{symbol} = $hash{$code}->{symbol} || '¤'; | ||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/2
$hash{$code}{'symbol'} || '¤'
MC/DC0%0/1
$hash{$code}{'symbol'} || '¤'
$hash{$code}{'symbol'}
Truth table0%0/2
A
A= $hash{$code}{'symbol'}
| |||||||||||||||||||||||||||||||||||||||||||
| 152 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 153 | 0 | $self->{error} = "User currency '$code' has no text part"; | |||||||||||||||||||||||||||||||||||||||||
| 154 | } | ||||||||||||||||||||||||||||||||||||||||||
| 155 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 156 | 0 | $self->{error} = "User currency '$code' is invalid"; | |||||||||||||||||||||||||||||||||||||||||
| 157 | } | ||||||||||||||||||||||||||||||||||||||||||
| 158 | } | ||||||||||||||||||||||||||||||||||||||||||
| 159 | } | ||||||||||||||||||||||||||||||||||||||||||
| 160 | |||||||||||||||||||||||||||||||||||||||||||
| 161 | =item convert | ||||||||||||||||||||||||||||||||||||||||||
| 162 | |||||||||||||||||||||||||||||||||||||||||||
| 163 | Converts some currency value into another using XE.com's UCC. | ||||||||||||||||||||||||||||||||||||||||||
| 164 | |||||||||||||||||||||||||||||||||||||||||||
| 165 | An anonymous hash is used to pass parameters. Legal hash keys and values | ||||||||||||||||||||||||||||||||||||||||||
| 166 | are as follows: | ||||||||||||||||||||||||||||||||||||||||||
| 167 | |||||||||||||||||||||||||||||||||||||||||||
| 168 | convert( | ||||||||||||||||||||||||||||||||||||||||||
| 169 | source => $currency_from, | ||||||||||||||||||||||||||||||||||||||||||
| 170 | target => $currency_to, | ||||||||||||||||||||||||||||||||||||||||||
| 171 | value => $currency_from_value, | ||||||||||||||||||||||||||||||||||||||||||
| 172 | format => $print_format | ||||||||||||||||||||||||||||||||||||||||||
| 173 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 174 | |||||||||||||||||||||||||||||||||||||||||||
| 175 | The format key is optional, and takes one of the following strings: | ||||||||||||||||||||||||||||||||||||||||||
| 176 | |||||||||||||||||||||||||||||||||||||||||||
| 177 | 'number' (returns '12.34') | ||||||||||||||||||||||||||||||||||||||||||
| 178 | 'symbol' (returns '£12.34') | ||||||||||||||||||||||||||||||||||||||||||
| 179 | 'text' (returns '12.34 Great Britain, Pound') | ||||||||||||||||||||||||||||||||||||||||||
| 180 | 'abbv' (returns '12.34 GBP') | ||||||||||||||||||||||||||||||||||||||||||
| 181 | |||||||||||||||||||||||||||||||||||||||||||
| 182 | If format key is omitted, 'number' is assumed and the converted value | ||||||||||||||||||||||||||||||||||||||||||
| 183 | is returned. | ||||||||||||||||||||||||||||||||||||||||||
| 184 | |||||||||||||||||||||||||||||||||||||||||||
| 185 | If only a value is passed, it is assumed that this is the value to be | ||||||||||||||||||||||||||||||||||||||||||
| 186 | converted and the remaining parameters will be defined by the defaults set | ||||||||||||||||||||||||||||||||||||||||||
| 187 | in the constructor. Note that no internal defaults are assumed. | ||||||||||||||||||||||||||||||||||||||||||
| 188 | |||||||||||||||||||||||||||||||||||||||||||
| 189 | Note that not all countries have symbols in the standard character set. | ||||||||||||||||||||||||||||||||||||||||||
| 190 | Where known the appropriate currency symbol is used, otherwise the | ||||||||||||||||||||||||||||||||||||||||||
| 191 | generic currency symbol is used. | ||||||||||||||||||||||||||||||||||||||||||
| 192 | |||||||||||||||||||||||||||||||||||||||||||
| 193 | It should also be noted that there is a recommendation to use only the | ||||||||||||||||||||||||||||||||||||||||||
| 194 | standardised three letter abbreviation ('abbv' above). However, for | ||||||||||||||||||||||||||||||||||||||||||
| 195 | further reading please see: | ||||||||||||||||||||||||||||||||||||||||||
| 196 | |||||||||||||||||||||||||||||||||||||||||||
| 197 | http://www.jhall.demon.co.uk/currency/ | ||||||||||||||||||||||||||||||||||||||||||
| 198 | http://www.jhall.demon.co.uk/currency/by_symbol.html | ||||||||||||||||||||||||||||||||||||||||||
| 199 | |||||||||||||||||||||||||||||||||||||||||||
| 200 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 201 | |||||||||||||||||||||||||||||||||||||||||||
| 202 | 0 | ▶ | sub convert { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
convert: not called CC=15 SCAR=54.8
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 203 | 0 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||
| 204 | 0 | ▶ | my %params = @_ > 1 ? @_ : (value => $_[0]); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 205 | 0 | ▶ | $params{$_} ||= $self->{$_} for(@defaults); | ||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/3
$params{$_} ||= $self->{$_}
MC/DC0%0/2
$params{$_} ||= $self->{$_}
$params{$_}
$self->{$_}
Truth table0%0/3
A ||= B
A= $params{$_}B= $self->{$_}
| |||||||||||||||||||||||||||||||||||||||||||
| 206 | |||||||||||||||||||||||||||||||||||||||||||
| 207 | 0 | undef $self->{error}; | |||||||||||||||||||||||||||||||||||||||||
| 208 | 0 | ▶ | unless( $params{source} ){ | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 209 | 0 | $self->{error} = 'Source currency is blank. This parameter is required'; | |||||||||||||||||||||||||||||||||||||||||
| 210 | 0 | return; | |||||||||||||||||||||||||||||||||||||||||
| 211 | } | ||||||||||||||||||||||||||||||||||||||||||
| 212 | |||||||||||||||||||||||||||||||||||||||||||
| 213 | 0 | ▶ | unless( exists($currencies{$params{source}}) ){ | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 214 | 0 | $self->{error} = 'Source currency "' . $params{source} . '" is not available'; | |||||||||||||||||||||||||||||||||||||||||
| 215 | 0 | return; | |||||||||||||||||||||||||||||||||||||||||
| 216 | } | ||||||||||||||||||||||||||||||||||||||||||
| 217 | |||||||||||||||||||||||||||||||||||||||||||
| 218 | 0 | ▶ | unless( $params{target} ){ | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 219 | 0 | $self->{error} = 'Target currency is blank. This parameter is required'; | |||||||||||||||||||||||||||||||||||||||||
| 220 | 0 | return; | |||||||||||||||||||||||||||||||||||||||||
| 221 | } | ||||||||||||||||||||||||||||||||||||||||||
| 222 | |||||||||||||||||||||||||||||||||||||||||||
| 223 | 0 | ▶ | unless( exists($currencies{$params{target}}) ){ | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 224 | 0 | $self->{error} = 'Target currency "' . $params{target} . '" is not available'; | |||||||||||||||||||||||||||||||||||||||||
| 225 | 0 | return; | |||||||||||||||||||||||||||||||||||||||||
| 226 | } | ||||||||||||||||||||||||||||||||||||||||||
| 227 | |||||||||||||||||||||||||||||||||||||||||||
| 228 | # store later use | ||||||||||||||||||||||||||||||||||||||||||
| 229 | 0 | $self->{code} = $params{target}; | |||||||||||||||||||||||||||||||||||||||||
| 230 | 0 | $self->{name} = $currencies{$params{target}}->{name}; | |||||||||||||||||||||||||||||||||||||||||
| 231 | 0 | $self->{symbol} = $currencies{$params{target}}->{symbol}; | |||||||||||||||||||||||||||||||||||||||||
| 232 | 0 | $self->{string} = $self->_format($params{format}); | |||||||||||||||||||||||||||||||||||||||||
| 233 | |||||||||||||||||||||||||||||||||||||||||||
| 234 | # This "feature" is actually useful as a pass-thru filter. | ||||||||||||||||||||||||||||||||||||||||||
| 235 | 0 | ▶ | if( $params{source} eq $params{target} ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 236 | return sprintf $self->{string}, $params{value} | ||||||||||||||||||||||||||||||||||||||||||
| 237 | 0 | } | |||||||||||||||||||||||||||||||||||||||||
| 238 | |||||||||||||||||||||||||||||||||||||||||||
| 239 | # get the base site | ||||||||||||||||||||||||||||||||||||||||||
| 240 | 0 | $web->get( UCC ); | |||||||||||||||||||||||||||||||||||||||||
| 241 | |||||||||||||||||||||||||||||||||||||||||||
| 242 | 0 | ▶ | unless($web->success()) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 243 | 0 | $self->{error} = 'Unable to retrieve webpage'; | |||||||||||||||||||||||||||||||||||||||||
| 244 | 0 | return; | |||||||||||||||||||||||||||||||||||||||||
| 245 | } | ||||||||||||||||||||||||||||||||||||||||||
| 246 | |||||||||||||||||||||||||||||||||||||||||||
| 247 | 0 | my @forms = $web->forms(); | |||||||||||||||||||||||||||||||||||||||||
| 248 | 0 | my $form_number = 1; | |||||||||||||||||||||||||||||||||||||||||
| 249 | 0 | my $found = 0; | |||||||||||||||||||||||||||||||||||||||||
| 250 | |||||||||||||||||||||||||||||||||||||||||||
| 251 | 0 | foreach my $form (@forms) { | |||||||||||||||||||||||||||||||||||||||||
| 252 | 0 | ▶ | if ($form->action eq 'http://www.xe.com/currencyconverter/convert/') { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 253 | 0 | $found = 1; | |||||||||||||||||||||||||||||||||||||||||
| 254 | 0 | last; | |||||||||||||||||||||||||||||||||||||||||
| 255 | } | ||||||||||||||||||||||||||||||||||||||||||
| 256 | |||||||||||||||||||||||||||||||||||||||||||
| 257 | 0 | $form_number++; | |||||||||||||||||||||||||||||||||||||||||
| 258 | } | ||||||||||||||||||||||||||||||||||||||||||
| 259 | |||||||||||||||||||||||||||||||||||||||||||
| 260 | 0 | ▶ | if ($found) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 261 | # complete and submit the form | ||||||||||||||||||||||||||||||||||||||||||
| 262 | $web->submit_form( | ||||||||||||||||||||||||||||||||||||||||||
| 263 | form_number => $form_number, | ||||||||||||||||||||||||||||||||||||||||||
| 264 | fields => { 'From' => $params{source}, | ||||||||||||||||||||||||||||||||||||||||||
| 265 | 'To' => $params{target}, | ||||||||||||||||||||||||||||||||||||||||||
| 266 | 'Amount' => $params{value} | ||||||||||||||||||||||||||||||||||||||||||
| 267 | } | ||||||||||||||||||||||||||||||||||||||||||
| 268 | 0 | ); | |||||||||||||||||||||||||||||||||||||||||
| 269 | } | ||||||||||||||||||||||||||||||||||||||||||
| 270 | |||||||||||||||||||||||||||||||||||||||||||
| 271 | 0 | ▶ | unless($found && $web->success()) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$found and $web->success
MC/DC0%0/2
$found and $web->success
$found
$web->success
Truth table0%0/3
A and B
A= $foundB= $web->success
| |||||||||||||||||||||||||||||||||||||||||||
| 272 | 0 | $self->{error} = 'Unable to retrieve webform'; | |||||||||||||||||||||||||||||||||||||||||
| 273 | 0 | return; | |||||||||||||||||||||||||||||||||||||||||
| 274 | } | ||||||||||||||||||||||||||||||||||||||||||
| 275 | |||||||||||||||||||||||||||||||||||||||||||
| 276 | # return the converted value | ||||||||||||||||||||||||||||||||||||||||||
| 277 | 0 | return $self->_extract_text($web->content()); | |||||||||||||||||||||||||||||||||||||||||
| 278 | } | ||||||||||||||||||||||||||||||||||||||||||
| 279 | |||||||||||||||||||||||||||||||||||||||||||
| 280 | =item error | ||||||||||||||||||||||||||||||||||||||||||
| 281 | |||||||||||||||||||||||||||||||||||||||||||
| 282 | Returns a (hopefully) meaningful error string. | ||||||||||||||||||||||||||||||||||||||||||
| 283 | |||||||||||||||||||||||||||||||||||||||||||
| 284 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 285 | |||||||||||||||||||||||||||||||||||||||||||
| 286 | 0 | ▶ | sub error { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
error: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 287 | 0 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||
| 288 | 0 | return $self->{error}; | |||||||||||||||||||||||||||||||||||||||||
| 289 | } | ||||||||||||||||||||||||||||||||||||||||||
| 290 | |||||||||||||||||||||||||||||||||||||||||||
| 291 | ########################################################################### | ||||||||||||||||||||||||||||||||||||||||||
| 292 | #Internal Functions # | ||||||||||||||||||||||||||||||||||||||||||
| 293 | ########################################################################### | ||||||||||||||||||||||||||||||||||||||||||
| 294 | |||||||||||||||||||||||||||||||||||||||||||
| 295 | 2 | ▶ | sub _initialize { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_initialize: called CC=4 SCAR=13.9
| |||||||||||||||||||||||||||||||||||||||||||
| 296 | 2 | my($self, %params) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 297 | # set defaults | ||||||||||||||||||||||||||||||||||||||||||
| 298 | 2 | $self->{$_} = $params{$_} for(@defaults); | |||||||||||||||||||||||||||||||||||||||||
| 299 | |||||||||||||||||||||||||||||||||||||||||||
| 300 | 2 | ▶ | return if(keys %currencies); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 301 | 1 | local($_); | |||||||||||||||||||||||||||||||||||||||||
| 302 | |||||||||||||||||||||||||||||||||||||||||||
| 303 | # Extract the mapping of currencies and their atrributes | ||||||||||||||||||||||||||||||||||||||||||
| 304 | 1 | ▶ | while(<Finance::Currency::Convert::XE::DATA>){ | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 305 | 169 | s/\s*$//; | |||||||||||||||||||||||||||||||||||||||||
| 306 | 169 | my ($code,$text,$symbol) = split /\|/; | |||||||||||||||||||||||||||||||||||||||||
| 307 | 169 | $currencies{$code}->{name} = $text; | |||||||||||||||||||||||||||||||||||||||||
| 308 | 169 | $currencies{$code}->{symbol} = $symbol; | |||||||||||||||||||||||||||||||||||||||||
| 309 | } | ||||||||||||||||||||||||||||||||||||||||||
| 310 | |||||||||||||||||||||||||||||||||||||||||||
| 311 | 1 | return; | |||||||||||||||||||||||||||||||||||||||||
| 312 | } | ||||||||||||||||||||||||||||||||||||||||||
| 313 | |||||||||||||||||||||||||||||||||||||||||||
| 314 | # Formats the return string to the requirements of the caller | ||||||||||||||||||||||||||||||||||||||||||
| 315 | 0 | ▶ | sub _format { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_format: not called CC=3 SCAR=24.8
| |||||||||||||||||||||||||||||||||||||||||||
| 316 | 0 | my($self, $form) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 317 | |||||||||||||||||||||||||||||||||||||||||||
| 318 | my %formats = ( | ||||||||||||||||||||||||||||||||||||||||||
| 319 | 'symbol' => $self->{symbol} . '%.02f', | ||||||||||||||||||||||||||||||||||||||||||
| 320 | 'abbv' => '%.02f ' . $self->{code}, | ||||||||||||||||||||||||||||||||||||||||||
| 321 | 'text' => '%.02f ' . $self->{name}, | ||||||||||||||||||||||||||||||||||||||||||
| 322 | 0 | 'number' => '%.02f', | |||||||||||||||||||||||||||||||||||||||||
| 323 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 324 | |||||||||||||||||||||||||||||||||||||||||||
| 325 | 0 | ▶ | return $formats{$form} if(defined $form && $formats{$form}); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
defined $form and $formats{$form}
MC/DC0%0/2
defined $form and $formats{$form}
defined $form
$formats{$form}
Truth table0%0/3
A and B
A= defined $formB= $formats{$form}
| |||||||||||||||||||||||||||||||||||||||||||
| 326 | 0 | return '%.02f'; | |||||||||||||||||||||||||||||||||||||||||
| 327 | } | ||||||||||||||||||||||||||||||||||||||||||
| 328 | |||||||||||||||||||||||||||||||||||||||||||
| 329 | # Extract the text from the html we get back from UCC and return | ||||||||||||||||||||||||||||||||||||||||||
| 330 | # it (keying on the fact that what we want is in the table after | ||||||||||||||||||||||||||||||||||||||||||
| 331 | # the faq link). | ||||||||||||||||||||||||||||||||||||||||||
| 332 | 0 | ▶ | sub _extract_text { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_extract_text: not called CC=8 SCAR=42.8
| |||||||||||||||||||||||||||||||||||||||||||
| 333 | 0 | my($self, $html) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 334 | 0 | my $tag; | |||||||||||||||||||||||||||||||||||||||||
| 335 | 0 | my $p = HTML::TokeParser->new(\$html); | |||||||||||||||||||||||||||||||||||||||||
| 336 | |||||||||||||||||||||||||||||||||||||||||||
| 337 | # first look for the 'td' element | ||||||||||||||||||||||||||||||||||||||||||
| 338 | 0 | while (1) { | |||||||||||||||||||||||||||||||||||||||||
| 339 | 0 | ▶ | return unless ($tag = $p->get_tag('td')); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 340 | 0 | ▶ | next unless (defined($tag->[1]{'align'}) && ($tag->[1]{'align'} eq 'left')); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
defined $tag->[1]{"align"} and $tag->[1]{"align"} eq "left"
MC/DC0%0/2
defined $tag->[1]{"align"} and $tag->[1]{"align"} eq "left"
defined $tag->[1]{"align"}
$tag->[1]{"align"} eq "left"
Truth table0%0/3
A and B
A= defined $tag->[1]{"align"}B= $tag->[1]{"align"} eq "left"
| |||||||||||||||||||||||||||||||||||||||||||
| 341 | # this will probably be the value | ||||||||||||||||||||||||||||||||||||||||||
| 342 | 0 | my $value = $p->get_trimmed_text; | |||||||||||||||||||||||||||||||||||||||||
| 343 | |||||||||||||||||||||||||||||||||||||||||||
| 344 | # then make sure this has the 'span' with the target | ||||||||||||||||||||||||||||||||||||||||||
| 345 | # currency code | ||||||||||||||||||||||||||||||||||||||||||
| 346 | 0 | my $tag2 = $p->get_tag('span'); | |||||||||||||||||||||||||||||||||||||||||
| 347 | 0 | my $cd = $p->get_trimmed_text; | |||||||||||||||||||||||||||||||||||||||||
| 348 | 0 | ▶ | if (defined($tag2) && defined($tag2->[1]{'class'} && $tag2->[1]{class} eq 'uccResCde' | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/6
defined $tag2 and defined($tag2->[1]{"class"} && $tag2->[1]{"class"} eq "uccResCde")
$tag2->[1]{'class'} && $tag2->[1]{'class'} eq 'uccResCde'
MC/DC0%0/3
defined $tag2 and defined($tag2->[1]{"class"} && $tag2->[1]{"class"} eq "uccResCde")
defined $tag2
$tag2->[1]{'class'}
$tag2->[1]{'class'} eq 'uccResCde'
Truth table0%0/4
A and B && C
A= defined $tag2B= $tag2->[1]{'class'}C= $tag2->[1]{'class'} eq 'uccResCde'
| |||||||||||||||||||||||||||||||||||||||||||
| 349 | )) { | ||||||||||||||||||||||||||||||||||||||||||
| 350 | 0 | ▶ | if ($cd eq $self->{code}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 351 | # found it, return | ||||||||||||||||||||||||||||||||||||||||||
| 352 | 0 | $value =~ s/,//g; | |||||||||||||||||||||||||||||||||||||||||
| 353 | 0 | return sprintf $self->{string}, $value; | |||||||||||||||||||||||||||||||||||||||||
| 354 | } | ||||||||||||||||||||||||||||||||||||||||||
| 355 | } | ||||||||||||||||||||||||||||||||||||||||||
| 356 | } | ||||||||||||||||||||||||||||||||||||||||||
| 357 | |||||||||||||||||||||||||||||||||||||||||||
| 358 | # didn't find anything | ||||||||||||||||||||||||||||||||||||||||||
| 359 | 0 | return; | |||||||||||||||||||||||||||||||||||||||||
| 360 | } | ||||||||||||||||||||||||||||||||||||||||||
| 361 | |||||||||||||||||||||||||||||||||||||||||||
| 362 | 7 | 1; | |||||||||||||||||||||||||||||||||||||||||
| 363 | |||||||||||||||||||||||||||||||||||||||||||
| 364 | #-------------------------------------------------------------------------- | ||||||||||||||||||||||||||||||||||||||||||
| 365 | |||||||||||||||||||||||||||||||||||||||||||
| 366 | =back | ||||||||||||||||||||||||||||||||||||||||||
| 367 | |||||||||||||||||||||||||||||||||||||||||||
| 368 | =head1 TERMS OF USE | ||||||||||||||||||||||||||||||||||||||||||
| 369 | |||||||||||||||||||||||||||||||||||||||||||
| 370 | XE.com have a Terms of Use policy that states: | ||||||||||||||||||||||||||||||||||||||||||
| 371 | |||||||||||||||||||||||||||||||||||||||||||
| 372 | This website is for informational purposes only and is not intended to | ||||||||||||||||||||||||||||||||||||||||||
| 373 | provide specific commercial, financial, investment, accounting, tax, or | ||||||||||||||||||||||||||||||||||||||||||
| 374 | legal advice. It is provided to you solely for your own personal, | ||||||||||||||||||||||||||||||||||||||||||
| 375 | non-commercial use and not for purposes of resale, distribution, public | ||||||||||||||||||||||||||||||||||||||||||
| 376 | display or performance, or any other uses by you in any form or manner | ||||||||||||||||||||||||||||||||||||||||||
| 377 | whatsoever. Unless otherwise indicated on this website, you may display, | ||||||||||||||||||||||||||||||||||||||||||
| 378 | download, archive, and print a single copy of any information on this | ||||||||||||||||||||||||||||||||||||||||||
| 379 | website, or otherwise distributed from XE.com, for such personal, | ||||||||||||||||||||||||||||||||||||||||||
| 380 | non-commercial use, provided it is done pursuant to the User Conduct and | ||||||||||||||||||||||||||||||||||||||||||
| 381 | Obligations set forth herein. | ||||||||||||||||||||||||||||||||||||||||||
| 382 | |||||||||||||||||||||||||||||||||||||||||||
| 383 | As such this software is for personal use ONLY. No liability is accepted by | ||||||||||||||||||||||||||||||||||||||||||
| 384 | the author for abuse or miuse of the software herein. Use of this software | ||||||||||||||||||||||||||||||||||||||||||
| 385 | is only permitted under the terms stipulated by XE.com. | ||||||||||||||||||||||||||||||||||||||||||
| 386 | |||||||||||||||||||||||||||||||||||||||||||
| 387 | The full legal document is available at L<http://www.xe.com/legal/> | ||||||||||||||||||||||||||||||||||||||||||
| 388 | |||||||||||||||||||||||||||||||||||||||||||
| 389 | =head1 TODO | ||||||||||||||||||||||||||||||||||||||||||
| 390 | |||||||||||||||||||||||||||||||||||||||||||
| 391 | Currency symbols are currently specified with a generic symbol, if the | ||||||||||||||||||||||||||||||||||||||||||
| 392 | currency symbol is unknown. Are there any other symbols available in | ||||||||||||||||||||||||||||||||||||||||||
| 393 | Unicode? Let me know if there are. | ||||||||||||||||||||||||||||||||||||||||||
| 394 | |||||||||||||||||||||||||||||||||||||||||||
| 395 | =head1 SEE ALSO | ||||||||||||||||||||||||||||||||||||||||||
| 396 | |||||||||||||||||||||||||||||||||||||||||||
| 397 | L<HTML::TokeParser>, | ||||||||||||||||||||||||||||||||||||||||||
| 398 | L<WWW::Mechanize> | ||||||||||||||||||||||||||||||||||||||||||
| 399 | |||||||||||||||||||||||||||||||||||||||||||
| 400 | =head1 SUPPORT | ||||||||||||||||||||||||||||||||||||||||||
| 401 | |||||||||||||||||||||||||||||||||||||||||||
| 402 | There are no known bugs at the time of this release. However, if you spot a | ||||||||||||||||||||||||||||||||||||||||||
| 403 | bug or are experiencing difficulties that are not explained within the POD | ||||||||||||||||||||||||||||||||||||||||||
| 404 | documentation, please submit a bug to the RT system (see link below). However, | ||||||||||||||||||||||||||||||||||||||||||
| 405 | it would help greatly if you are able to pinpoint problems or even supply a | ||||||||||||||||||||||||||||||||||||||||||
| 406 | patch. | ||||||||||||||||||||||||||||||||||||||||||
| 407 | |||||||||||||||||||||||||||||||||||||||||||
| 408 | Fixes are dependant upon their severity and my availablity. Should a fix not | ||||||||||||||||||||||||||||||||||||||||||
| 409 | be forthcoming, please feel free to (politely) remind me by sending an email | ||||||||||||||||||||||||||||||||||||||||||
| 410 | to barbie@cpan.org . | ||||||||||||||||||||||||||||||||||||||||||
| 411 | |||||||||||||||||||||||||||||||||||||||||||
| 412 | RT: L<http://rt.cpan.org/Public/Dist/Display.html?Name=Finance-Currency-Convert-XE> | ||||||||||||||||||||||||||||||||||||||||||
| 413 | |||||||||||||||||||||||||||||||||||||||||||
| 414 | =head1 AUTHOR | ||||||||||||||||||||||||||||||||||||||||||
| 415 | |||||||||||||||||||||||||||||||||||||||||||
| 416 | Barbie, <barbie@cpan.org> | ||||||||||||||||||||||||||||||||||||||||||
| 417 | for Miss Barbell Productions <http://www.missbarbell.co.uk>. | ||||||||||||||||||||||||||||||||||||||||||
| 418 | |||||||||||||||||||||||||||||||||||||||||||
| 419 | =head1 COPYRIGHT | ||||||||||||||||||||||||||||||||||||||||||
| 420 | |||||||||||||||||||||||||||||||||||||||||||
| 421 | Copyright © 2002-2011 Barbie for Miss Barbell Productions. | ||||||||||||||||||||||||||||||||||||||||||
| 422 | |||||||||||||||||||||||||||||||||||||||||||
| 423 | This module is free software; you can redistribute it and/or | ||||||||||||||||||||||||||||||||||||||||||
| 424 | modify it under the Artistic Licence v2. | ||||||||||||||||||||||||||||||||||||||||||
| 425 | |||||||||||||||||||||||||||||||||||||||||||
| 426 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 427 | |||||||||||||||||||||||||||||||||||||||||||
| 428 | #-------------------------------------------------------------------------- | ||||||||||||||||||||||||||||||||||||||||||
| 429 | |||||||||||||||||||||||||||||||||||||||||||
| 430 | __DATA__ | ||||||||||||||||||||||||||||||||||||||||||