File Coverage

blib/lib/Business/PT/BI.pm
Criterion Covered Total %
statement 17 17 100.0
branch 7 8 87.5
condition n/a
subroutine 3 3 100.0
pod 1 1 100.0
total 28 29 96.5


line stmt bran cond sub pod time code
1             package Business::PT::BI;
2              
3 2     2   54421 use warnings;
  2         4  
  2         62  
4 2     2   9 use strict;
  2         3  
  2         473  
5              
6             =head1 NAME
7              
8             Business::PT::BI - Validate Portuguese BI (Bilhete de Identidade)
9              
10             =head1 VERSION
11              
12             Version 0.01
13              
14             =cut
15              
16             our $VERSION = '0.01';
17              
18             require Exporter;
19              
20             our @ISA = qw(Exporter);
21              
22             our @EXPORT = qw(valid_bi);
23              
24             =head1 SYNOPSIS
25              
26             use Business::PT::BI;
27              
28             if ( valid_bi($bi, $control_number) ) {
29             # ...
30             }
31              
32             =head1 FUNCTIONS
33              
34             =head2 valid_bi
35              
36             Validates Portuguese BI's.
37              
38             A Portuguese BI number is comprised by eight digits. Between that
39             number and the "issue" box, the card also has a small box with a
40             single digit. That's the control digit. In order to validate a
41             Portuguese BI, you have to provide C with the BI number and
42             the control digit.
43              
44             valid_bi( $bi, $control_number );
45              
46             Validation is done as follows:
47              
48             1) BI is matched with /^\d{8}$/ (eigth consecutive digits with
49             nothing more)
50             2) First digit is multiplied by 9, second by 8, third by 7, fourth by
51             6, fifth by 5, sixth by 4, seventh by 3, eighth by 2
52             3) All the results of the multiplication are summed
53             4) Modulo of the sum by 11 is found
54             5) Complement of the sum by 11 is found
55             6) Control digit is compared to said complement
56              
57             Example for BI 12345678, with control digit 9:
58              
59             1) BI is matched with /^\d{8}/, test passes
60             2) Multiplication: 1*9, 2*8, 3*7, 4*6, 5*5, 6*4, 7*3, 8*2
61             3) Sum: 156
62             4) 156 % 11 = 2
63             5) 11 - 2 = 9
64             6) 9 == 9, test passes
65              
66             When the complement (the result of step 5) is greater than 9, the
67             number is assumed to be 0.
68              
69             =cut
70              
71             sub valid_bi {
72 13 100   13 1 55 my $bi = shift or return undef;
73 12 50       50 $bi =~ /^\d{8}$/ or return undef;
74              
75 12         15 my $control = shift;
76 12 100       32 defined $control or return undef;
77              
78 11         12 my $sum;
79 11         22 for (2..9) {
80 88         143 $sum += $_ * chop $bi;
81             }
82              
83 11         22 my $expected = 11 - $sum % 11;
84 11 100       24 if ($expected > 9) { $expected = 0 }
  1         2  
85              
86 11         56 return $control == $expected;
87             }
88              
89             =head1 AUTHOR
90              
91             Jose Castro, C<< >>
92              
93             =head1 BUGS
94              
95             Please report any bugs or feature requests to
96             C, or through the web interface at
97             L.
98             I will be notified, and then you'll automatically be notified of progress on
99             your bug as I make changes.
100              
101             =head1 SUPPORT
102              
103             You can find documentation for this module with the perldoc command.
104              
105             perldoc Business::PT::BI
106              
107             You can also look for information at:
108              
109             =over 4
110              
111             =item * AnnoCPAN: Annotated CPAN documentation
112              
113             L
114              
115             =item * CPAN Ratings
116              
117             L
118              
119             =item * RT: CPAN's request tracker
120              
121             L
122              
123             =item * Search CPAN
124              
125             L
126              
127             =back
128              
129             =head1 COPYRIGHT & LICENSE
130              
131             Copyright 2005 Jose Castro, all rights reserved.
132              
133             This program is free software; you can redistribute it and/or modify it
134             under the same terms as Perl itself.
135              
136             =cut
137              
138             1; # End of Business::PT::BI