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?

有帮助吗?

解决方案

The reason your code doesn't work is that chop returns the character removed from the end of the string, not the string with the last character removed. You should also be using chomp which is identical except that it returns the last character only if it is a newline. That avoids problems when the last line of a file isn't terminated with a newline.

In addition you must always start a Perl program with use strict and use warnings, and declare all variables at their first point of use.

You should also avoid calling subroutines with the ampersand & on the name. That hasn't been correct practice since Perl 4 - about twenty years ago. Your calls should look like

read_exchange_rate();
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top