line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Catmandu::Fix::pica_map; |
2
|
|
|
|
|
|
|
|
3
|
|
|
|
|
|
|
our $VERSION = '0.15'; |
4
|
|
|
|
|
|
|
|
5
|
1
|
|
|
1
|
|
738
|
use Catmandu::Sane; |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
5
|
|
6
|
1
|
|
|
1
|
|
211
|
use Moo; |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
8
|
|
7
|
|
|
|
|
|
|
|
8
|
1
|
|
|
1
|
|
917
|
use Catmandu::Fix::Has; |
|
1
|
|
|
|
|
1119
|
|
|
1
|
|
|
|
|
5
|
|
9
|
1
|
|
|
1
|
|
831
|
use PICA::Path; |
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
10
|
|
|
|
|
|
|
|
11
|
|
|
|
|
|
|
has pica_path => ( fix_arg => 1 ); |
12
|
|
|
|
|
|
|
has path => ( fix_arg => 1 ); |
13
|
|
|
|
|
|
|
has record => ( fix_opt => 1 ); |
14
|
|
|
|
|
|
|
has split => ( fix_opt => 1 ); |
15
|
|
|
|
|
|
|
has join => ( fix_opt => 1 ); |
16
|
|
|
|
|
|
|
has value => ( fix_opt => 1 ); |
17
|
|
|
|
|
|
|
|
18
|
|
|
|
|
|
|
sub emit { |
19
|
|
|
|
|
|
|
my ( $self, $fixer ) = @_; |
20
|
|
|
|
|
|
|
my $path = $fixer->split_path( $self->path ); |
21
|
|
|
|
|
|
|
my $record_key = $fixer->emit_string( $self->record // 'record' ); |
22
|
|
|
|
|
|
|
my $join_char = $fixer->emit_string( $self->join // '' ); |
23
|
|
|
|
|
|
|
my $pica_path = PICA::Path->new($self->pica_path); |
24
|
|
|
|
|
|
|
|
25
|
|
|
|
|
|
|
my ($field_regex, $occurrence_regex, $subfield_regex, $from, $length) = @$pica_path; |
26
|
|
|
|
|
|
|
|
27
|
|
|
|
|
|
|
my $var = $fixer->var; |
28
|
|
|
|
|
|
|
my $vals = $fixer->generate_var; |
29
|
|
|
|
|
|
|
my $perl = $fixer->emit_declare_vars( $vals, '[]' ); |
30
|
|
|
|
|
|
|
|
31
|
|
|
|
|
|
|
my $field_regex_var = $fixer->generate_var; |
32
|
|
|
|
|
|
|
$perl .= $fixer->emit_declare_vars( $field_regex_var, "qr{$field_regex}" ); |
33
|
|
|
|
|
|
|
|
34
|
|
|
|
|
|
|
my $subfield_regex_var = $fixer->generate_var; |
35
|
|
|
|
|
|
|
$perl .= $fixer->emit_declare_vars( $subfield_regex_var, "qr{$subfield_regex}" ); |
36
|
|
|
|
|
|
|
|
37
|
|
|
|
|
|
|
my $occurrence_regex_var; |
38
|
|
|
|
|
|
|
if (defined $occurrence_regex) { |
39
|
|
|
|
|
|
|
$occurrence_regex_var = $fixer->generate_var; |
40
|
|
|
|
|
|
|
$perl .= $fixer->emit_declare_vars( $occurrence_regex_var, "qr{$occurrence_regex}" ); |
41
|
|
|
|
|
|
|
} |
42
|
|
|
|
|
|
|
|
43
|
|
|
|
|
|
|
$perl .= $fixer->emit_foreach( |
44
|
|
|
|
|
|
|
"${var}->{${record_key}}", |
45
|
|
|
|
|
|
|
sub { |
46
|
|
|
|
|
|
|
my $var = shift; |
47
|
|
|
|
|
|
|
my $v = $fixer->generate_var; |
48
|
|
|
|
|
|
|
my $perl = ""; |
49
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
|
$perl .= "next if ${var}->[0] !~ ${field_regex_var};"; |
51
|
|
|
|
|
|
|
|
52
|
|
|
|
|
|
|
if (defined $occurrence_regex) { |
53
|
|
|
|
|
|
|
$perl .= "next if (!defined ${var}->[1] || ${var}->[1] !~ ${occurrence_regex_var});"; |
54
|
|
|
|
|
|
|
} |
55
|
|
|
|
|
|
|
|
56
|
|
|
|
|
|
|
if ( $self->value ) { |
57
|
|
|
|
|
|
|
$perl .= $fixer->emit_declare_vars( $v, |
58
|
|
|
|
|
|
|
$fixer->emit_string( $self->value ) ); |
59
|
|
|
|
|
|
|
} |
60
|
|
|
|
|
|
|
else { |
61
|
|
|
|
|
|
|
my $i = $fixer->generate_var; |
62
|
|
|
|
|
|
|
my $add_subfields = sub { |
63
|
|
|
|
|
|
|
my $start = shift; |
64
|
|
|
|
|
|
|
"for (my ${i} = ${start}; ${i} < \@{${var}}; ${i} += 2) {" |
65
|
|
|
|
|
|
|
. "if (${var}->[${i}] =~ ${subfield_regex_var}) {" |
66
|
|
|
|
|
|
|
. "push(\@{${v}}, ${var}->[${i} + 1]);" . "}" . "}"; |
67
|
|
|
|
|
|
|
}; |
68
|
|
|
|
|
|
|
$perl .= $fixer->emit_declare_vars( $v, "[]" ); |
69
|
|
|
|
|
|
|
$perl .= $add_subfields->(2); |
70
|
|
|
|
|
|
|
$perl .= "if (\@{${v}}) {"; |
71
|
|
|
|
|
|
|
if ( !$self->split ) { |
72
|
|
|
|
|
|
|
$perl .= "${v} = join(${join_char}, \@{${v}});"; |
73
|
|
|
|
|
|
|
if ( defined( my $off = $from ) ) { |
74
|
|
|
|
|
|
|
$perl .= "if (eval { ${v} = substr(${v}, ${off}, ${length}); 1 }) {"; |
75
|
|
|
|
|
|
|
} |
76
|
|
|
|
|
|
|
} |
77
|
|
|
|
|
|
|
$perl .= $fixer->emit_create_path( |
78
|
|
|
|
|
|
|
$fixer->var, |
79
|
|
|
|
|
|
|
$path, |
80
|
|
|
|
|
|
|
sub { |
81
|
|
|
|
|
|
|
my $var = shift; |
82
|
|
|
|
|
|
|
if ( $self->split ) { |
83
|
|
|
|
|
|
|
"if (is_array_ref(${var})) {" |
84
|
|
|
|
|
|
|
. "push \@{${var}}, ${v};" |
85
|
|
|
|
|
|
|
. "} else {" |
86
|
|
|
|
|
|
|
. "${var} = [${v}];" . "}"; |
87
|
|
|
|
|
|
|
} |
88
|
|
|
|
|
|
|
else { |
89
|
|
|
|
|
|
|
"if (is_string(${var})) {" |
90
|
|
|
|
|
|
|
. "${var} = join(${join_char}, ${var}, ${v});" |
91
|
|
|
|
|
|
|
. "} else {" |
92
|
|
|
|
|
|
|
. "${var} = ${v};" . "}"; |
93
|
|
|
|
|
|
|
} |
94
|
|
|
|
|
|
|
} |
95
|
|
|
|
|
|
|
); |
96
|
|
|
|
|
|
|
if ( defined($from) ) { |
97
|
|
|
|
|
|
|
$perl .= "}"; |
98
|
|
|
|
|
|
|
} |
99
|
|
|
|
|
|
|
$perl .= "}"; |
100
|
|
|
|
|
|
|
} |
101
|
|
|
|
|
|
|
$perl; |
102
|
|
|
|
|
|
|
} |
103
|
|
|
|
|
|
|
); |
104
|
|
|
|
|
|
|
|
105
|
|
|
|
|
|
|
$perl; |
106
|
|
|
|
|
|
|
} |
107
|
|
|
|
|
|
|
|
108
|
|
|
|
|
|
|
1; |
109
|
|
|
|
|
|
|
__END__ |