I am trying to run this Perl program.
#!/usr/bin/perl
# --------------- exchange.pl -----------------
&read_exchange_rate; # read exchange rate into memory
# now let's cycle, asking the user for input...
print "Please enter the amount, appending the first letter of the name of\n";
print "the currency that you're using (franc, yen, deutschmark, pound) -\n";
print "the default value is US dollars.\n\n";
print "Amount: ";
while (<STDIN>) {
($amnt,$curr) = &breakdown(chop($_));
$baseval = $amnt * (1/$rateof{$curr});
printf("%2.2f USD, ", $baseval * $rateof{'U'});
printf("%2.2f Franc, ", $baseval * $rateof{'F'});
printf("%2.2f DM, ", $baseval * $rateof{'D'});
printf("%2.2f Yen, and ", $baseval * $rateof{'Y'});
printf("%2.2f Pound\n\nAmount: ", $baseval * $rateof{'P'});
}
sub breakdown {
@line = split(" ", $_);
$amnt = $line[0];
if ($#line == 1) {
$curr = $line[1];
$curr =~ tr/a-z/A-Z/; # uppercase
$curr = substr($curr, 0, 1); # first char only
} else { $curr = "U"; }
return ($amnt, $curr);
}
sub read_exchange_rate {
open(EXCHRATES, "
Whenever it gets to line 17 ($baseval = $amnt * (1/$rateof{$curr})
), I get the error Illegal division by zero
.
What's wrong?
I am new to Perl, so please explain your answer.
This only happens in Strawberry Perl. ActivePerl works, but it lists all the currency conversions as 0.0.
UPDATE: I changed the code to look like this:
#!/usr/bin/perl
&read_exchange_rate; # read exchange rate into memory
# now let's cycle, asking the user for input...
print "Please enter the amount, appending the first letter of the name of\n";
print "the currency that you're using (franc, yen, deutschmark, pound) -\n";
print "the default value is US dollars.\n\n";
print "Amount: ";
while (<STDIN>) {
($amnt,$curr) = &breakdown(chomp($_));
$baseval = eval { $amnt * (1/$rateof{$curr}) };
printf("%2.2f USD, ", $baseval * $rateof{'U'});
printf("%2.2f Franc, ", $baseval * $rateof{'F'});
printf("%2.2f DM, ", $baseval * $rateof{'D'});
printf("%2.2f Yen, and ", $baseval * $rateof{'Y'});
printf("%2.2f Pound\n\nAmount: ", $baseval * $rateof{'P'});
}
sub breakdown {
@line = split(" ", $_);
$amnt = $line[0];
if ($#line == 1) {
$curr = $line[1];
$curr =~ tr/a-z/A-Z/; # uppercase
$curr = substr($curr, 0, 1); # first char only
} else { $curr = "U"; }
return ($amnt, $curr);
}
sub read_exchange_rate {
open EXCHRATES, "<exchange.db" or die "$!\n";
while ( <EXCHRATES> ) {
chomp; split;
$curr = $_[0];
$val = $_[1];
$rateof{$curr} = $val;
}
close(EXCHRATES);
}
Now, I'm getting this in Strawberry Perl when I use Open With(Yes, I'm on Windows):
No such file or directory
But if I double-click it, it starts fine, but the session looks kind of like this:
Please enter the amount, appending the first letter of the name of
the currency that you're using (franc, yen, deutschmark, pound) -
the default value is US dollars.
Amount: 5 y
0.00 USD, 0.00 Franc, 0.00 DM, 0.00 Yen, and 0.00 Pound
Amount:
Something is obviously wrong. I already changed all instances of chop
to chomp
. What do I do now?