blib/lib/Business/ISIN.pm

filter: Statementstmt 88.8% 16 / 18 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 6 / 6 Podpod n/a totaltotal 91.6% 22 / 24 CC 1 SCAR 0.5
CC
1
Cov
89%
BEGIN
1.3
BEGIN
1.0
BEGIN
1.0
CRAP 1.0
↑ Summary
1 #######################################################################
2 # This package validates ISINs and calculates the check digit
3 #######################################################################
4  
5 package Business::ISIN;
6 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 require 5.005;
8  
9 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use vars qw($VERSION %country_code);
Subroutine BEGIN: called CC=1 SCAR=0.0
11 $VERSION = '0.20';
12  
13 1 ▶use subs qw(check_digit);
Subroutine BEGIN: called CC=1 SCAR=0.0
14 1 ▶use overload '""' => \&get; # "$isin" shows value
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16  
17 # Get list of valid two-letter country codes.
18 0 ▶use Locale::Country;
Subroutine BEGIN: called CC=1 SCAR=2.6
19 $country_code{$_} = 1 for map {uc} Locale::Country::all_country_codes();
20  
21 # Also include the non-country "country codes", used for bonds issued
22 # in multiple countries, etc..
23 $country_code{$_} = 1 for qw(XS XA XB XC XD);
24 #######################################################################
25 # Class Methods
26 #######################################################################
27  
28 sub new {
29 my $proto = shift;
30 my $initializer = shift;
31  
32 my $class = ref($proto) || $proto;
33 my $self = {value => undef, error => undef};
34 bless ($self, $class);
35  
36 $self->set($initializer) if defined $initializer;
37 return $self;
38 }
39  
40 #######################################################################
41 # Object Methods
42 #######################################################################
43  
44 sub set {
45 my ($self, $isin) = @_;
46 $self->{value} = $isin;
47 return $self;
48 }
49  
50 sub get {
51 my $self = shift;
52 return undef unless $self->is_valid;
53 return $self->{value};
54 }
55  
56 sub is_valid { # checks if self is a valid ISIN
57 my $self = shift;
58
59 # return not defined $self->error; # or for speed, do this instead
60 return (
61 $self->{value} =~ /^(([A-Za-z]{2})([A-Za-z0-9]{9}))([0-9]) $/x
62 and exists $country_code{uc $2}
63 and $4 == check_digit($1)
64 );
65 }
66  
67 sub error {
68 # returns the error string resulting from failure of is_valid
69 my $self = shift;
70 local $_ = $self->{value};
71  
72 /^([A-Za-z]{2})? ([A-Za-z0-9]{9})? ([0-9])? (.*)?$/x;
73  
74 return "'$_' does not start with a 2-letter country code"
75 unless length $1 > 0 and exists $country_code{uc $1};
76  
77 return "'$_' does not have characters 3-11 in [A-Za-z0-9]"
78 unless length $2 > 0;
79  
80 return "'$_' character 12 should be a digit"
81 unless length $3 > 0;
82  
83 return "'$_' has too many characters"
84 unless length $4 == 0;
85  
86 return "'$_' has an inconsistent check digit"
87 unless $3 == check_digit($1.$2);
88  
89 return undef;
90 }
91  
92  
93 #######################################################################
94 # Subroutines
95 #######################################################################
96  
97 sub check_digit {
98 # takes a 9 digit string, returns the "double-add-double" check digit
99 my $data = uc shift;
100  
101 $data =~ /^[A-Z]{2}[A-Z0-9]{9}$/ or croak "Invalid data: $data";
102  
103 $data =~ s/([A-Z])/ord($1) - 55/ge; # A->10, ..., Z->35.
104  
105 my @n = split //, $data; # take individual digits
106  
107 my $max = scalar @n - 1;
108 for my $i (0 .. $max) { if ($i % 2 == 0) { $n[$max - $i] *= 2 } }
109 # double every second digit, starting from the RIGHT hand side.
110  
111 for my $i (@n) { $i = $i % 10 + int $i / 10 } # add digits if >=10
112  
113 my $sum = 0; for my $i (@n) { $sum += $i } # get the sum of the digits
114  
115 return (10 - $sum) % 10; # tens complement, number between 0 and 9
116 }
117  
118 1;
119  
120  
121  
122 __END__
↑ Summary