| line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
|
1
|
|
|
|
|
|
|
package Finance::Currency::Convert::BChile; |
|
2
|
|
|
|
|
|
|
|
|
3
|
7
|
|
|
7
|
|
220111
|
use warnings; |
|
|
7
|
|
|
|
|
21
|
|
|
|
7
|
|
|
|
|
279
|
|
|
4
|
7
|
|
|
7
|
|
190
|
use strict; |
|
|
7
|
|
|
|
|
13
|
|
|
|
7
|
|
|
|
|
234
|
|
|
5
|
|
|
|
|
|
|
|
|
6
|
7
|
|
|
7
|
|
39
|
use Carp; |
|
|
7
|
|
|
|
|
13
|
|
|
|
7
|
|
|
|
|
6079
|
|
|
7
|
|
|
|
|
|
|
|
|
8
|
|
|
|
|
|
|
=head1 NAME |
|
9
|
|
|
|
|
|
|
|
|
10
|
|
|
|
|
|
|
Finance::Currency::Convert::BChile - Currency conversion module |
|
11
|
|
|
|
|
|
|
between Chilean Pesos (CLP) and USA Dollars (USD). |
|
12
|
|
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
=head1 VERSION |
|
14
|
|
|
|
|
|
|
|
|
15
|
|
|
|
|
|
|
Version 0.05 |
|
16
|
|
|
|
|
|
|
|
|
17
|
|
|
|
|
|
|
=cut |
|
18
|
|
|
|
|
|
|
|
|
19
|
|
|
|
|
|
|
$Finance::Currency::Convert::BChile::VERSION = '0.05'; |
|
20
|
|
|
|
|
|
|
|
|
21
|
7
|
|
|
7
|
|
16170
|
use LWP::UserAgent; |
|
|
7
|
|
|
|
|
462096
|
|
|
|
7
|
|
|
|
|
255
|
|
|
22
|
7
|
|
|
7
|
|
7015
|
use HTML::TokeParser; |
|
|
7
|
|
|
|
|
110125
|
|
|
|
7
|
|
|
|
|
7367
|
|
|
23
|
|
|
|
|
|
|
|
|
24
|
|
|
|
|
|
|
# As of 2013-07-24, bcentral.cl uses an iframe in homepage and |
|
25
|
|
|
|
|
|
|
# feeds values from another URL |
|
26
|
|
|
|
|
|
|
my $BCENTRAL_URL = 'http://si3.bcentral.cl/indicadoresvalores/secure/indicadoresvalores.aspx'; |
|
27
|
|
|
|
|
|
|
my $DEFAULT_UA = 'Finance::Currency::Convert::BChile perl module'; |
|
28
|
|
|
|
|
|
|
|
|
29
|
|
|
|
|
|
|
=head1 SYNOPSIS |
|
30
|
|
|
|
|
|
|
|
|
31
|
|
|
|
|
|
|
Currency conversion module between Chilean Pesos (CLP) and USA |
|
32
|
|
|
|
|
|
|
Dollars (USD). The conversion rate is obtained from the official |
|
33
|
|
|
|
|
|
|
source in Chile: the central bank "Banco Central de Chile", from |
|
34
|
|
|
|
|
|
|
their webpage http://www.bcentral.cl |
|
35
|
|
|
|
|
|
|
|
|
36
|
|
|
|
|
|
|
use Finance::Currency::Convert::BChile; |
|
37
|
|
|
|
|
|
|
|
|
38
|
|
|
|
|
|
|
my $conversor = Finance::Currency::Convert::BChile->new(); |
|
39
|
|
|
|
|
|
|
$conversor->update; |
|
40
|
|
|
|
|
|
|
print $conversor->CLP2USD(20170); |
|
41
|
|
|
|
|
|
|
|
|
42
|
|
|
|
|
|
|
=head1 FUNCTIONS |
|
43
|
|
|
|
|
|
|
|
|
44
|
|
|
|
|
|
|
=head2 new |
|
45
|
|
|
|
|
|
|
|
|
46
|
|
|
|
|
|
|
Creates a new Finance::Currency::Convert::BChile object. |
|
47
|
|
|
|
|
|
|
Initializes the web robot. You can pass a user agent string |
|
48
|
|
|
|
|
|
|
as a parameter. |
|
49
|
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
|
=cut |
|
51
|
|
|
|
|
|
|
|
|
52
|
|
|
|
|
|
|
sub new { |
|
53
|
6
|
|
|
6
|
1
|
439
|
my ($this, @args) = @_; |
|
54
|
|
|
|
|
|
|
|
|
55
|
6
|
|
33
|
|
|
59
|
my $class = ref($this) || $this; |
|
56
|
|
|
|
|
|
|
|
|
57
|
6
|
|
33
|
|
|
39
|
my $ua_string = $args[0] || $DEFAULT_UA; |
|
58
|
|
|
|
|
|
|
|
|
59
|
6
|
|
|
|
|
11
|
my $self = {}; |
|
60
|
6
|
|
|
|
|
23
|
bless $self, $class; |
|
61
|
|
|
|
|
|
|
|
|
62
|
6
|
50
|
|
|
|
53
|
$self->{'ua'} = LWP::UserAgent->new |
|
63
|
|
|
|
|
|
|
or croak "Unable to create user agent"; |
|
64
|
6
|
|
|
|
|
20622
|
$self->{'ua'}->agent($ua_string); |
|
65
|
|
|
|
|
|
|
|
|
66
|
6
|
|
|
|
|
418
|
$self->{'dolar'} = ''; |
|
67
|
6
|
|
|
|
|
90
|
$self->{'fecha'} = ''; |
|
68
|
|
|
|
|
|
|
|
|
69
|
6
|
|
|
|
|
27
|
return $self; |
|
70
|
|
|
|
|
|
|
} |
|
71
|
|
|
|
|
|
|
|
|
72
|
|
|
|
|
|
|
=head2 update |
|
73
|
|
|
|
|
|
|
|
|
74
|
|
|
|
|
|
|
Obtains the data from bcentral page. |
|
75
|
|
|
|
|
|
|
You can pass a fake value as a parameter. In this case there's no |
|
76
|
|
|
|
|
|
|
update from bcentral site. |
|
77
|
|
|
|
|
|
|
|
|
78
|
|
|
|
|
|
|
=cut |
|
79
|
|
|
|
|
|
|
|
|
80
|
|
|
|
|
|
|
sub update { |
|
81
|
5
|
|
|
5
|
1
|
27
|
my $self = shift; |
|
82
|
5
|
|
|
|
|
7
|
my $simulate = shift; |
|
83
|
|
|
|
|
|
|
|
|
84
|
5
|
50
|
|
|
|
20
|
if ($simulate) { |
|
85
|
5
|
|
|
|
|
12
|
$self->{'dolar'} = $simulate; |
|
86
|
5
|
|
|
|
|
12
|
$self->{'fecha'} = 'UNKNOWN'; |
|
87
|
|
|
|
|
|
|
|
|
88
|
5
|
|
|
|
|
15
|
return 1; |
|
89
|
|
|
|
|
|
|
} |
|
90
|
|
|
|
|
|
|
|
|
91
|
0
|
|
|
|
|
0
|
my $response = $self->{'ua'}->get($BCENTRAL_URL); |
|
92
|
|
|
|
|
|
|
|
|
93
|
0
|
0
|
|
|
|
0
|
unless ($response->is_success) { |
|
94
|
0
|
|
|
|
|
0
|
carp "Unable to get page: " . $response->status_line; |
|
95
|
|
|
|
|
|
|
|
|
96
|
0
|
|
|
|
|
0
|
$self->{'dolar'} = ''; |
|
97
|
0
|
|
|
|
|
0
|
$self->{'fecha'} = ''; |
|
98
|
0
|
|
|
|
|
0
|
return 0; |
|
99
|
|
|
|
|
|
|
} |
|
100
|
|
|
|
|
|
|
|
|
101
|
0
|
0
|
|
|
|
0
|
my $p = HTML::TokeParser->new(\$response->content) |
|
102
|
|
|
|
|
|
|
or croak "Unable to parse response: $!"; |
|
103
|
|
|
|
|
|
|
|
|
104
|
0
|
|
|
|
|
0
|
my ($dolar, $fecha); |
|
105
|
|
|
|
|
|
|
|
|
106
|
0
|
|
|
|
|
0
|
while (my $token = $p->get_tag("div")) { |
|
107
|
0
|
0
|
|
|
|
0
|
next unless $token->[1]{id} eq 'ind-dia'; |
|
108
|
|
|
|
|
|
|
|
|
109
|
0
|
|
|
|
|
0
|
while (my $token2 = $p->get_tag("p")) { |
|
110
|
0
|
0
|
|
|
|
0
|
next unless $token2->[1]{class} eq 'published-date'; |
|
111
|
0
|
|
|
|
|
0
|
last; |
|
112
|
|
|
|
|
|
|
} |
|
113
|
0
|
|
|
|
|
0
|
while (my $token2 = $p->get_tag("span")) { |
|
114
|
0
|
0
|
|
|
|
0
|
next unless $token2->[1]{id} eq 'Lbl_fecha'; |
|
115
|
0
|
|
|
|
|
0
|
last; |
|
116
|
|
|
|
|
|
|
} |
|
117
|
0
|
|
|
|
|
0
|
$fecha = $p->get_text; |
|
118
|
|
|
|
|
|
|
|
|
119
|
0
|
|
|
|
|
0
|
while (my $token2 = $p->get_tag("span")) { |
|
120
|
0
|
0
|
|
|
|
0
|
next unless $token2->[1]{id} eq 'RptListado_ctl03_lbl_valo'; |
|
121
|
0
|
|
|
|
|
0
|
$dolar = $p->get_text; |
|
122
|
0
|
|
|
|
|
0
|
last; |
|
123
|
|
|
|
|
|
|
} |
|
124
|
|
|
|
|
|
|
} |
|
125
|
|
|
|
|
|
|
|
|
126
|
0
|
0
|
0
|
|
|
0
|
return 0 unless $dolar and $fecha; |
|
127
|
0
|
0
|
|
|
|
0
|
return 0 unless $dolar =~ /^\d+[,.]{0,1}\d+$/; |
|
128
|
|
|
|
|
|
|
|
|
129
|
0
|
|
|
|
|
0
|
$self->{'dolar'} = $dolar; |
|
130
|
0
|
|
|
|
|
0
|
$self->{'fecha'} = $fecha; |
|
131
|
|
|
|
|
|
|
|
|
132
|
0
|
|
|
|
|
0
|
return 1; |
|
133
|
|
|
|
|
|
|
} |
|
134
|
|
|
|
|
|
|
|
|
135
|
|
|
|
|
|
|
=head2 date |
|
136
|
|
|
|
|
|
|
|
|
137
|
|
|
|
|
|
|
Return the date obtained from Banco Central's site. |
|
138
|
|
|
|
|
|
|
There's no formatting at all. |
|
139
|
|
|
|
|
|
|
|
|
140
|
|
|
|
|
|
|
=cut |
|
141
|
|
|
|
|
|
|
|
|
142
|
|
|
|
|
|
|
sub date { |
|
143
|
1
|
|
|
1
|
1
|
3
|
my $self = shift; |
|
144
|
|
|
|
|
|
|
|
|
145
|
1
|
|
|
|
|
8
|
return $self->{'fecha'}; |
|
146
|
|
|
|
|
|
|
} |
|
147
|
|
|
|
|
|
|
|
|
148
|
|
|
|
|
|
|
=head2 dollarValue |
|
149
|
|
|
|
|
|
|
|
|
150
|
|
|
|
|
|
|
Return the value of 1 dollar in CLP. |
|
151
|
|
|
|
|
|
|
There's no formatting. |
|
152
|
|
|
|
|
|
|
|
|
153
|
|
|
|
|
|
|
=cut |
|
154
|
|
|
|
|
|
|
|
|
155
|
|
|
|
|
|
|
sub dollarValue { |
|
156
|
1
|
|
|
1
|
1
|
5
|
my $self = shift; |
|
157
|
|
|
|
|
|
|
|
|
158
|
1
|
|
|
|
|
8
|
return $self->{'dolar'}; |
|
159
|
|
|
|
|
|
|
} |
|
160
|
|
|
|
|
|
|
|
|
161
|
|
|
|
|
|
|
=head2 rate |
|
162
|
|
|
|
|
|
|
|
|
163
|
|
|
|
|
|
|
Return the rate of transforming CLP in USD. |
|
164
|
|
|
|
|
|
|
If there's no value data, return -1 |
|
165
|
|
|
|
|
|
|
|
|
166
|
|
|
|
|
|
|
=cut |
|
167
|
|
|
|
|
|
|
|
|
168
|
|
|
|
|
|
|
sub rate { |
|
169
|
2
|
|
|
2
|
1
|
7
|
my $self = shift; |
|
170
|
|
|
|
|
|
|
|
|
171
|
2
|
|
|
|
|
5
|
my $valor = $self->{'dolar'}; |
|
172
|
|
|
|
|
|
|
|
|
173
|
|
|
|
|
|
|
# Changing decimal separator |
|
174
|
2
|
|
|
|
|
4
|
$valor =~ s/,/./; |
|
175
|
|
|
|
|
|
|
|
|
176
|
2
|
50
|
|
|
|
8
|
return -1 unless $valor; |
|
177
|
2
|
50
|
|
|
|
16
|
return -1 unless $valor =~ /^\d+[,.]{0,1}\d+$/; |
|
178
|
|
|
|
|
|
|
|
|
179
|
2
|
50
|
|
|
|
10
|
return -1 unless $valor >= 0; |
|
180
|
|
|
|
|
|
|
|
|
181
|
2
|
|
|
|
|
14
|
return 1 / $valor; |
|
182
|
|
|
|
|
|
|
} |
|
183
|
|
|
|
|
|
|
|
|
184
|
|
|
|
|
|
|
=head2 CLP2USD |
|
185
|
|
|
|
|
|
|
|
|
186
|
|
|
|
|
|
|
Convert the amount received as parameter (CLP) to USD, |
|
187
|
|
|
|
|
|
|
rounded at two decimals. |
|
188
|
|
|
|
|
|
|
Returns -1 on error. |
|
189
|
|
|
|
|
|
|
|
|
190
|
|
|
|
|
|
|
=cut |
|
191
|
|
|
|
|
|
|
|
|
192
|
|
|
|
|
|
|
sub CLP2USD { |
|
193
|
1
|
|
|
1
|
1
|
4
|
my $self = shift; |
|
194
|
1
|
|
|
|
|
2
|
my $pesos = shift; |
|
195
|
|
|
|
|
|
|
|
|
196
|
1
|
50
|
|
|
|
7
|
if ($pesos !~ /^\d+$/) { |
|
197
|
0
|
|
|
|
|
0
|
carp "Argument to CLP2USD must be an integer"; |
|
198
|
0
|
|
|
|
|
0
|
return -1; |
|
199
|
|
|
|
|
|
|
} |
|
200
|
|
|
|
|
|
|
|
|
201
|
1
|
|
|
|
|
5
|
my $rate = $self->rate; |
|
202
|
1
|
50
|
|
|
|
4
|
return -1 unless $rate >= 0; |
|
203
|
|
|
|
|
|
|
|
|
204
|
1
|
|
|
|
|
19
|
return sprintf("%.2f", $pesos * $rate); |
|
205
|
|
|
|
|
|
|
} |
|
206
|
|
|
|
|
|
|
|
|
207
|
|
|
|
|
|
|
=head2 USD2CLP |
|
208
|
|
|
|
|
|
|
|
|
209
|
|
|
|
|
|
|
Convert the amount received as parameter (USD) to CLP, |
|
210
|
|
|
|
|
|
|
rounded as integer (no decimals). |
|
211
|
|
|
|
|
|
|
Returns -1 on error. |
|
212
|
|
|
|
|
|
|
|
|
213
|
|
|
|
|
|
|
=cut |
|
214
|
|
|
|
|
|
|
|
|
215
|
|
|
|
|
|
|
sub USD2CLP { |
|
216
|
1
|
|
|
1
|
1
|
2
|
my $self = shift; |
|
217
|
1
|
|
|
|
|
2
|
my $dolares = shift; |
|
218
|
|
|
|
|
|
|
|
|
219
|
1
|
50
|
|
|
|
11
|
if ($dolares !~ /^\d+(\.\d{0,2}){0,1}$/) { |
|
220
|
0
|
|
|
|
|
0
|
carp "Wrong format in argument to USD2CLP"; |
|
221
|
0
|
|
|
|
|
0
|
return -1; |
|
222
|
|
|
|
|
|
|
} |
|
223
|
|
|
|
|
|
|
|
|
224
|
1
|
|
|
|
|
2
|
my $valor = $self->{'dolar'}; |
|
225
|
|
|
|
|
|
|
|
|
226
|
|
|
|
|
|
|
# Changing decimal separator |
|
227
|
1
|
|
|
|
|
3
|
$valor =~ s/,/./; |
|
228
|
|
|
|
|
|
|
|
|
229
|
1
|
50
|
|
|
|
5
|
return -1 unless $valor >= 0; |
|
230
|
|
|
|
|
|
|
|
|
231
|
1
|
|
|
|
|
7
|
return sprintf("%.0f", $dolares * $valor); |
|
232
|
|
|
|
|
|
|
} |
|
233
|
|
|
|
|
|
|
|
|
234
|
|
|
|
|
|
|
=head1 AUTHOR |
|
235
|
|
|
|
|
|
|
|
|
236
|
|
|
|
|
|
|
Hugo Salgado, C<< >> |
|
237
|
|
|
|
|
|
|
|
|
238
|
|
|
|
|
|
|
=head1 BUGS |
|
239
|
|
|
|
|
|
|
|
|
240
|
|
|
|
|
|
|
Please report any bugs or feature requests to C, or through |
|
241
|
|
|
|
|
|
|
the web interface at L. I will be notified, and then you'll |
|
242
|
|
|
|
|
|
|
automatically be notified of progress on your bug as I make changes. |
|
243
|
|
|
|
|
|
|
|
|
244
|
|
|
|
|
|
|
=head1 SUPPORT |
|
245
|
|
|
|
|
|
|
|
|
246
|
|
|
|
|
|
|
You can find documentation for this module with the perldoc command. |
|
247
|
|
|
|
|
|
|
|
|
248
|
|
|
|
|
|
|
perldoc Finance::Currency::Convert::BChile |
|
249
|
|
|
|
|
|
|
|
|
250
|
|
|
|
|
|
|
|
|
251
|
|
|
|
|
|
|
You can also look for information at: |
|
252
|
|
|
|
|
|
|
|
|
253
|
|
|
|
|
|
|
=over 4 |
|
254
|
|
|
|
|
|
|
|
|
255
|
|
|
|
|
|
|
=item * RT: CPAN's request tracker |
|
256
|
|
|
|
|
|
|
|
|
257
|
|
|
|
|
|
|
L |
|
258
|
|
|
|
|
|
|
|
|
259
|
|
|
|
|
|
|
=item * AnnoCPAN: Annotated CPAN documentation |
|
260
|
|
|
|
|
|
|
|
|
261
|
|
|
|
|
|
|
L |
|
262
|
|
|
|
|
|
|
|
|
263
|
|
|
|
|
|
|
=item * CPAN Ratings |
|
264
|
|
|
|
|
|
|
|
|
265
|
|
|
|
|
|
|
L |
|
266
|
|
|
|
|
|
|
|
|
267
|
|
|
|
|
|
|
=item * Search CPAN |
|
268
|
|
|
|
|
|
|
|
|
269
|
|
|
|
|
|
|
L |
|
270
|
|
|
|
|
|
|
|
|
271
|
|
|
|
|
|
|
=back |
|
272
|
|
|
|
|
|
|
|
|
273
|
|
|
|
|
|
|
=head1 SEE ALSO |
|
274
|
|
|
|
|
|
|
|
|
275
|
|
|
|
|
|
|
HTML::TokeParser |
|
276
|
|
|
|
|
|
|
LWP::UserAgent |
|
277
|
|
|
|
|
|
|
|
|
278
|
|
|
|
|
|
|
=cut |
|
279
|
|
|
|
|
|
|
|
|
280
|
|
|
|
|
|
|
=head1 TERMS OF USE |
|
281
|
|
|
|
|
|
|
|
|
282
|
|
|
|
|
|
|
The data obtained from bcentral site is under this policy: |
|
283
|
|
|
|
|
|
|
http://www.bcentral.cl/sitio/condiciones-uso.htm |
|
284
|
|
|
|
|
|
|
|
|
285
|
|
|
|
|
|
|
No liability is accepted by the author for abuse or miuse of |
|
286
|
|
|
|
|
|
|
the software herein. Use of this software is only permitted under |
|
287
|
|
|
|
|
|
|
the terms stipulated by bcentral.cl. |
|
288
|
|
|
|
|
|
|
|
|
289
|
|
|
|
|
|
|
=cut |
|
290
|
|
|
|
|
|
|
|
|
291
|
|
|
|
|
|
|
=head1 ACKNOWLEDGEMENTS |
|
292
|
|
|
|
|
|
|
|
|
293
|
|
|
|
|
|
|
This module was built for NIC Chile (http://www.nic.cl), who |
|
294
|
|
|
|
|
|
|
granted its liberation as free software. |
|
295
|
|
|
|
|
|
|
|
|
296
|
|
|
|
|
|
|
=head1 COPYRIGHT & LICENSE |
|
297
|
|
|
|
|
|
|
|
|
298
|
|
|
|
|
|
|
Copyright 2007 Hugo Salgado, all rights reserved. |
|
299
|
|
|
|
|
|
|
|
|
300
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or modify it |
|
301
|
|
|
|
|
|
|
under the same terms as Perl itself. |
|
302
|
|
|
|
|
|
|
|
|
303
|
|
|
|
|
|
|
=cut |
|
304
|
|
|
|
|
|
|
|
|
305
|
|
|
|
|
|
|
1; # End of Finance::Currency::Convert::BChile |