marquezc329 has asked for the wisdom of the Perl Monks concerning the following question:
I'm still new to programming and have been working through Lerning Perl and LPORM in conjunction with the perldoc. While reading an article I came across about how credit card numbers work, I figured it was a decent opportunity to practice what I've been learning. I know this is basic code, but I was hoping that in offering it up, the monks would review it and tell me if I'm striking out syntactically/semantically and/or demonstrating ignorance of generally accepted standards/conventions.
The code runs, and does what it's supposed to. I plan on modifying it later to include validation for 13,14, and 15 digit credit card numbers as well as possibly reading a list of numbers from a text file and writing only valid numbers to a new file. I wanted to wait on expanding the program until I was sure that I wasn't completely off track.
#!/usr/perl/bin #Simple program to check validity of 16 digit credit card numbers use strict; use warnings; sub checkNumber { my $in = shift; my @cardNum = @{$in}; my $i = $#cardNum - 1; while ($i >= 0) { $cardNum[$i] = $cardNum[$i] * 2; if ($cardNum[$i] >= 10) { $cardNum[$i] -= 9; } $i -= 2; } my $total = 0; $total += $_ foreach (@cardNum); if ($total % 10 == 0) { return 1; } else { return 0; } } sub cardInfo { my $cardNum = shift; my $issuer; if ($cardNum =~ /^4/) { $issuer = "VISA" } if ($cardNum =~ /^5/) { $issuer = "MasterCard" } if ($cardNum =~ /^6/) { $issuer = "Discover" } print "\n Card Number: $cardNum\n"; print " Issuer: ", $issuer,"\n\n"; } print "Enter Number: "; chomp(my $cardNum = <STDIN>); if ($cardNum =~ /^[0-9]+$/) { my @number = split(//,$cardNum); if ($#number == 15) { if (checkNumber(\@number)) { cardInfo($cardNum); } else { print "\nInvalid Card Number\n"; } } else { print "\n16 digit cards only for now.\n"; } } else { print "\nInvalid Input.\n"; }
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: Code Review: Simple Credit Card Number Validation
by GrandFather (Saint) on Oct 01, 2012 at 01:30 UTC | |
by marquezc329 (Scribe) on Oct 01, 2012 at 02:47 UTC | |
by GrandFather (Saint) on Oct 01, 2012 at 03:11 UTC | |
Re: Code Review: Simple Credit Card Number Validation
by toolic (Bishop) on Oct 01, 2012 at 01:32 UTC | |
by marquezc329 (Scribe) on Oct 01, 2012 at 02:51 UTC |
Back to
Seekers of Perl Wisdom