Perl programmer for hire: download my resume (PDF).
John Bokma Perl
freelance Perl programmer

Currency converter interface

Posting a form example | 4 comments

The Perl program given on this page is an example, for educational purposes only, of how to use the LWP::UserAgent and HTML::TreeBuilder modules to download a web page and extract information from it. Don't use this program to automatically extract data from XE.com, since the site strictly forbids this. The Perl program is just a simple example to get you started on how to fill in a web form and using the POST method to get the result back.

The Perl program is very barebone and improvement on the code is left as an exercise to the reader.

# xe.pl - Currency converter interface
#
# © Copyright, 2004-2005 By John Bokma, http://johnbokma.com/
#
# This script is for educational purposes only.
# Don't use it to extract data from http://www.xe.com/ since
# the site explicitly forbids this.
#
# $Id$ 

use strict;
use warnings;

use LWP::UserAgent;
use HTML::TreeBuilder;

unless ( @ARGV == 3 ) {

    print "usage: xe.pl amount from to\n";
    print "    example: xe.pl 200 usd eur\n";
    exit( 1 );
}

my ( $amount, $from, $to ) = @ARGV;

my $ua = LWP::UserAgent->new( agent => 'Mozilla/5.0' );

my $response = $ua->post(

    'http://www.xe.com/ucc/convert.cgi', [

        Amount => $amount,
        From   => uc $from,
        To     => uc $to,
    ]
);

$response->is_success or
    die $response->status_line;

my $root = HTML::TreeBuilder->
    new_from_content( $response->content );

my @td = $root->look_down( _tag => 'td', width => '49%' );

if ( @td == 2 ) {

    my @values;

    for ( @td ) {

        my $span = $_->look_down( _tag => 'span' );
        push @values, $span->as_text;
        $span->delete;

        my $text = $_->as_text;
        $text =~ s/\s+$//;
        push @values, $text;
    }

    print "$values[0] ($values[1]) = $values[2] ($values[3])\n";

} else {

    print "No result\n";
}

$root->delete;

Explanation of how the Perl program works

The program first creates a "browser" using the LWP::UserAgent module. Note that setting the User-Agent header is necessary, otherwise a page is returned stating that Automated extraction of data from our services is an explicit violation of the Terms of Use Agreement.

Next, the amount and valuta codes are POSTed to the convert.cgi CGI program via this browser object. The resulting content, an HTML page, is used to create an HTML::TreeBuilder object.

The resulting HTML page has two table cells (td elements) that have the width attribute set to a value of 49%. The first cell contains the formatted "from" value and the second cell contains the formatted result.

The look_down method is used to find those two td elements in the HTML parse tree created by the new_from_content constructor of HTML::TreeBuilder.

Each value is inside a span element, so first the value and valuta code is extracted, and then the span element is deleted from the td element, leaving the full description of the valuta code which is then extracted. The description has white space to the right, which is deleted. Both results added to the @values array for both table cells, so 4 items in the end which are printed formatted on one line.

In case there are not exactly two td elements found with a width of 49%, the Perl program assumes that there is no result, and hence outputs "No result".

Example of usage

Examples of the currency converter program usage and output:

xe.pl 100 eur usd
100.00 EUR (Euro) = 122.774 USD (United States Dollars)

xe.pl 1000 mxn eur
1,000.00 MXN (Mexico Pesos) = 71.3280 EUR (Euro)

xe.pl 50 sit usd
50.00 SIT (Slovenia Tolars) = 0.254731 USD (United States Dollars)

Note that Google recently also supports currency converting, see the Google calculator interface for more information.

Links

Please post a comment | read 4 comments, latest by John Bokma | RSS feed