SICI/Util.pm

filter: Statementstmt 85.7% 60 / 70 Branchbran 66.6% 12 / 18 Conditioncond 33.3% 2 / 6 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 71.4% 10 / 14 Podpod 100% 2 / 2 totaltotal 75.4% 86 / 114 CC 14 SCAR 17.9
CC
14
Cov
79%
titleCode_from_title
11.8
calculate_check_char
8.0
__ANON__
2.0
CRAP 6.0
« SICI/Role/ValidSegment.pm ↑ Summary SICI/ContributionSegment.pm »
1  
2 package Biblio::SICI::Util;
3 {
4 4 $Biblio::SICI::Util::VERSION = '0.04';
5 }
6  
7 # ABSTRACT: Utility functions
8  
9 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 4 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 4 ▶use 5.010001;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 4 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
14 4 $Biblio::SICI::Util::TITLE_CODE = qr/[0-9A-Z&ยด*\\\{\}\(\)\[\],\@\$=!#%.+?";\/^\`~_|-]/;
15 }
16  
17 4 ▶use Exporter 'import';
Subroutine BEGIN: called CC=1 SCAR=0.0
18 4 our @EXPORT_OK = qw( calculate_check_char titleCode_from_title );
19  
20 4 ▶use Try::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
21  
22  
23 7 ▶sub titleCode_from_title {
Subroutine titleCode_from_title: called CC=7 SCAR=24.6
Pod documented
24 7 my $title = shift;
25  
26 7 ▶ die "Expected title string as parameter" unless defined($title) and $title;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $title and $title 0 7 7
Condition33%1/3
defined $title and $title
!ll&&!rl&&r
007
MC/DC0%0/2
defined $title and $title
defined $title $title
Truth table33%1/3
A and B
A= defined $titleB= $title
ABresult
0X0
100
111
27  
28 7 my $code = '';
29  
30 7 ▶ try {
Subroutine __ANON__: called CC=1 SCAR=0.0
31 7 require Text::Unidecode;
32 }
33 0 ▶ catch {
Subroutine __ANON__: not called CC=1 SCAR=6.9
34 0 warn __PACKAGE__ . "::titleCode_from_title() - unable to load 'Text::Unidecode': " . $_;
35 7 };
36  
37 7 ▶ try {
Subroutine __ANON__: called CC=1 SCAR=0.0
38 7 require Text::Undiacritic;
39 }
40 0 ▶ catch {
Subroutine __ANON__: not called CC=1 SCAR=6.9
41 0 warn __PACKAGE__ . "::titleCode_from_title() - unable to load 'Text::Undiacritic': " . $_;
42 7 };
43  
44 7 my @words = split( /\s+/, $title );
45 7 my @chars = ();
46 7 foreach my $word (@words) {
47 41 my $firstChar = uc( substr( $word, 0, 1 ) );
48 41 ▶ if ( $firstChar =~ $Biblio::SICI::Util::TITLE_CODE ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($firstChar =~ /$Biblio::SICI::Util::TITLE_CODE/) { } 41 0 41
49 41 push @chars, $firstChar;
50 }
51 else {
52 0 ▶ try {
Subroutine __ANON__: not called CC=1 SCAR=6.9
53 0 $word = Text::Unidecode::unidecode($word);
54 0 };
55 0 ▶ try {
Subroutine __ANON__: not called CC=1 SCAR=6.9
56 0 $word = Text::Undiacritic::undiacritic($word);
57 0 };
58 0 $firstChar = uc( substr( $word, 0, 1 ) );
59 0 ▶ if ( $firstChar =~ $Biblio::SICI::Util::TITLE_CODE ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($firstChar =~ /$Biblio::SICI::Util::TITLE_CODE/) 0 0 0
60 0 push @chars, $firstChar;
61 }
62 }
63 }
64  
65 7 ▶ if ( @chars >= 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@chars >= 1) 7 0 7
66 7 $code = join( "", splice( @chars, 0, 6 ) );
67 7 return $code;
68 }
69  
70 0 return;
71 } ## end sub titleCode_from_title
72  
73  
74 183 ▶sub calculate_check_char {
Subroutine calculate_check_char: called CC=8 SCAR=20.8
Pod documented
75 183 my $str = shift;
76  
77 183 ▶ return unless defined($str) and $str;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $str and $str 0 183 183
Condition33%1/3
defined $str and $str
!ll&&!rl&&r
00183
MC/DC0%0/2
defined $str and $str
defined $str $str
Truth table33%1/3
A and B
A= defined $strB= $str
ABresult
0X0
100
111
78  
79 183 state $charValues = {
80 0 => 0, 1 => 1, 2 => 2, 3 => 3, 4 => 4, 5 => 5, 6 => 6, 7 => 7,
81 8 => 8, 9 => 9, A => 10, B => 11, C => 12, D => 13, E => 14, F => 15,
82 G => 16, H => 17, I => 18, J => 19, K => 20, L => 21, M => 22, N => 23,
83 O => 24, P => 25, Q => 26, R => 27, S => 28, T => 29, U => 30, V => 31,
84 W => 32, X => 33, Y => 34, Z => 35, '#' => 36
85 };
86  
87 183 state $valueToChar = { reverse %{$charValues} };
88  
89 183 $str =~ s/\-[0-9A-Z#]\Z/-/o; # remove check char if present
90  
91 183 my @chars = split( //, $str );
92 183 my @mapped = ();
93 183 my $i = 0;
94 183 foreach my $c (@chars) {
95 6759 ▶ if ( exists $charValues->{$c} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $charValues->{$c}) { } 4773 1986 6759
96 4773 $mapped[$i] = $charValues->{$c};
97 }
98 else {
99 1986 $mapped[$i] = 36;
100 }
101 6759 $i++;
102 }
103  
104 183 my $sum = 0;
105 183 ▶ for ( my $j = $#mapped; $j >= 0; $j -= 2 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($j >= 0) 3438 183 3621
106 3438 $sum += $mapped[$j];
107 }
108  
109 183 $sum *= 3;
110  
111 183 ▶ for ( my $j = $#mapped - 1; $j >= 0; $j -= 2 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($j >= 0) 3321 183 3504
112 3321 $sum += $mapped[$j];
113 }
114  
115 183 my $mod = $sum % 37;
116  
117 # if remainder == 0; then 0 is the check char
118 183 ▶ return '0' if $mod == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $mod == 0 4 179 183
119  
120 179 my $checkVal = 37 - $mod;
121 179 my $checkChar = $valueToChar->{$checkVal};
122  
123 179 return $checkChar;
124 } ## end sub calculate_check_char
125  
126  
127 4 1;
128  
129 __END__
« SICI/Role/ValidSegment.pm ↑ Summary SICI/ContributionSegment.pm »