Barcode/EAN8.pm

filter: Statementstmt 79.4% 54 / 68 Branchbran 50.0% 10 / 20 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 90.0% 9 / 10 Podpod 60.0% 3 / 5 totaltotal 73.7% 76 / 103 CC 11 SCAR 14.2
CC
11
Cov
73%
plot
12.0
init
4.9
calcEAN8CD
3.0
CRAP 4.1
« Barcode/Industrial2of5.pm ↑ Summary Barcode/Code39.pm »
1 package GD::Barcode::EAN8;
2 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 1 ▶use GD::Barcode;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use parent qw(Exporter);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use vars qw($VERSION @ISA $errStr);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 @ISA = qw(GD::Barcode Exporter);
9 1 our $VERSION = '2.02';
10 1 my $leftOddBar = {
11 '0' => '0001101',
12 '1' => '0011001',
13 '2' => '0010011',
14 '3' => '0111101',
15 '4' => '0100011',
16 '5' => '0110001',
17 '6' => '0101111',
18 '7' => '0111011',
19 '8' => '0110111',
20 '9' => '0001011'
21 };
22 1 my $rightBar = {
23 '0' => '1110010',
24 '1' => '1100110',
25 '2' => '1101100',
26 '3' => '1000010',
27 '4' => '1011100',
28 '5' => '1001110',
29 '6' => '1010000',
30 '7' => '1000100',
31 '8' => '1001000',
32 '9' => '1110100'
33 };
34 1 my $guardBar = 'G0G';
35 1 my $centerBar = '0G0G0';
36  
37 1 ▶sub new {
Subroutine new: called CC=2 SCAR=7.0
Pod documented
38 1 my ( $sClass, $sTxt ) = @_;
39 1 $errStr = '';
40 1 my $oThis = {};
41 1 bless $oThis, $sClass;
42 1 ▶ return if ( $errStr = $oThis->init($sTxt) );
Branch: 1 branch
BranchTrueFalseTotal
if $GD::Barcode::EAN8::errStr = $oThis->init($sTxt) 0 1 1
43 1 return $oThis;
44 }
45  
46 1 ▶sub init {
Subroutine init: called CC=4 SCAR=15.9
Pod undocumented
47 1 my ( $oThis, $sTxt ) = @_;
48  
49 #Check
50 1 ▶ return 'Invalid Characters' if ( $sTxt =~ /[^0-9]/ );
Branch: 1 branch
BranchTrueFalseTotal
if $sTxt =~ /[^0-9]/ 0 1 1
51  
52 #CalcCd
53 1 ▶ if ( length($sTxt) == 7 ) {
Branch: 2 branches
BranchTrueFalseTotal
if (length $sTxt == 7) { } 1 0 1
elsif (length $sTxt == 8) { } 0 0 0
54 1 $sTxt .= calcEAN8CD($sTxt);
55 }
56 elsif ( length($sTxt) == 8 ) {
57 ;
58 }
59 else {
60 0 return 'Invalid Length';
61 }
62 1 $oThis->{text} = $sTxt;
63 1 return '';
64 }
65  
66 1 ▶sub calcEAN8CD {
Subroutine calcEAN8CD: called CC=3 SCAR=11.0
Pod undocumented
67 1 my ($sTxt) = @_;
68 1 my ( $i, $iSum );
69  
70 1 my @aWeight = ( 3, 1, 3, 1, 3, 1, 3 );
71 1 $iSum = 0;
72 1 ▶ for ( $i = 0 ; $i < 7 ; $i++ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < 7) 7 1 8
73 7 $iSum += substr( $sTxt, $i, 1 ) * $aWeight[$i];
74 }
75 1 $iSum %= 10;
76 1 ▶ $iSum = ( $iSum == 0 ) ? 0 : ( 10 - $iSum );
Branch: 1 branch
BranchTrueFalseTotal
$iSum == 0 ? : 1 0 1
77 1 return "$iSum";
78 }
79  
80 1 ▶sub barcode {
Subroutine barcode: called CC=3 SCAR=11.0
Pod documented
81 1 my ($oThis) = @_;
82 1 my ( $i, $sRes );
83  
84 #(1) Init
85 1 my $sTxt = $oThis->{text};
86 1 $sRes = $guardBar; #GUARD
87  
88 #(2) Left 4
89 1 ▶ for ( $i = 0 ; $i < 4 ; $i++ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < 4) 4 1 5
90 4 $sRes .= GD::Barcode::barPtn( substr( $sTxt, $i, 1 ), $leftOddBar );
91 }
92  
93 #(3) Center
94 1 $sRes .= $centerBar;
95  
96 #(4) Right 4 bytes
97 1 ▶ for ( $i = 4 ; $i < 8 ; $i++ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < 8) 4 1 5
98 4 $sRes .= GD::Barcode::barPtn( substr( $sTxt, $i, 1 ), $rightBar );
99 }
100  
101 #(5)GUARD
102 1 $sRes .= $guardBar;
103 1 return $sRes;
104 }
105  
106 0 ▶sub plot {
Subroutine plot: not called CC=3 SCAR=24.8
Pod documented
107 0 my ( $oThis, %hParam ) = @_;
108  
109 0 my $sPtn = $oThis->barcode();
110  
111 #Create Image
112 0 require GD;
113 0 ▶ my $iHeight = ( $hParam{Height} ) ? $hParam{Height} : 50;
Branch: 1 branch
BranchTrueFalseTotal
$hParam{'Height'} ? : 0 0 0
114 0 my ( $oGd, $cBlack );
115 0 ▶ if ( $hParam{NoText} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($hParam{'NoText'}) { } 0 0 0
116 0 ( $oGd, $cBlack ) =
117 GD::Barcode::plot( $sPtn, length($sPtn), $iHeight, 0, 0 );
118 }
119 else {
120 0 my ( $fW, $fH ) = ( GD::Font->Small->width, GD::Font->Small->height );
121 0 my $iWidth = length($sPtn);
122  
123 0 ( $oGd, $cBlack ) =
124 GD::Barcode::plot( $sPtn, $iWidth, $iHeight, $fH, 0 );
125 $oGd->string(
126 GD::Font->Small, $fW + 1,
127 $iHeight - $fH,
128 0 substr( $oThis->{text}, 0, 4 ), $cBlack
129 );
130 $oGd->string(
131 GD::Font->Small, $fW + 33,
132 $iHeight - $fH,
133 0 substr( $oThis->{text}, 4, 4 ), $cBlack
134 );
135 }
136 0 return $oGd;
137 }
138 1 1;
139 __END__
« Barcode/Industrial2of5.pm ↑ Summary Barcode/Code39.pm »