错误消息"全局符号...需要显式包名称"多行

时间:2017-04-22 17:38:14

标签: perl

我在使用此代码时遇到问题。由于某些奇怪的原因,它会生成错误消息,如

  

全球符号" $ plan3"需要显式包名称

多行上有多个变量。

#!/usr/bin/perl

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

use CGI qw(:standard);
use CGI::Carp qw(warningsToBrowser fatalsToBrowser);

use strict;

my $recipient = 'email@myemail.com';

my $firstname, $lastname, $emailaddress, $paymentplan, $pricipalamount;
my $errors, $fn1, $lnl2, $ea3, $pp4, $pa5;
my $paymentamount;
my $email;

my $plan1 = 24, $plan2 = 48, $plan3 = 60;
my $month;

$firstname    = param( "firstname" );
$lastname     = param( "lastname" );
$emailaddress = param( "emailaddress" );
$paymentplan  = param( "paymentplan" );

$month           = param( "paymentplan" );
$principalamount = param( "principalamount" );
$errors          = 0;
$fn              = 0;
$ln              = 0;
$ea              = 0;
$pp              = 0;
$pa              = 0;

if ( $paymentplan == 1 ) {
    $paymentplan = $plan1;
}

if ( $paymentplan == 2 ) {
    $paymentplan = $plan2;
}

if ( $paymentplan == 3 ) {
    $paymentplan = $plan3;
}

$paymount = ( $amount * 1.14 ) / ( $plan - 2 );

$email = "Welcome to the CSIT Loan Company, $firstname $lastname! \n\n  Your 
email address is: $emailaddress \n  Your payment plan number is #$month \n  
Your Prinicpal Amount is \$  $principalamount \n  And your total is 
\$$paymentamount! \n\n  Thank you very much!";

if ( index( $address, "@" ) == -1 ) {
    print "Please use an @ sign!";
    $errors = 1;
}

if ( $firstname eq "" ) {
    $fn     = 1;
    $errors = 1;
}

if ( $lastname eq "" ) {
    $ln     = 1;
    $errors = 1;
}

if ( $emailaddress eq "" ) {
    $ea     = 1;
    $errors = 1;
}

if ( $paymemtplan ne "24" && $paymentplan ne "48" && $paymentplan ne "60" ) {
    $pp     = 1;
    $errors = 1;
}

if ( $principalamount < "5000" || $principalamount > "50000" ) {
    $pa     = 1;
    $errors = 1;
}

if ( $errors == 0 ) {

    print MAIL "To: $recipient\n";
    print MAIL "From:apmycs2\n";
    print MAIL "Subject:Server Validation\n\n";

    print MAIL $email;

    close( MAIL );
    print "Success! this is an email to notify of successful validation";
}
else {

    if ( $fn == 1 ) {
        print "<br>Please Enter your First Name!<br>";
    }

    if ( $ln == 1 ) {
        print "<br>Please Enter your Last Name!<br>";
    }

    if ( $ea == 1 ) {
        print "<br>Please Enter a valid Email Address!<br>";
    }

    if ( $pp == 1 ) {
        print "<br>Please Enter a Payment Plan of 1, 2, or 3!<br>";
    }

    if ( $pa == 1 ) {
        print "<br>Please Enter a Principal Amount between 5000 and 50000!<br>";
    }
}

print "<br><p>Thank you!<p>";

1 个答案:

答案 0 :(得分:3)

Perl希望你做一个列表赋值来同时声明多个变量:

 my( $plan1, $plan2, $plan3 ) = ( 24, 48, 60 );

否则,您有逗号运算符基本上尝试执行此操作,其中my仅适用于第一个:

 (my $plan1 = 24), ($plan2 = 48), ($plan3 = 60);

但是,您可能想要更容易管理的东西。您可以根据自己的计划数量创建不会改变的代码:

 my @payment_plans = ( undef, 24, 48, 60 );
 my $payment_plan_input = param( 'paymentplan' );
 my $fee;
 if( 1 <= $payment_plan_input and $payment_plan_input <= $#payment_plans ) {
      $fee = $payment_plans[ $payment_plan_input ];
      }

或者,哈希,它允许您将呈现给客户的名称映射到费用:

 my %payment_plan_fees = (
      basic  => 24,
      medium => 48,
      deluxe => 60,
      );

 my $payment_plan_input = param( 'paymentplan' );
 die "Bad input!" unless exists $payment_plan_fees{$payment_plan_input};
 my $fee = $payment_plan_fees{ $payment_plan_input };