File Coverage

blib/lib/Business/PT/NIF.pm
Criterion Covered Total %
statement 16 16 100.0
branch 6 6 100.0
condition n/a
subroutine 3 3 100.0
pod 1 1 100.0
total 26 26 100.0


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