Thread Umsetzung eines Konsolenskripts: Hilfe für Perl-Newbie (17 answers)
Opened by subi at 2006-12-29 20:11

Ronnie
 2006-12-29 21:50
#9413 #9413
User since
2003-08-14
2022 Artikel
BenutzerIn
[default_avatar]
Hier ein kleiner Ansatz:
Code: (dl )
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#!/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;
}

View full thread Umsetzung eines Konsolenskripts: Hilfe für Perl-Newbie