1Algorithm::LUHN(3) User Contributed Perl Documentation Algorithm::LUHN(3)
2
3
4
6 Algorithm::LUHN - Calculate the Modulus 10 Double Add Double checksum
7
9 use Algorithm::LUHN qw/check_digit is_valid/;
10
11 $c = check_digit("43881234567");
12 print "It works\n" if is_valid("43881234567$c");
13
14 $c = check_digit("A2C4E6G8"); # this will cause an error
15
16 print "Valid LUHN characters are:\n";
17 my %vc = Algorithm::LUHN::valid_chars();
18 for (sort keys %vc) {
19 print "$_ => $vc{$_}\n";
20 }
21
22 Algorithm::LUHN::valid_chars(map {$_ => ord($_)-ord('A')+10} A..Z);
23 $c = check_digit("A2C4E6G8");
24 print "It worked again\n" if is_valid("A2C4E6G8$c");
25
27 This module calculates the Modulus 10 Double Add Double checksum, also
28 known as the LUHN Formula. This algorithm is used to verify credit card
29 numbers and Standard & Poor's security identifiers such as CUSIP's and
30 CSIN's.
31
32 You can find plenty of information about the algorithm by searching the
33 web for "modulus 10 double add double".
34
36 is_valid CHECKSUMMED_NUM
37 This function takes a credit-card number and returns true if the
38 number passes the LUHN check.
39
40 Ie it returns true if the final character of CHECKSUMMED_NUM is the
41 correct checksum for the rest of the number and false if not.
42 Obviously the final character does not factor into the checksum
43 calculation. False will also be returned if NUM contains in an
44 invalid character as defined by valid_chars(). If NUM is not valid,
45 $Algorithm::LUHN::ERROR will contain the reason.
46
47 This function is equivalent to
48
49 substr $N,length($N)-1 eq check_digit(substr $N,0,length($N)-1)
50
51 For example, "4242 4242 4242 4242" is a valid Visa card number,
52 that is provided for test purposes. The final digit is '2', which
53 is the right check digit. If you change it to a '3', it's not a
54 valid card number. Ie:
55
56 is_valid('4242424242424242'); # true
57 is_valid('4242424242424243'); # false
58
59 check_digit NUM
60 This function returns the checksum of the given number. If it
61 cannot calculate the check_digit it will return undef and set
62 $Algorithm::LUHN::ERROR to contain the reason why.
63
64 valid_chars LIST
65 By default this module only recognizes 0..9 as valid characters,
66 but sometimes you want to consider other characters as valid, e.g.
67 Standard & Poor's identifers may contain 0..9, A..Z, @, #, *. This
68 function allows you to add additional characters to the accepted
69 list.
70
71 LIST is a mapping of "character" => "value". For example, Standard
72 & Poor's maps A..Z to 10..35 so the LIST to add these valid
73 characters would be (A, 10, B, 11, C, 12, ...)
74
75 Please note that this adds or re-maps characters, so any characters
76 already considered valid but not in LIST will remain valid.
77
78 If you do not provide LIST, this function returns the current valid
79 character map.
80
82 Algorithm::CheckDigits provides a front-end to a large collection of
83 modules for working with check digits.
84
85 Business::CreditCard provides three functions for checking credit card
86 numbers. Business::CreditCard::Object provides an OO interface to those
87 functions.
88
89 Business::CardInfo provides a class for holding credit card details,
90 and has a type constraint on the card number, to ensure it passes the
91 LUHN check.
92
93 Business::CCCheck provides a number of functions for checking credit
94 card numbers.
95
96 Regexp::Common supports combined LUHN and issuer checking against a
97 card number.
98
99 Algorithm::Damm implements a different kind of check digit algorithm,
100 the Damm algorithm <https://en.wikipedia.org/wiki/Damm_algorithm>
101 (Damm, not Damn).
102
103 Math::CheckDigits implements yet another approach to check digits.
104
105 I have also written a review of LUHN modules
106 <http://neilb.org/reviews/luhn.html>, which covers them in more detail
107 than this section.
108
110 <https://github.com/neilb/Algorithm-LUHN>
111
113 This module was written by Tim Ayers
114 (http://search.cpan.org/search?author=TAYERS).
115
117 Copyright (c) 2001 Tim Ayers. All rights reserved.
118
120 This program is free software; you can redistribute it and/or modify it
121 under the same terms as Perl itself.
122
123
124
125perl v5.32.0 2020-07-28 Algorithm::LUHN(3)