blib/lib/MARC/MIR.pm

filter: Statementstmt 26.0% 50 / 192 Branchbran 10.0% 5 / 50 Conditioncond 11.1% 2 / 18 MC/DCmcdc 0.0% 0 / 10 Subroutinesub 26.3% 15 / 57 Podpod 9.6% 3 / 31 totaltotal 20.9% 75 / 358 CC 36 SCAR 26.1
CC
36
Cov
22%
to_iso2709
90.0
from_iso2709
20.0
iso2709_records_of
12.0
CRAP 13.6
↑ Summary
1 package MARC::MIR;
2 1 ▶use parent 'Exporter';
Subroutine BEGIN: called CC=1 SCAR=0.0
3 1 ▶use autodie;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use Modern::Perl;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use Perlude;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 # use Perlude::Sh qw< :all >;
7  
8 # ABSTRACT: DSL to manipulate MIR records.
9 1 our $VERSION = '0.4';
10  
11 # our %EXPORT_TAGS =
12 # ( dsl => [qw<
13 # with_fields
14 # with_subfields
15 # map_fields
16 # map_subfields
17 # grep_fields
18 # grep_subfields
19 # any_fields
20 # any_subfields
21 # map_values
22 #
23 # tag
24 # value
25 #
26 # with_value
27 # is_control
28 # record_id
29 #
30 # for_humans
31 # >]
32 # , debug => [qw< ready_to_see >]
33 # , iso2709 => [qw< from_iso2709 to_iso2709 iso2709_records_of >]
34 # , marawk => [qw< marawk $NUM $RAW $REC $ID %FIELDS >]
35 # , all => [qw<
36  
37 1 our @EXPORT = qw<
38 with_fields
39 with_subfields
40 map_fields
41 map_subfields
42 grep_fields
43 grep_subfields
44 any_fields
45 any_subfields
46 map_values
47  
48 with_datafields
49 map_datafields
50 grep_datafields
51 any_datafields
52  
53 append_subfields_to
54  
55 tag
56 value
57  
58 with_value
59 is_control
60 record_id
61  
62 for_humans
63 ready_to_see
64 from_iso2709 to_iso2709 iso2709_records_of
65 marawk $NUM $RAW $REC $ID %FIELDS
66  
67 yaz_marcdump
68  
69 record_charset
70  
71 cib_handlers cib_keys cib_reader cib_writer
72  
73 indicators
74 >;
75 # );
76 # our @EXPORT_OK = $EXPORT_TAGS{all} = [map @$_, values %EXPORT_TAGS];
77  
78 1 our $RS = "\x1d";
79 1 our $FS = "\x1e";
80 1 our $SS = "\x1f";
81  
82 0 ▶sub iso2709_records_of (_) {
Subroutine iso2709_records_of: not called CC=3 SCAR=24.8
Pod undocumented
83 0 my $fh;
84 0 ▶ if ( ref $_[0] ) { $fh = shift }
Branch: 1 branch
BranchTrueFalseTotal
if (ref $_[0]) { } 0 0 0
85 0 ▶ else { open $fh, shift or die $! }
Branch: 1 branch
BranchTrueFalseTotal
&MARC::MIR::open($fh, shift()) or die $! 0 0 0
86 0 ▶ sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
87 0 local $/ = $RS;
88 0 ▶ <$fh> // ();
Condition0%0/3
readline $fh // ()
l!l&&r!l&&!r
000
MC/DC0%0/2
readline $fh // ()
readline $fh ()
Truth table0%0/3
A // B
A= readline $fhB= ()
ABresult
1X1
011
000
89 }
90 0 }
91  
92 0 ▶sub ready_to_see (_) {
Subroutine ready_to_see: not called CC=1 SCAR=6.9
Pod undocumented
93 0 s/$ISO2709::FS/$ISO2709::FS\n/g;
94 0 $_
95 }
96  
97 0 ▶sub _fold_indicators {
Subroutine _fold_indicators: not called CC=3 SCAR=24.8
98 0 ▶ my $ind = shift or return " ";
Branch: 1 branch
BranchTrueFalseTotal
my $ind = shift() or return " " 0 0 0
99 0 ▶ ref $ind ? @$ind : $ind
Branch: 1 branch
BranchTrueFalseTotal
ref $ind ? : 0 0 0
100 }
101  
102 0 ▶sub to_iso2709 (_) {
Subroutine to_iso2709: not called CC=9 SCAR=45.0
Pod undocumented
103 # adapted from Frederic Demian's MARC::Moose serializer
104 0 state $empty_header = 'x'x24;
105  
106 0 my $rec = shift;
107 0 ▶ for ( $$rec[0] ) { length or $_ = $empty_header }
Branch: 1 branch
BranchTrueFalseTotal
length $_ or $_ = $empty_header 0 0 0
108 0 my (@directory,@data);
109 0 my $from = 0;
110  
111 # TODO: middleware anaromy_check (control fields)
112 # TODO: is serialization a middleware ?
113 0 for my $field ( @{ $$rec[1] } ) { # TODO: use map_fields ? :)
114 # my ( $tag, $data, $indicator ) = @$field;
115 0 my $last;
116 0 my $raw = do {
117 0 ▶ if ( ref $$field[1] ) { # data field
Branch: 1 branch
BranchTrueFalseTotal
if (ref $field->[1]) { } 0 0 0
118 0 $last = pop @{ $$field[1] };
119 join ''
120 , # TODO: is *this* a middleware ?
121 0 ▶ ( map { ref $_ ? @$_ : $_ } ($$field[2] ||= [' ',' '] ) )
Branch: 1 branch
BranchTrueFalseTotal
ref $_ ? : 0 0 0
Condition0%0/2
$field->[2] ||= [" ", " "]
l!l
00
MC/DC0%0/1
$field->[2] ||= [" ", " "]
$field->[2]
Truth table0%0/2
A
A= $field->[2]
Aresult
11
00
122 , $SS
123 0 , map( { @$_, $SS } @{ $$field[1] } )
124 , @$last
125 , $FS
126 }
127 else { # control field
128 0 $$field[1] . $FS;
129 }
130 };
131 0 ▶ $last and push @{ $$field[1] }, $last;
Branch: 1 branch
BranchTrueFalseTotal
$last and push @{$$field[1];}, $last 0 0 0
132  
133 # my $len = bytes::length( $raw );
134 0 my $len = length( $raw );
135 0 push @data, $raw;
136 0 push @directory
137 , sprintf( "%03s%04d%05d", $$field[0], $len, $from );
138  
139 0 $from+=$len;
140 }
141  
142 0 my $offset = 24 + 12 * @{ $$rec[1] } + 1;
143 0 my $length = $offset + $from + 1;
144  
145 # $length > 9999 and die "$length bytes is too long for a marc record";
146  
147 0 for ( $$rec[0] ) {
148 0 substr($_, 0, 5) = sprintf("%05d", $length);
149 0 substr($_, 12, 5) = sprintf("%05d", $offset);
150 # Default leader various pseudo variable fields
151 # Force UNICODE MARC21: substr($$rec[0], 9, 1) = 'a';
152 # those are defaults described at http://archive.ifla.org/VI/3/p1996-1/uni.htm
153 # xxxxnAxxxxxxxxxxxxxxxx
154 # A:
155 # a printed language
156 # b manuscript language
157 # c printed scores
158 # d manuscript scores
159 # e printed carto
160 # f manuscript carto
161 # g video
162 # i sound
163 # j music
164 # k tron
165 # m multimedia
166 # r 3D
167  
168 }
169  
170 0 join ''
171 , $$rec[0]
172 , @directory
173 , $FS
174 , @data
175 , $RS
176 }
177  
178  
179 0 ▶sub _field {
Subroutine _field: not called CC=3 SCAR=24.8
180 0 my @chunks = split /\x1f(.)/;
181 0 ▶ return @chunks if @chunks == 1;
Branch: 1 branch
BranchTrueFalseTotal
if @chunks == 1 0 0 0
182 0 my @subfields;
183 0 my $indicators = [split //, shift @chunks];
184 0 ▶ while (@chunks) {
Branch: 1 branch
BranchTrueFalseTotal
if (@chunks) 0 0 0
185 0 push @subfields, [splice @chunks,0,2];
186 }
187 0 \@subfields, $indicators;
188 }
189  
190 0 ▶sub from_iso2709 (_) {
Subroutine from_iso2709: not called CC=4 SCAR=30.0
Pod undocumented
191 0 my $raw = shift;
192 0 chop $raw;
193 0 my ( $head, @fields ) = split /\x1e/, $raw;
194 0 ▶ @fields or die "raw $raw";
Branch: 1 branch
BranchTrueFalseTotal
@fields or die "raw $raw" 0 0 0
195 0 ▶ $head =~ /(.{24})/cg or die;
Branch: 1 branch
BranchTrueFalseTotal
$head =~ /(.{24})/cgu or die 0 0 0
196 0 my $leader = $1;
197 0 my @tags = $head =~ /\G(\d{3})\d{9}/cg;
198 0 ▶ unless ( $head =~ /\G$/cg ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($head =~ /\G$/cgu) 0 0 0
199 0 die "head tailing ".( $head =~ /(.*)/cg );
200 }
201 0 [ $leader
202 , [ map [ shift(@tags), _field ], @fields ]
203 ];
204 }
205  
206 0 ▶sub _control_field_for_human {
Subroutine _control_field_for_human: not called CC=2 SCAR=17.9
207 0 ▶ ref $$_[1]
Branch: 1 branch
BranchTrueFalseTotal
ref $_->[1] ? : 0 0 0
208 ? ()
209 : "$$_[0] $$_[1]"
210 }
211  
212 0 ▶sub _data_field_for_human {
Subroutine _data_field_for_human: not called CC=2 SCAR=17.9
213 0 my ($tag, $subfields, $indicators) = @$_;
214 0 ▶ ref $subfields or return (); # probably a control field
Branch: 1 branch
BranchTrueFalseTotal
ref $subfields or return () 0 0 0
215 join ''
216 , $tag
217 , '(' , _fold_indicators( $indicators ) , ') '
218 , map {
219 0 ' $'
220 , $$_[0]
221 , ' '
222 , $$_[1]
223 } @$subfields
224 }
225  
226 0 ▶sub for_humans (_) {
Subroutine for_humans: not called CC=3 SCAR=24.8
Pod undocumented
227 0 my $record = shift;
228 join "\n"
229 , $$record[0]
230 , map {
231 0 ▶ _control_field_for_human ||
Condition0%0/5
_control_field_for_human() || _data_field_for_human() || die(YAML::Dump({q[can't humanize ], $_}))
l!l
00
_control_field_for_human() or _data_field_for_human()
l!l&&r!l&&!r
000
MC/DC0%0/2
_control_field_for_human() || _data_field_for_human() || die(YAML::Dump({q[can't humanize ], $_}))
_control_field_for_human() _data_field_for_human()
Truth table0%0/3
A or B
A= _control_field_for_human()B= _data_field_for_human()
ABresult
1X1
011
000
232 _data_field_for_human || die YAML::Dump { "can't humanize ", $_ }
233 0 } @{ $$record[1]}
234 }
235  
236  
237 0 ▶sub is_control (_) {
Subroutine is_control: not called CC=1 SCAR=6.9
Pod undocumented
238 0 my $r = shift;
239 0 @$r == 2;
240 }
241 22 ▶sub tag (_) { @{ shift() }[0] }
Subroutine tag: called CC=1 SCAR=0.0
Pod documented
242 0 ▶sub value (_) { @{ shift() }[1] }
Subroutine value: not called CC=1 SCAR=6.9
Pod documented
243  
244 6 ▶sub _use_arg { push @_, $_ unless @_ > 1 }
Subroutine _use_arg: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
unless @_ > 1 1 5 6
245 4 ▶sub _one_or_array {
Subroutine _one_or_array: called CC=2 SCAR=7.2
246 4 my $r = shift;
247 4 ▶ (ref $r) ? @$r : $r
Branch: 1 branch
BranchTrueFalseTotal
ref $r ? : 4 0 4
248 }
249  
250  
251 2 ▶sub _with_data {
Subroutine _with_data: called CC=1 SCAR=0.0
252 2 &_use_arg;
253 2 my ( $code, $on ) = @_;
254 2 map { $code->() } $$on[1];
255 }
256  
257 0 ▶sub _map_data {
Subroutine _map_data: not called CC=1 SCAR=6.9
258 0 &_use_arg;
259 0 my ( $code, $on ) = @_;
260 0 map { $code->() } _one_or_array $$on[1]
261 }
262  
263 0 ▶sub _any_data {
Subroutine _any_data: not called CC=2 SCAR=17.9
264 0 &_use_arg;
265 0 my ( $code, $on ) = @_;
266 map {
267 0 my $r = $code->();
268 0 ▶ $r and return $r;
Condition0%0/2
$r && (return $r)
!ll
00
MC/DC0%0/1
$r && (return $r)
$r
Truth table0%0/2
A
A= $r
Aresult
00
11
269 } _one_or_array $$on[1]
270 };
271  
272 4 ▶sub _grep_data {
Subroutine _grep_data: called CC=1 SCAR=0.0
273 4 &_use_arg;
274 4 my ( $code, $on ) = @_;
275 4 grep { $code->() } _one_or_array $$on[1]
276 }
277  
278 2 ▶sub with_fields (&;$) { $_[1] ||= $_; &_with_data }
Subroutine with_fields: called CC=2 SCAR=7.1
Pod undocumented
Condition66%2/3
$_[1] ||= $_
l!l&&r!l&&!r
110
MC/DC0%0/2
$_[1] ||= $_
$_[1] $_
Truth table66%2/3
A ||= B
A= $_[1]B= $_
ABresult
1X1
011
000
279 0 ▶sub with_subfields (&;$) { $_[1] ||= $_; &_with_data }
Subroutine with_subfields: not called CC=2 SCAR=17.9
Pod undocumented
Condition0%0/3
$_[1] ||= $_
l!l&&r!l&&!r
000
MC/DC0%0/2
$_[1] ||= $_
$_[1] $_
Truth table0%0/3
A ||= B
A= $_[1]B= $_
ABresult
1X1
011
000
280 0 ▶sub map_fields (&;$) { &_map_data }
Subroutine map_fields: not called CC=1 SCAR=6.9
Pod undocumented
281 0 ▶sub map_subfields (&;$) { &_map_data }
Subroutine map_subfields: not called CC=1 SCAR=6.9
Pod undocumented
282 4 ▶sub grep_fields (&;$) { &_grep_data }
Subroutine grep_fields: called CC=1 SCAR=0.0
Pod undocumented
283 0 ▶sub grep_subfields (&;$) { &_grep_data }
Subroutine grep_subfields: not called CC=1 SCAR=6.9
Pod undocumented
284 0 ▶sub any_fields (&;$) { &_any_data }
Subroutine any_fields: not called CC=1 SCAR=6.9
Pod undocumented
285 0 ▶sub any_subfields (&;$) { &_any_data }
Subroutine any_subfields: not called CC=1 SCAR=6.9
Pod undocumented
286  
287 0 ▶sub datafields_only {
Subroutine datafields_only: not called CC=1 SCAR=6.9
Pod undocumented
288 0 my $r = $_[0];
289 0 ▶ $_[0] = sub { (is_control) ? () : $r->() };
Subroutine __ANON__: not called CC=2 SCAR=17.9
Branch: 1 branch
BranchTrueFalseTotal
&is_control($_) ? : 0 0 0
290 }
291  
292 0 ▶sub map_datafields (&;$) { &datafields_only; &_map_data }
Subroutine map_datafields: not called CC=1 SCAR=6.9
Pod undocumented
293 # sub with_datafields (&;$) { &datafields_only; &_with_data }
294 0 ▶sub grep_datafields (&;$) { &datafields_only; &_grep_data }
Subroutine grep_datafields: not called CC=1 SCAR=6.9
Pod undocumented
295 0 ▶sub any_datafields (&;$) { &datafields_only; &_any_data }
Subroutine any_datafields: not called CC=1 SCAR=6.9
Pod undocumented
296  
297 0 ▶sub with_value (&;$) {
Subroutine with_value: not called CC=2 SCAR=17.9
Pod undocumented
298 0 my $code = shift;
299 0 ▶ my $r = @_ ? shift : $_;
Branch: 1 branch
BranchTrueFalseTotal
@_ ? : 0 0 0
300 0 ( map $code->(), $$r[1] )[0];
301 }
302  
303 0 ▶sub record_id (_) {
Subroutine record_id: not called CC=1 SCAR=6.9
Pod undocumented
304 0 ▶ any_fields { tag eq '001' and value } shift;
Subroutine __ANON__: not called CC=2 SCAR=17.9
Branch: 1 branch
BranchTrueFalseTotal
&tag($_) eq "001" and &value($_) 0 0 0
305 }
306  
307 0 ▶sub map_values (&$;$) {
Subroutine map_values: not called CC=2 SCAR=17.9
Pod undocumented
308 0 my $code = shift;
309 0 my ( $fspec, $sspec ) = map { @$_ } shift;
310 0 ▶ my $rec = @_ ? shift : $_;
Branch: 1 branch
BranchTrueFalseTotal
@_ ? : 0 0 0
311 map {
312 0 ▶ map { with_value {$code->()} }
Subroutine __ANON__: not called CC=1 SCAR=6.9
313 0 ▶ grep_subfields { (tag) ~~ $sspec }
Subroutine __ANON__: not called CC=1 SCAR=6.9
314 0 ▶ } grep_fields { (tag) ~~ $fspec }
Subroutine __ANON__: not called CC=1 SCAR=6.9
315 # TODO: Benchmark: is it really faster ?
316 # map_fields {
317 # if ( (tag) ~~ $fspec ) {
318 # map_subfields {
319 # if ( (tag) ~~ $sspec ) {
320 # with_value { $code->() }
321 # } else { () }
322 # }
323 # } else { () }
324 # } $rec
325 0 }
326  
327 0 ▶sub marawk (&$) {
Subroutine marawk: not called CC=2 SCAR=17.9
Pod undocumented
328 0 my $code = shift;
329 my ($stream) = map {
330 0 ref $_
331 ? $_
332 0 ▶ : concatM {iso2709_records_of} ls $_
Subroutine __ANON__: not called CC=1 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
ref $_ ? : 0 0 0
333 } $_[0];
334  
335 0 our ( $NUM, $RAW, $REC, $ID, %FIELDS )
336 = ( 0 );
337 0 ▶ now {
Subroutine __ANON__: not called CC=2 SCAR=17.9
338 0 $NUM++;
339 0 $RAW = $_;
340 0 $_ = $REC = from_iso2709 $_;
341 0 ▶ $ID = record_id or die "no ID inthere :". for_humans;
Branch: 1 branch
BranchTrueFalseTotal
$MARC::MIR::ID = &record_id($_) or die 'no ID inthere :' . &for_humans($_) 0 0 0
342 0 %FIELDS=();
343  
344 0 ▶ map_fields {
Subroutine __ANON__: not called CC=1 SCAR=6.9
345 0 push @{ $FIELDS{(tag)} }
346 , $_
347 0 };
348 0 $code->();
349 0 } $stream
350 }
351  
352 1 ▶sub cib_reader {
Subroutine cib_reader: called CC=1 SCAR=1.2
Pod undocumented
353 1 my $fmt = shift;
354 1 my @fields = map @$_, shift;
355  
356 0 ▶ sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
357 0 my %cib;
358 0 @cib{ @fields } = unpack $fmt, shift;
359 0 \%cib
360 }
361  
362 1 }
363  
364 1 ▶sub cib_writer {
Subroutine cib_writer: called CC=1 SCAR=0.6
Pod undocumented
365 1 my $fmt = shift;
366 1 my @fields = map @$_, shift;
367 0 ▶ sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
368 0 my $cib = shift;
369 0 pack $fmt, @$cib{@fields};
370 }
371 1 }
372  
373 1 ▶sub cib_keys {
Subroutine cib_keys: called CC=2 SCAR=6.9
Pod undocumented
374 1 my $cib = shift;
375 1 my $last = @$cib;
376 1 my ( @fmt, @fields );
377 1 ▶ for ( my $i = 0; $last > $i; ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($last > $i) 12 1 13
378 12 push @fmt , "A$$cib[$i++]";
379 12 push @fields, $$cib[$i++];
380 }
381  
382 1 ( (join '',@fmt)
383 , \@fields
384 );
385  
386 }
387  
388 1 ▶sub cib_handlers {
Subroutine cib_handlers: called CC=1 SCAR=0.0
Pod undocumented
389 1 my @spec = cib_keys shift;
390 1 ( cib_reader ( @spec )
391 , cib_writer ( @spec )
392 );
393 }
394  
395 1 our ($gdp_reader,$gpd_writer) = cib_handlers
396 [qw[
397 8 entered
398 1 date_type
399 4 pub
400 4 pub2
401 3 audience
402 1 gov
403 1 modif
404 3 lang
405 1 transliteration
406 4 charset
407 4 charset2
408 2 title ]];
409  
410 0 ▶sub record_charset (_) {
Subroutine record_charset: not called CC=1 SCAR=6.9
Pod undocumented
411 0 my $rec = shift;
412 ''. ( map_values
413 0 ▶ {$MARC::MIR::gdp_parser->( $_ )->{charset}}
Subroutine __ANON__: not called CC=1 SCAR=6.9
414 0 [qw< 100 a >], $rec
415 )[0]
416 }
417  
418 0 ▶sub append_subfields_to {
Subroutine append_subfields_to: not called CC=2 SCAR=17.9
Pod undocumented
419 0 my ($dest,@data) = @_;
420 0 ▶ @data or @data = @{$$_[1]};
Branch: 1 branch
BranchTrueFalseTotal
@data or @data = @{$_->[1];} 0 0 0
421 0 push @{ $$dest[1] }
422 0 , @{ $$_[1] };
423 }
424  
425 0 ▶sub indicators { $$_[2] }
Subroutine indicators: not called CC=1 SCAR=6.9
Pod documented
426  
427 1 1;
↑ Summary