File Coverage

blib/lib/Identifier/PL/PESEL.pm
Criterion Covered Total %
statement 26 26 100.0
branch 9 10 90.0
condition n/a
subroutine 6 6 100.0
pod 3 3 100.0
total 44 45 97.7


line stmt bran cond sub pod time code
1             package Identifier::PL::PESEL;
2              
3 3     3   466873 use strict;
  3         5  
  3         87  
4 3     3   9 use warnings;
  3         5  
  3         120  
5              
6 3     3   10 use Carp;
  3         19  
  3         1040  
7              
8             =head1 NAME
9              
10             Identifier::PL::PESEL - Validator for polish PESEL number.
11              
12             =head1 VERSION
13              
14             Version 0.4
15              
16             =cut
17              
18             our $VERSION = '0.4';
19              
20             =head1 SYNOPSIS
21              
22             use Identifier::PL::PESEL;
23              
24             my $pesel_number = '02070803628';
25             my $psl = Identifier::PL::PESEL->new();
26             print "OK" if $psl->validate( $pesel_number );
27             print $psl->gender( $pesel_number );
28              
29             =head1 DESCRIPTION
30              
31             More informations about PESEL L
32              
33             =head1 METHODS
34              
35             =head2 new
36              
37             Create new object of C
38              
39             =cut
40              
41             sub new {
42 3     3 1 179941 return bless {}, $_[0];
43             }
44              
45             =head2 validate
46              
47             Validate given PESEL number.
48              
49             Return 1 if number is valid.
50             Otherwise return undef.
51              
52             C will be called if number to validate is missing.
53              
54             =cut
55              
56             sub validate {
57 12     12 1 392 my ( $self, $pesel ) = @_;
58              
59 12 100       68 confess 'Missing pesel parameter' unless defined $pesel;
60              
61 11 100       65 return unless $pesel =~ /^\d{11}$/;
62              
63 6         30 my @p = split '', $pesel;
64 6         14 my $check_sum = pop @p;
65 6         17 my @weight = (1,3,7,9,1,3,7,9,1,3);
66 6         12 my $new_check_sum = 0;
67              
68 6         45 $new_check_sum += $_ * shift @weight for @p;
69 6         13 $new_check_sum %= 10;
70 6         9 $new_check_sum = 10 - $new_check_sum;
71              
72 6 100       27 return 1 if $check_sum == $new_check_sum;
73              
74 3         24 return;
75             }
76              
77             =head2 gender
78              
79             Returns the gender of the PESEL number owner.
80              
81             C will be called if number to validate is missing.
82              
83             =cut
84              
85             sub gender {
86 2     2 1 7 my ( $self, $pesel ) = @_;
87            
88 2 50       6 return unless $self->validate($pesel);
89              
90 2         7 my $gender_digit = substr $pesel, 9, 1;
91 2 100       15 return $gender_digit % 2 == 0 ? 'female' : 'male';
92             }
93              
94             =head1 AUTHOR
95              
96             Andrzej Cholewiusz
97              
98             =head1 COPYRIGHT
99              
100             The full text of the license can be found in the
101             LICENSE file included with this module.
102              
103             =cut
104              
105             1;