Beefy Boxes and Bandwidth Generously Provided by pair Networks chromatic writing perl on a camel
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Comment on

( #3333=superdoc: print w/ replies, xml ) Need Help??

First off put the card processing into a sub to make it easier to test the code. That way you can toss a bunch of test case numbers at the code and see that it does the right thing.

Next, deal with problems by bailing as soon as you can so that you don't clutter later code. In particular use early exits to avoid nested conditional blocks and loops.

There are a few other things I've done to make maintenance easier:

#!/usr/perl/bin #Simple program to check validity of 16 digit credit card numbers use strict; use warnings; print processCard($_) for 'x', '41245678912345', '3124567891234560', '3324567891234560', '4124567891234560'; sub processCard { my ($cardNum) = @_; return "Invalid Input: $cardNum.\n" if $cardNum !~ /^[0-9]+$/; return "16 digit cards only for now ($cardNum).\n" if length $cardNum != 16; return "Invalid Card Number $cardNum\n" if !numberOK($cardNum); return cardInfo($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; } sub cardInfo { my ($cardNum) = @_; my %issuers = (4 => "VISA", 5 => "MasterCard", 6 => "Discover"); my ($iDigit) = $cardNum =~ /^(\d)/; return "Unknown issuer code: $iDigit\n" if !exists $issuers{$iDigi +t}; return <<INFO; Card Number: $cardNum Issuer: $issuers{$iDigit} INFO }

Prints:

Invalid Input: x. 16 digit cards only for now (41245678912345). Invalid Card Number 3124567891234560 Unknown issuer code: 3 Card Number: 4124567891234560 Issuer: VISA
True laziness is hard work

In reply to Re: Code Review: Simple Credit Card Number Validation by GrandFather
in thread Code Review: Simple Credit Card Number Validation by marquezc329

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • Outside of code tags, you may need to use entities for some characters:
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others scrutinizing the Monastery: (13)
    As of 2014-04-23 17:42 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      April first is:







      Results (551 votes), past polls