#!/usr/bin/env perl
use strict;
use warnings;
use CGI;
use CGI::Carp qw/warningsToBrowser fatalsToBrowser/;
#use Data::Dumper;
package Money;
sub new {
my $class = shift;
my $self = {};
return bless $self, $class;
}
sub get {
my $self = shift;
return sprintf("%.2f", $self->{value}) . " EUR\n";
}
sub get_dm {
my $self = shift;
return sprintf("%.2f", $self->{value} * 1.95583) . " DM\n";
}
sub set {
my $self = shift;
my $val = shift || '0.0';
die "class money#set: value isn't numeric" unless $val =~ /^-?\d+\.?\d?\d?$/;
$self->{value} = $val;
}
sub set_from_dm {
my $self = shift;
my $val = shift || '0.0';
die "class money#set_dm: value isn't numeric" unless
$val =~ /^-?\d+\.?\d?\d?$/;
$self->{value} = $val / 1.95583;
}
package main;
my $q = CGI->new;
my %form = $q->Vars;
my $coins = Money->new;
if ($form{'action'} eq 'convert') {
my $money_bag;
if ($form{'cur'} eq 'EUR') {
$coins->set_from_dm($form{'val'});
$money_bag = $coins->get;
} else {
$coins->set($form{'val'});
$money_bag = $coins->get_dm;
}
print $q->header,
$q->start_html('CurrencyConverter'),
$q->h1('MoneyBag'),
$money_bag,
$q->end_html;
} else {
print $q->header,
$q->start_html('CurrencyConverter'),
$q->h1('CurrencyConverter'),
$q->start_form,
'Value: ', $q->textfield(-name=>'val'),
$q->br,
'convert to: ',
$q->radio_group('cur', ['EUR','DM']),
$q->br,
$q->submit('action', 'convert'),
$q->end_form,
$q->end_html;
}