blib/lib/MARC/Utils/MARC2Ini.pm

filter: Statementstmt 100% 71 / 71 Branchbran 91.6% 22 / 24 Conditioncond 66.6% 4 / 6 MC/DCmcdc 50.0% 2 / 4 Subroutinesub 100% 11 / 11 Podpod 0.0% 0 / 3 totaltotal 92.4% 110 / 119 CC 20 SCAR 19.2
CC
20
Cov
96%
ini2marc
12.0
marc2ini
6.0
quote
4.0
CRAP 6.8
↑ Summary
1 #---------------------------------------------------------------------
2 package MARC::Utils::MARC2Ini;
3  
4 1 ▶use 5.008002;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 1 our $VERSION = '0.02';
9  
10 1 our (@ISA, @EXPORT_OK);
11 1 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 require Exporter;
13 1 @ISA = qw(Exporter);
14 1 @EXPORT_OK = qw( marc2ini ini2marc );
15 }
16  
17 1 ▶use constant equals => ' = ' ;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 1 ▶use constant blank_line => '' ;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 1 ▶use constant null_section => '_' ;
Subroutine BEGIN: called CC=1 SCAR=0.0
20  
21 1 ▶use MARC::Record;
Subroutine BEGIN: called CC=1 SCAR=0.0
22  
23 95 ▶sub quote {
Subroutine quote: called CC=4 SCAR=13.9
Pod undocumented
24 95 my( $data ) = @_;
25 95 ▶ for( $data ) { return "'$_'" if /^ / or / $/ } # dumb quotes
Branch: 1 branch
BranchTrueFalseTotal
if /^ / or / $/ 33 62 95
Condition66%2/3
/^ / or / $/
l!l&&r!l&&!r
33062
MC/DC50%1/2
/^ / or / $/
/^ / / $/
Truth table66%2/3
A or B
A= /^ /B= / $/
ABresult
1X1
011
000
26 62 $data; # returned
27 }
28  
29 #---------------------------------------------------------------------
30 1 ▶sub marc2ini {
Subroutine marc2ini: called CC=6 SCAR=17.9
Pod undocumented
31 1 my( $marc_record ) = @_;
32  
33 1 my @ini;
34  
35 1 for my $leader ( $marc_record->leader() ) {
36 1 push @ini, join equals, leader => quote $leader;
37 }
38  
39 1 for my $field ( $marc_record->fields() ) {
40  
41 27 my $ftag = $field->tag();
42  
43 27 ▶ if( $field->is_control_field() ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($field->is_control_field) { } 4 23 27
44 4 push @ini, join equals, $ftag => quote $field->data();
45 }
46  
47 else {
48 23 push @ini, blank_line, "[$ftag]";
49  
50 23 for my $i ( 1, 2 ) {
51 46 push @ini, join equals, "ind$i" => quote $field->indicator( $i );
52 }
53  
54 23 for my $subfield ( $field->subfields ) {
55 44 push @ini, join equals, $subfield->[0] => quote $subfield->[1];
56 }
57 }
58 }
59  
60 1 join( "\n" => @ini ) . "\n"; # returned
61 }
62  
63 #---------------------------------------------------------------------
64 2 ▶sub ini2marc {
Subroutine ini2marc: called CC=12 SCAR=24.9
Pod undocumented
65 2 my( $ini_string ) = @_;
66  
67 2 my $marc_record = MARC::Record->new();
68  
69 2 open my $fh, '<', \$ini_string;
70  
71 2 my $section = null_section;
72 2 my( $tag, $data, @field );
73  
74 2 local *_;
75 2 ▶ while( <$fh> ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline $fh)) 283 2 285
76  
77 # comment or blank line
78 283 ▶ if( /^\s*[#;]/ or /^\s*$/ ) { next }
Branch: 1 branch
BranchTrueFalseTotal
if (/^\s*[#;]/ or /^\s*$/) 47 236 283
Condition66%2/3
/^\s*[#;]/ or /^\s*$/
l!l&&r!l&&!r
047236
MC/DC50%1/2
/^\s*[#;]/ or /^\s*$/
/^\s*[#;]/ /^\s*$/
Truth table66%2/3
A or B
A= /^\s*[#;]/B= /^\s*$/
ABresult
1X1
011
000
79  
80 # [section]
81 236 ▶ if( /^\[([^\]]*)\]/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (/^\[([^\]]*)\]/) 46 190 236
82 46 $section = $1;
83 46 ▶ $marc_record->append_fields( MARC::Field->new( @field ) ) if @field;
Branch: 1 branch
BranchTrueFalseTotal
if @field 44 2 46
84 46 @field = $section;
85 46 next;
86 }
87  
88 # tag = data (tag="data" tag='data')
89 190 ▶ if( /^\s*([^=:]+?)\s*[=:]\s*(.*)$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (/^\s*([^=:]+?)\s*[=:]\s*(.*)$/) 190 0 190
90 190 $tag = $1;
91 190 $data = $2;
92 190 ▶ $data = $2 if $data =~ /^(['"])(.*)\1$/; # dumb quotes
Branch: 1 branch
BranchTrueFalseTotal
if $data =~ /^(['"])(.*)\1$/ 79 111 190
93 }
94  
95 # control fields
96 190 ▶ if( $section eq null_section ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($section eq '_') { } 10 180 190
97 10 ▶ if( $tag eq 'leader' ) { $marc_record->leader( $data ) }
Branch: 1 branch
BranchTrueFalseTotal
if ($tag eq 'leader') { } 2 8 10
98 8 else { $marc_record->append_fields( MARC::Field->new( $tag, $data ) ) }
99 }
100  
101 else {
102 180 ▶ if( $tag =~ /^ind[12]$/ ) { push @field, $data }
Branch: 1 branch
BranchTrueFalseTotal
if ($tag =~ /^ind[12]$/) { } 92 88 180
103 88 else { push @field, $tag, $data }
104 }
105 }
106 2 ▶ $marc_record->append_fields( MARC::Field->new( @field ) ) if @field;
Branch: 1 branch
BranchTrueFalseTotal
if @field 2 0 2
107  
108 2 $marc_record; #returned
109 }
110  
111 1 1;
112 __END__
↑ Summary