Algorithm-LUHN

 view release on metacpan or  search on metacpan

Changes  view on Meta::CPAN

Revision history for Perl module Algorithm::LUHN

1.02 2014-06-30 NEILB
    - Updated github repo URL after changing my github username
    - Added Algorithm::Damm and Math::CheckDigits to SEE ALSO
    - Fixed errors in pod: using c<...> instead of C<...> and
      not escaping the > character.
    - Added [MetaJSON] to dist.ini so META.json is included in releases

1.01 2014-06-30 NEILB

    - Added SEE ALSO section with links to other LUHN modules.
    - Renamed CHANGES to Changes and reformatted as per CPAN::Changes::Spec
    - Moved LUHN.pm to lib/Algorithm/LUHN.pm, as per convention
    - Switched to Dist::Zilla and set min perl version to 5.006
    - Improved doc for is_valid(), in response to RT#43842
    - Added github repo to pod

1.00 2002-03-26 TAYERS

    - Fixed linguistic problem in error message
      ("not correct" vs.  "incorrect").

0.13 2001-05-03 TAYERS

    - Changed is_valid to always return 1 or '' (instead of 0 or
      sometimes undef) and it sets $ERROR when the check_digit doesn't match.
    - Small change to the calculation of the return value of check_digit.
      Purely for aesthetic reasons.

0.12 2001-05-02 TAYERS

lib/Algorithm/LUHN.pm  view on Meta::CPAN


Algorithm::LUHN - Calculate the Modulus 10 Double Add Double checksum

=head1 SYNOPSIS

  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");

t/01-cc.t  view on Meta::CPAN


# Check some straight-forward, numeric only values
my @values = qw/83764912 8 123456781234567 0 4992739871 6/;
while (@values) {
  my ($v, $expected) = splice @values, 0, 2;
  my $c = check_digit($v);
  ok($c, $expected, "check_digit($v): expected $expected; got $c\n");
  ok(is_valid("$v$c"));
  ok(!is_valid("$v".(9-$c)));
  ok($Algorithm::LUHN::ERROR, qr/^Check digit/,
   "  Did not get the expected error. Got $Algorithm::LUHN::ERROR\n");

}

# Check a value including alphas (should fail).
my ($v, $c);
$v = 'A2';
ok(!($c=check_digit($v)));
$c ||= ''; # make sure $c is defined or we get an "uninit val" warning
ok($Algorithm::LUHN::ERROR, qr/\S/,
   "  Expected an error, but got a check_digit instead: $v => $c\n");
ok($Algorithm::LUHN::ERROR, qr/^Invalid/,
   "  Did not get the expected error. Got $Algorithm::LUHN::ERROR\n");

__END__

t/03-csp.t  view on Meta::CPAN


my @values = qw/83764912 8 123456781234567 0 4992739871 6
                392690QT 3 035231AH 2 157125AA 3/;
while (@values) {
  my ($v, $expected) = splice @values, 0, 2;
  my $c = check_digit($v);
  ok($c, $expected, "check_digit($v): expected $expected; got $c\n");
  ok(is_valid("$v$c"));
  ok(!is_valid("$v".(9-$c)));
  ok($Algorithm::LUHN::ERROR, qr/^Check digit/,
     "  Did not get the expected error. Got $Algorithm::LUHN::ERROR\n");
}

# Check a value including non-alphanum char (should fail).
my ($v, $c);
$v = '016783A@';
ok(!($c=check_digit($v)));
$c ||= ''; # make sure $c is defined or we get an "uninit val" warning
ok($Algorithm::LUHN::ERROR, qr/\S/,
   "  Expected an error, but got a check_digit instead: $v => $c\n");
ok($Algorithm::LUHN::ERROR, qr/^Invalid/,
   "  Did not get the expected error. Got $Algorithm::LUHN::ERROR\n");

__END__



( run in 0.666 second using v1.01-cache-2.11-cpan-65fba6d93b7 )