blib/lib/Se/PersonNr.pm untested

filter: Statementstmt 0.0% 0 / 79 Branchbran 0.0% 0 / 32 Conditioncond 0.0% 0 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 0.0% 0 / 11 Podpod 0.0% 0 / 9 totaltotal 0.0% 0 / 136 CC 17 SCAR 32.6
CC
17
Cov
0%
personnr
72.0
get_valid
12.0
new
6.0
CRAP 25.9
1 package Se::PersonNr;
2  
3 use strict;
4 use warnings;
5  
6 our $VERSION = '0.01';
7  
8 sub new {
9 my $proto = shift;
10 my $class = ref($proto) || $proto;
11  
12 my $self = {
13 year => '',
14 month => '',
15 day => '',
16 serial => '',
17 checksum => '',
18 };
19
20 bless ($self, $class);
21  
22 $self->personnr(shift) if (@_);
23  
24 return $self;
25 }
26  
27  
28 sub personnr {
29 my $self = shift;
30
31 if (@_) {
32 my $number = shift;
33
34 $self->{checksum} = chop($number);
35  
36 $self->{serial} = chop($number);
37 $self->{serial} = chop($number) . $self->{serial};
38 $self->{serial} = chop($number) . $self->{serial};
39
40 if (length($number) < 8) {
41 $self->{year} = substr($number, 0, 2);
42 $number = substr($number, 2);
43 } else {
44 $self->{year} = substr($number, 0, 4);
45 $number = substr($number, 4);
46 }
47
48 $self->{month} = substr($number, 0, 2);
49 $self->{day} = substr($number, 2, 2);
50
51 my ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday, $isdst) = gmtime;
52 $year += 1900;
53
54 if (length($self->{year}) < 4) {
55 if ($self->{year} > substr($year, 2, 2)) {
56 $self->{year} = (substr($year, 0, 2) - 1) . $self->{year};
57 } else {
58 $self->{year} = substr($year, 0, 2) . $self->{year};
59 }
60
61 if (length($number) == 5) {
62 if (substr($number, 4, 1) eq '+') {
63 $self->{year} = (substr($self->{year}, 0, 2) - 1) . substr($self->{year}, 2, 2);
64 }
65 }
66 }
67
68 }
69  
70 my ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday, $isdst) = gmtime;
71 $year += 1900;
72
73 my $yearsign = '-';
74  
75 if ($year > ($self->{year} + 100)) {
76 $yearsign = '+';
77 }
78
79 return substr($self->{year}, 2, 2) . $self->{month} . $self->{day} . $yearsign . $self->{serial} . $self->{checksum};
80 }
81  
82  
83 sub year {
84 my $self = shift;
85 $self->{year} = shift if (@_);
86 return $self->{year};
87 }
88  
89  
90 sub month {
91 my $self = shift;
92 $self->{month} = shift if (@_);
93 return $self->{month};
94 }
95  
96  
97 sub day {
98 my $self = shift;
99 $self->{day} = shift if (@_);
100 return $self->{day};
101 }
102  
103 sub serial {
104 my $self = shift;
105 $self->{serial} = shift if (@_);
106 return $self->{serial};
107 }
108  
109 sub checksum {
110 my $self = shift;
111 $self->{checksum} = shift if (@_);
112 return $self->{checksum};
113 }
114  
115  
116 sub is_valid {
117 my $self = shift;
118
119 if ($self->{checksum} == $self->get_valid(substr($self->{year}, 2, 2) . $self->{month} . $self->{day} . $self->{serial})) {
120 return 1;
121 }
122
123 return 0;
124 }
125  
126  
127 sub get_valid {
128 my $self = shift;
129 my $stn;
130
131 if (@_) {
132 $stn = shift;
133 } else {
134 $stn = substr($self->{year}, 2, 2) . $self->{month} . $self->{day} . $self->{serial};
135 }
136
137 my $digits;
138 my $thismod = 2;
139
140 # go through each digit and multiply with 2 and 1 alternated
141 while (length($stn) > 0) {
142 $digits .= chop($stn) * $thismod;
143  
144 if ($thismod > 1) {
145 $thismod = 1;
146 } else {
147 $thismod = 2;
148 }
149 }
150  
151 # get the sum of all digits
152 $stn = 0;
153 while (length($digits) > 0) {
154 $stn += int(chop($digits));
155 }
156  
157 # subtract the rightmost digit from 10 to get checksum
158 $digits = 10 - substr($stn, -1, 1);
159  
160 return $digits;
161 }
162  
163  
164 1;
165  
166 __END__