この Perl プログラムを実行しようとしています。
#!/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, "
17 行目 ( ) に到達するたびに$baseval = $amnt * (1/$rateof{$curr})
、エラーが発生しますIllegal division by zero
。
どうしたの?
私はPerlが初めてなので、あなたの答えを説明してください。
これは Strawberry Perl でのみ発生します。ActivePerl は機能しますが、すべての通貨換算が 0.0 としてリストされます。
更新: コードを次のように変更しました。
#!/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);
}
Open With を使用すると、Strawberry Perl でこれが表示されます (はい、Windows を使用しています)。
No such file or directory
しかし、ダブルクリックすると問題なく開始しますが、セッションは次のようになります。
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:
明らかに何かが間違っています。のすべてのインスタンスを既に に変更しchop
ましたchomp
。私は今何をしますか?