| 1 | 1 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 2 | 1 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 3 | package Validate::NPI; | ||||||||||
| 4 | # ABSTRACT: Validates National Provider Identifier (NPI) numbers | ||||||||||
| 5 | |||||||||||
| 6 | 1 | ▶ | use vars qw{ $VERSION @ISA @EXPORT }; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 7 | 1 | $VERSION = '0.03'; | |||||||||
| 8 | |||||||||||
| 9 | 1 | require Exporter; | |||||||||
| 10 | |||||||||||
| 11 | 1 | @ISA = qw(Exporter); | |||||||||
| 12 | 1 | @EXPORT = qw(validate_npi); | |||||||||
| 13 | |||||||||||
| 14 | 3 | ▶ | sub validate_npi { | ||||||||
|
Subroutine
validate_npi: called CC=7 SCAR=19.5
Pod
documented
| |||||||||||
| 15 | 3 | my ($value,$msg)=@_; | |||||||||
| 16 | |||||||||||
| 17 | # Assume the 9-position identifier part of the NPI is 123456789. | ||||||||||
| 18 | # Using the Luhn formula on the identifier portion, the check digit is calculated as follows: | ||||||||||
| 19 | # NPI without check digit: 1 2 3 4 5 6 7 8 9 | ||||||||||
| 20 | # Step 1: Double the value of alternate digits, beginning with the rightmost digit. | ||||||||||
| 21 | # 2 6 10 14 18 | ||||||||||
| 22 | # Step 2: Add constant 24, to account for the 80840 prefix that would be present on a card issuer | ||||||||||
| 23 | # identifier, plus the individual digits of products of doubling, plus unaffected digits. | ||||||||||
| 24 | # 24 + 2 + 2 + 6 + 4 + 1 + 0 + 6 + 1 + 4 + 8 + 1 + 8 = 67 | ||||||||||
| 25 | # Step 3: Subtract from next higher number ending in zero. | ||||||||||
| 26 | # 70 - 67 = 3 | ||||||||||
| 27 | # Check digit = 3 | ||||||||||
| 28 | # NPI with check digit = 1234567893 | ||||||||||
| 29 | |||||||||||
| 30 | 3 | ▶ | if ($value!~/^\d{10}$/) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 31 | 1 | ▶ | push @$msg,"NPI must be exactly 10 digits long" if ref $msg eq 'ARRAY'; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 32 | 1 | return 0; | |||||||||
| 33 | } | ||||||||||
| 34 | 2 | my @digits=split(//,$value); | |||||||||
| 35 | 2 | map { $digits[$_]*=2 } (0,2,4,6,8); | |||||||||
| 36 | 2 | my $sum=24; | |||||||||
| 37 | 2 | for my $d (@digits[0..8]) { | |||||||||
| 38 | 18 | ▶ | if ($d>9) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 39 | 6 | $sum+=int($d/10)+$d%10; # individual digits | |||||||||
| 40 | } else { | ||||||||||
| 41 | 12 | $sum+=$d; | |||||||||
| 42 | } | ||||||||||
| 43 | } | ||||||||||
| 44 | 2 | my $m=10*(int($sum/10)+1); | |||||||||
| 45 | 2 | $m-=$sum; | |||||||||
| 46 | 2 | ▶ | if ($m!=$digits[9]) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 47 | 1 | ▶ | push @$msg,"NPI does not validate" if ref $msg eq 'ARRAY'; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 48 | 1 | return 0; | |||||||||
| 49 | } | ||||||||||
| 50 | 1 | 1; | |||||||||
| 51 | } | ||||||||||
| 52 | |||||||||||
| 53 | 1 | 1; | |||||||||
| 54 | |||||||||||
| 55 | __END__ | ||||||||||