Algorithm::LUHN(3)

๐Ÿ“ฆ
version 1.02 ยท 2 versions
, in fedora 43

๐Ÿ’– Algorithm::LUHN ๐Ÿ’–

okay bestie, gather round, because this is Algorithm::LUHN(3) from the User Contributed Perl Documentation section and it's literally the little module that goes beep boop and tells you if a number is legit or not. no cap. ๐ŸŽ€


โœจ NAME โœจ

Algorithm::LUHN โ€” Calculate the Modulus 10 Double Add Double checksum ๐Ÿงฎ๐Ÿ’…


๐ŸŒธ SYNOPSIS ๐ŸŒธ

here's the vibe, copy-paste ready:

  use Algorithm::LUHN qw/check_digit is_valid/;

  $c = check_digit("43881234567");
  print "It works\n" if is_valid("43881234567$c");

  $c = check_digit("A2C4E6G8"); # this will cause an error

  print "Valid LUHN characters are:\n";
  my %vc = Algorithm::LUHN::valid_chars();
  for (sort keys %vc) {
    print "$_ => $vc{$_}\n";
  }

  Algorithm::LUHN::valid_chars(map {$_ => ord($_)-ord('A')+10} A..Z);
  $c = check_digit("A2C4E6G8");
  print "It worked again\n" if is_valid("A2C4E6G8$c");

see that # this will cause an error line? yeah. she said what she said. ๐Ÿ™ˆ we get to why in a sec.


๐Ÿฆ‹ DESCRIPTION ๐Ÿฆ‹

so this module calculates the Modulus 10 Double Add Double checksum, which the girlies also call the LUHN Formula. it's the algorithm used to verify credit card numbers, and also Standard & Poor's security identifiers like CUSIP's and CSIN's. iconic behavior tbh. ๐Ÿ’ณโœจ

if you want the full lore, you can find plenty of information about the algorithm by searching the web for "modulus 10 double add double". iykyk. ๐Ÿ”ฅ


๐Ÿ‘‘ FUNCTION ๐Ÿ‘‘

๐Ÿ’œ is_valid CHECKSUMMED_NUM

this one takes a credit-card number and returns true if the number passes the LUHN check. simple as that, main character energy. ๐Ÿซถ

Ie it returns true if the final character of CHECKSUMMED_NUM is the correct checksum for the rest of the number, and false if not. obviously the final character does not factor into the checksum calculation (she's just there to be judged ๐Ÿ’€). False will also be returned if NUM contains in an invalid character as defined by valid_chars(). If NUM is not valid, $Algorithm::LUHN::ERROR will contain the reason. so like, always check the error var bestie, don't leave her hanging.

This function is equivalent to

substr $N,length($N)-1 eq check_digit(substr $N,0,length($N)-1)

For example, "4242 4242 4242 4242" is a valid Visa card number, that is provided for test purposes. The final digit is '2', which is the right check digit. If you change it to a '3', it's not a valid card number. Ie:

is_valid('4242424242424242');   # true
is_valid('4242424242424243');   # false
โญ note: that "4242 4242 4242 4242" number is a test number, provided for test purposes. do not get it twisted.

๐ŸŒˆ check_digit NUM

This function returns the checksum of the given number. If it cannot calculate the check_digit it will return undef and set $Algorithm::LUHN::ERROR to contain the reason why. so if you get undef back, go read $Algorithm::LUHN::ERROR, she's spilling the tea there. ๐Ÿ˜ญ

๐ŸŽ€ valid_chars LIST

By default this module only recognizes 0..9 as valid characters, but sometimes you want to consider other characters as valid, e.g. Standard & Poor's identifers may contain 0..9, A..Z, @, #, *. This function allows you to add additional characters to the accepted list. glow-up for your character set, honestly. โœจ

LIST is a mapping of "character" => "value". For example, Standard & Poor's maps A..Z to 10..35 so the LIST to add these valid characters would be (A, 10, B, 11, C, 12, ...).

๐Ÿ’€ please note that this adds or re-maps characters, so any characters already considered valid but not in LIST will remain valid. she adds, she does not replace. periodt.

If you do not provide LIST, this function returns the current valid character map. ๐Ÿ‘‘


๐ŸŒŸ SEE ALSO ๐ŸŒŸ

  • Algorithm::CheckDigits provides a front-end to a large collection of modules for working with check digits.
  • Business::CreditCard provides three functions for checking credit card numbers. Business::CreditCard::Object provides an OO interface to those functions.
  • Business::CardInfo provides a class for holding credit card details, and has a type constraint on the card number, to ensure it passes the LUHN check.
  • Business::CCCheck provides a number of functions for checking credit card numbers.
  • Regexp::Common supports combined LUHN and issuer checking against a card number.
  • Algorithm::Damm implements a different kind of check digit algorithm, the Damm algorithm <https://en.wikipedia.org/wiki/Damm_algorithm> (Damm, not Damn). ๐Ÿ’€ yes she really clarified that. it's Damm. not Damn.
  • Math::CheckDigits implements yet another approach to check digits.
  • I have also written a review of LUHN modules <http://neilb.org/reviews/luhn.html>, which covers them in more detail than this section. ๐Ÿ”ฅ

๐Ÿงš REPOSITORY ๐Ÿงš

<https://github.com/neilb/Algorithm-LUHN> โ€” go star her, she deserves it. โญ


๐Ÿ’– AUTHOR ๐Ÿ’–

This module was written by Tim Ayers (http://search.cpan.org/search?author=TAYERS). shoutout, king. ๐Ÿ‘‘


Copyright (c) 2001 Tim Ayers. All rights reserved.


๐Ÿฉท LICENSE ๐Ÿฉท

This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.


and that's the whole page bestie!! ๐ŸŽ‰ this was perl v5.42.0, dated 2025-07-25, and you just read Algorithm::LUHN(3). slay, go checksum something. ๐Ÿ’…๐Ÿฆ‹โœจ

๐Ÿ“ฆ where this page comes from