温度转换器 Perl

Temperature Converter Perl

我的代码有问题。华氏度和摄氏度的转换都不会显示,当我 select "Celsius to Fahrenheit" 时,它会给我华氏度到摄氏度的转换。我不确定我做错了什么。我已经寻求帮助,但一无所获。所以任何帮助将不胜感激!如果需要,这里是 link:https://crux.baker.edu/~wmorni01/WEB221_HTML/chapxx/c07case1.html

Perl 脚本(已修订):

#!/usr/bin/perl
#c07case1.cgi - Temperature conversion
print "Content-type: text/html\n\n";
use CGI qw(:standard);
use strict;

#variables
my ($temp, $fahConvert, $celConvert);
$temp = param('temp');
$unit = param('unit');
$fahConvert = param('fah');
$celConvert = param('cel');

#calculate
    if ($unit eq 'cel') {
        cel();
        $celConvert = ($temp - 32) * 5 / 9;

    }
    else {
        fah();
        $fahConvert = $temp * 9 / 5 + 32;
    }
sub cel {
    print "<html>\n";
    print "<head><title>Conversion from Celsius</title></head>\n";
    print "<body>\n";
    print "<h3>Celsius: $temp </h3>\n";
    print "<h3>Fahrenheit: $fahConvert </h3>\n";
    print "</body>\n";
    print "</html>\n";
}
sub fah {
    print "<html>\n";
    print "<head><title>Conversion from Fahrenheit</title></head>\n";
    print "<body>\n";
    print "<h3>Fahrenheit: $temp </h3>\n";
    print "<h3>Celsius: $celConvert </h3>\n";
    print "</body>\n";
    print "</html>\n";
}

HTML 脚本(已修订):

<!c07case1.html>
<HTML>
<HEAD><TITLE>Washington Middle School</TITLE></HEAD>
<BODY>
<H1>Temperature Converter</H1>
<HR>
<FORM ACTION="https://crux.baker.edu/~wmorni01/cgi-bin/chap07_wendy_morningstar/c07case1.cgi" METHOD=POST>
<P><B>Temperature:</B> <INPUT TYPE=text NAME=temp></P>

<INPUT TYPE=radio NAME=unit Value=fah CHECKED>Fahrenheit to Celsius<BR>
<INPUT TYPE=radio NAME=unit Value=cel>Celsius to Fahrenheit<BR>

<P><INPUT TYPE=submit VALUE=Convert></P>
</FORM></BODY></HTML>

您的两个输入都被命名为 temp。从解决这个问题开始。

<P><B>Temperature:</B> <INPUT TYPE=text NAME=temp></P>

<INPUT TYPE=radio NAME=unit Value=fah CHECKED>Fahrenheit to Celsius<BR>
<INPUT TYPE=radio NAME=unit Value=cel>Celsius to Fahrenheit<BR>

使用以下方法获取两个字段的值:

my $temp = param('temp');  # Grab the temperature
my $unit = param('unit');  # "fah" or "cel"

使用以下方法检查温度是否为摄氏度:

if ($unit eq 'cel') { ... }

最后,您仅在 生成使用将用于存储值的变量的 HTML 之后计算转换后的值 。您可以简单地颠倒计算和子调用的顺序,但最好避免使用全局变量而使用参数。

#!/usr/bin/perl
#c07case1.cgi - Temperature conversion

use strict;
use warnings;

use CGI qw(:standard);

sub fah {
    my ($cel, $fah) = @_;
    print "<html>\n";
    print "<head><title>Conversion to Fahrenheit</title></head>\n";
    print "<body>\n";
    print "<h3>Celsius: $cel </h3>\n";
    print "<h3>Fahrenheit: $fah </h3>\n";
    print "</body>\n";
    print "</html>\n";
}

sub cel {
    my ($fah, $cel) = @_;
    print "<html>\n";
    print "<head><title>Conversion to Celcius</title></head>\n";
    print "<body>\n";
    print "<h3>Fahrenheit: $fah </h3>\n";
    print "<h3>Celsius: $cel </h3>\n";
    print "</body>\n";
    print "</html>\n";
}

{
    print "Content-type: text/html\n\n";

    my $temp = param('temp');  # Grab the temperature
    my $unit = param('unit');  # "fah" or "cel"

    if ($unit eq 'cel') {
        cel( $temp, ($temp - 32) * 5 / 9 );
    } else {
        fah( $temp, $temp * 9 / 5 + 32 );
    }
}