File Coverage

blib/lib/Medical/NHSNumber.pm
Criterion Covered Total %
statement 25 26 96.1
branch 8 10 80.0
condition 3 3 100.0
subroutine 3 3 100.0
pod 1 1 100.0
total 40 43 93.0


line stmt bran cond sub pod time code
1             package Medical::NHSNumber;
2              
3 2     2   33497 use warnings;
  2         5  
  2         73  
4 2     2   9 use strict;
  2         3  
  2         6075  
5              
6             =head1 NAME
7              
8             Medical::NHSNumber - Check if an NHS number is valid
9              
10             =head1 VERSION
11              
12             Version 0.04
13              
14             =cut
15              
16             our $VERSION = '0.04';
17              
18              
19             =head1 SYNOPSIS
20              
21             Everyone registered with the NHS in England and Wales has their own
22             unique NHS number. The NHS number enables health care professionals
23             in the UK to obtain, link and index electronic health records for an
24             individual across a multiple sources of clinical information.
25              
26             Given a new-style NHS number, returns true if the number is
27             valid and false if the number appears to be invalid.
28              
29             use Medical::NHSNumber;
30              
31             my $nhs_no = '1234561234';
32             my $rv = Medical::NHSNumber::is_valid( $nhs_no );
33              
34             The validation is performed using the Modulus 11 algorith. More
35             details on the exact implementation followed can be found on the
36             NHS Connecting for Health website at L
37              
38             Additional information on Modulus 11 can also be found on this
39             HP site L
40              
41             Please note, this module will only check the validity of new style
42             NHS numbers i.e. ten digit numbers. There are over 21 different formats
43             for older styles of NHS numbers which currently, this module does
44             not support. If you do happen to know the specifics for the older
45             versions and would like to tell me, I would be more than happy to
46             incorporate some logic for them.
47              
48             =head1 FUNCTIONS
49              
50             =head2 is_valid
51              
52             Given a new-style NHS number, returns true if the number is
53             valid and false if the number appears to be invalid.
54              
55             my $rv = Medical::NHSNumber::is_valid( $nhs_no );
56              
57             =cut
58              
59             my $RH_WEIGHTS = {
60             0 => 10,
61             1 => 9,
62             2 => 8,
63             3 => 7,
64             4 => 6,
65             5 => 5,
66             6 => 4,
67             7 => 3,
68             8 => 2,
69             };
70              
71             sub is_valid {
72 6     6 1 1667 my $number = shift;
73              
74             return
75 6 50       16 unless ( defined $number);
76            
77 6         26 my @digits = split //, $number;
78            
79             return
80 6 100       24 unless ( scalar(@digits) == 10 );
81            
82             return
83 4 100       17 unless ( $number =~ /\d{10}/ );
84            
85             ##
86             ## obtain the check digit, 10th digit in NHS number
87            
88 3         4 my $check_digit = pop @digits;
89            
90             ##
91             ## Loop through first 9 digits and add up
92             ## their weighted factor.
93            
94 3         7 my $sum;
95 3         4 my $n = 0;
96 3         6 foreach my $digit ( @digits ){
97 27         34 my $weight = $RH_WEIGHTS->{ $n };
98 27         33 my $weighted_digit = $weight * $digit;
99 27         27 $sum += $weighted_digit;
100 27         45 $n++;
101             }
102            
103 3         13 my $remainder = $sum % 11;
104 3         6 my $calculated_check_digit = 11 - $remainder;
105            
106             ##
107             ## If check digit is 11, reset it to zero.
108            
109 3 50       7 if ( $calculated_check_digit == 11 ) {
110 0         0 $calculated_check_digit = 0;
111             }
112            
113             ##
114             ## If the calculated check digit is not equal
115             ## to the 10th digit of the entered NHS number
116             ## or is equal to 10, the number is invalid.
117            
118 3 100 100     15 if (
119             $calculated_check_digit == 10 ||
120             $calculated_check_digit != $check_digit ) {
121 2         9 return ;
122             } else {
123 1         6 return 1;
124             }
125              
126             }
127              
128              
129             =head1 AUTHOR
130              
131             Dr Spiros Denaxas, C<< >>
132              
133             =head1 BUGS
134              
135             Please report any bugs or feature requests to C, or through
136             the web interface at L. I will be notified, and then you'll
137             automatically be notified of progress on your bug as I make changes.
138              
139             =head1 SUPPORT
140              
141             You can find documentation for this module with the perldoc command.
142              
143             perldoc Medical::NHSNumber
144              
145              
146             You can also look for information at:
147              
148             =over 4
149              
150             =item * RT: CPAN's request tracker
151              
152             L
153              
154             =item * AnnoCPAN: Annotated CPAN documentation
155              
156             L
157              
158             =item * CPAN Ratings
159              
160             L
161              
162             =item * Search CPAN
163              
164             L
165              
166             =item * Source code
167              
168             The source code can be found on github L
169              
170             =back
171              
172             =head1 COPYRIGHT & LICENSE
173              
174             Copyright 2009 Dr Spiros Denaxas.
175              
176             This program is free software; you can redistribute it and/or modify it
177             under the terms of either: the GNU General Public License as published
178             by the Free Software Foundation; or the Artistic License.
179              
180             See http://dev.perl.org/licenses/ for more information.
181              
182              
183             =cut
184              
185             1; # End of Medical::NHSNumber