0

この 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。私は今何をしますか?

4

1 に答える 1

6

コードが機能しない理由はchop、最後の文字が削除された文字列ではなく、文字列の末尾から削除された文字を返すためです。chompまた、改行の場合にのみ最後の文字を返すことを除いて、同一の which を使用する必要があります。これにより、ファイルの最後の行が改行で終わっていない場合の問題が回避されます。

さらに、常にPerl プログラムをuse strictおよびuse warningsで開始し、すべての変数を最初に使用する時点で宣言する必要があります。

&また、名前にアンパサンドを付けてサブルーチンを呼び出すことも避ける必要があります。これは、Perl 4 (約 20 年前) 以来、正しいやり方ではありませんでした。あなたの呼び出しは次のようになります

read_exchange_rate();
于 2013-03-07T15:31:26.150 に答える