citabriabob has asked for the wisdom of the Perl Monks concerning the following question:

Hello, newbie here: Trying to add CC validation to script, but getting 500-errors. Have already checked and syntax is OK. Help?
sub sanity_checks { local(%data) = @_; local($any); if (!($data{'26Email'} =~ /.+@.+\..+/)) { &print_error("The email address entered does not appear to be a va +lid address. Please enter an address in the form \"who\@where.com\". +"); return 0; } elsif ((!($data{'01Member'} =~ /^[0-9]{1,4}$/)) && ($data{'01Membe +r'})) { &print_error("The member ID number is entered incorrectly."); return 0; # check CC } elsif (processCard($data{'51CardNum'}) = 2) { &print_error("Credit Card Number Invalid please try again."); return 0; } elsif ($data{$_}){ ++$any; } return 1; } # test CC number 11/7/2015 sub processCard { my ($cardNum) = @_; # &print_error("Test should be cc no. @_"); return 2 if $cardNum !~ /^[0-9]+$/; # return 2 if length $cardNum != 16; return 2 if !numberOK($cardNum); } sub numberOK { my @cardNum = split //, shift; for my $i (map {$_ * 2} 0 .. $#cardNum / 2) { $cardNum[$i] *= 2; $cardNum[$i] -= 9 if $cardNum[$i] >= 10; } my $total = 0; $total += $_ for (@cardNum); return $total % 10 == 0; } # end processCard sub

Replies are listed 'Best First'.
Re: CGI Script sub
by davido (Cardinal) on Nov 08, 2015 at 22:41 UTC

    One reason this code can't work is line 13:

    } elsif (processCard($data{'51CardNum'}) = 2) {

    ...because that attempts to assign 2 to an lvalue sub named processCard, but that subroutine is not defined as an lvalue sub. I'm sure you intended to use the relational == operator:

    } elsif (processCard($data{'51CardNum'}) == 2) {

    Also, on line 5:

    if (!($data{'26Email'} =~ /.+@.+\..+/)) {

    It is by sheer luck that Perl is treating @. as the literal character @, and the metacharacter .. If what came after @ were, instead, valid "identifier" characters, it would trigger variable interpolation. To be safe, precede the @ inside of regular expressions (update: with a \ escape) unless you intend for it to be treated as an array sigil.


    Dave

      Changing operator to == worked! Thank you all so much. Is there anyone out there that could review these scripts and offer suggestions? Paid, of course.

        Check out http://jobs.perl.org, or one of the many freelance sites if you're looking to pay someone to do a code review. Here you get small amounts of professional quality help in return for asking interesting questions. ;)


        Dave

      Missed your note on line 5; do you mean a backslash before the @ character? Sorry to be so obtuse, but these string manipulations are difficult for me to see.
        ... a backslash before the [literal] @ character?

        Taking the liberty of replying for fellow monk davido: Yes.


        Give a man a fish:  <%-{-{-{-<

Re: CGI Script sub
by 1nickt (Canon) on Nov 08, 2015 at 22:40 UTC

    Try putting

    use CGI::Carp qw/ fatalsToBrowser /;
    at the top of your script. This will make the cause of the error appear in the browser. Right now it is in the web server error log.

    See CGI::Carp

    P.S. Also see Regexp::Common ... some of what you are doing has been done before ;-)

    The way forward always starts with a minimal test.