user7880564
user7880564

Reputation:

Error message "Global symbol ... requires explicit package name" on multiple lines

I'm having trouble with this code. For some odd reason it generates error messages like

Global symbol "$plan3" requires explicit package name

on multiple lines for multiple variables.

#!/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 protected]';

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>";

Upvotes: 1

Views: 365

Answers (1)

brian d foy
brian d foy

Reputation: 132832

Perl wants you to make a list assignment to declare multiple variables at the same time:

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

Otherwise, you have the comma operator essentially trying to do this, where the my applies to only the first one:

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

But, you might want something a bit easier to manage. You can create code that doesn't change with the number of plans that you have:

 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 ];
      }

Or, a hash, which allows you to map the names you present to the customer to the fees:

 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 };

Upvotes: 3

Related Questions