File Coverage

blib/lib/Business/myIBAN.pm
Criterion Covered Total %
statement 33 33 100.0
branch 1 2 50.0
condition n/a
subroutine 7 7 100.0
pod 4 4 100.0
total 45 46 97.8


line stmt bran cond sub pod time code
1             package Business::myIBAN;
2 2     2   2631 use strict;
  2         3  
  2         64  
3 2     2   6 use warnings;
  2         3  
  2         43  
4 2     2   36 use 5.010;
  2         4  
  2         536  
5              
6             =head1 SYNOPSIS
7              
8             Package helper to compute an IBAN key, generate and format an IBAN from a BBAN
9             and the country code.
10              
11             =head2 to_digit $bban=$_
12              
13             Converts alpha characters into digit, following IBAN rules: replaces each
14             alpha character with 2 digits A = 10, B = 11, ..., Z = 35.
15              
16             C<$bban> A string to convert into the IBAN digit representation.
17              
18             returns a string representation of the Basic Bank Account Number (BBAN), which
19             contains only digits.
20              
21             =cut
22              
23             sub to_digit (_) {
24 6     6 1 18 my $bban = shift;
25 6         7 $bban = uc $bban;
26              
27 6         25 $bban =~ s/([A-Z])/(ord $1) - 55/eg;
  18         34  
28              
29 6         17 $bban;
30             }
31              
32             =head2 compute_key $country_code, $bban
33              
34             Computes the key corresponding to a given International Bank Account Number
35             (IBAN) when
36              
37             $country_code A string representation of the country code converted into IBAN digits.
38             $bban A string representation of the Basic Bank Account Number (BBAN)
39             with its key part and converted into IBAN digits.
40              
41             returns the IBAN key computed.
42              
43             =cut
44              
45             sub compute_key {
46 2     2 1 5 my $country_code = shift;
47 2         3 my $bban = shift;
48              
49 2         3 my $iban = $bban.$country_code.'00';
50              
51 2         3 my $rest = 0;
52 2         13 map { $rest = ($rest * 10 + $_ ) % 97 } split //, $iban;
  60         66  
53              
54 2         6 my $key = 98 - $rest;
55 2 50       7 if($key < 10)
56             {
57 2         4 $key = '0'.$key;
58             }
59              
60 2         8 $key;
61             }
62              
63             =head2 get_IBAN $country_code, $bban
64              
65             Computes and returns the International Bank Account Number (IBAN)
66             corresponding to the given country code and Basic Bank Account Number (BBAN).
67             where
68              
69             $country_code is the country code (i.e. "FR" for France, "DE" for Germany, etc.).
70             $bban is the Basic Bank Account Number (BBAN).
71              
72             returns a string representation of the International Bank Account Number
73             (IBAN) with the key part. The returned IBAN can contains alpha and digit
74             characters.
75              
76             =cut
77              
78             sub get_IBAN {
79 1     1 1 3 my $country_code = uc shift;
80 1         1 my $bban = shift;
81              
82 1         3 my $country_code_digit = to_digit($country_code);
83 1         2 my $bban_digit = to_digit($bban);
84              
85 1         2 my $iban_key = compute_key($country_code_digit, $bban_digit);
86              
87 1         4 format_with_spaces($country_code.$iban_key.$bban);
88             }
89              
90             =head2 format_with_spaces $iban
91              
92             Formats the IBAN provided and separate each 4 digit with a space.
93              
94             $iban an IBAN
95              
96             returns the IBAN separated each 4 digit with a space.
97              
98             =cut
99              
100             sub format_with_spaces {
101 1     1 1 2 my $iban = shift;
102 1         2 $iban =~ s/ //g;
103             # Only works with French account with an IBAN length of 27 characters.
104             # The following instruction selects longuest match first: 4 characters if
105             # available, else 3 characters.
106 1         9 join ' ', ($iban =~ /.{3,4}/g);
107             }
108              
109             1;