Perl非法划分为零

时间:2013-03-07 15:25:51

标签: perl unix activestate divide-by-zero

我正在尝试运行这个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。我现在该怎么办?

1 个答案:

答案 0 :(得分:6)

您的代码不起作用的原因是chop返回从字符串末尾删除的字符,而不是删除最后一个字符的字符串。您还应该使用相同的chomp,除非它仅在换行符时返回最后一个字符。当文件的最后一行没有以换行符终止时,这可以避免出现问题。

此外,您必须始终使用use strictuse warnings启动Perl程序,并在其首次使用时声明所有变量。

您还应该避免使用名称上的&符号&调用子例程。自从大约二十年前的Perl 4以来,这种做法并不正确。你的电话应该是

read_exchange_rate();