line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
|
2
|
|
|
|
|
|
|
use Catmandu::Sane; |
3
|
1
|
|
|
1
|
|
92384
|
|
|
1
|
|
|
|
|
3
|
|
|
1
|
|
|
|
|
6
|
|
4
|
|
|
|
|
|
|
our $VERSION = '1.2018'; |
5
|
|
|
|
|
|
|
|
6
|
|
|
|
|
|
|
use Catmandu::Importer::CSV; |
7
|
1
|
|
|
1
|
|
542
|
use Catmandu::Util::Path qw(as_path); |
|
1
|
|
|
|
|
4
|
|
|
1
|
|
|
|
|
42
|
|
8
|
1
|
|
|
1
|
|
8
|
use Catmandu::Util qw(is_value); |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
59
|
|
9
|
1
|
|
|
1
|
|
4
|
use Moo; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
38
|
|
10
|
1
|
|
|
1
|
|
5
|
use namespace::clean; |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
6
|
|
11
|
1
|
|
|
1
|
|
354
|
use Catmandu::Fix::Has; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
6
|
|
12
|
1
|
|
|
1
|
|
887
|
|
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
7
|
|
13
|
|
|
|
|
|
|
with 'Catmandu::Fix::Builder'; |
14
|
|
|
|
|
|
|
|
15
|
|
|
|
|
|
|
has path => (fix_arg => 1); |
16
|
|
|
|
|
|
|
has file => (fix_arg => 1); |
17
|
|
|
|
|
|
|
has default => (fix_opt => 1, predicate => 1); |
18
|
|
|
|
|
|
|
has delete => (fix_opt => 1); |
19
|
|
|
|
|
|
|
has csv_args => (fix_opt => 'collect'); |
20
|
|
|
|
|
|
|
has dictionary => (is => 'lazy', init_arg => undef); |
21
|
|
|
|
|
|
|
|
22
|
|
|
|
|
|
|
my ($self) = @_; |
23
|
|
|
|
|
|
|
Catmandu::Importer::CSV->new( |
24
|
7
|
|
|
7
|
|
66
|
%{$self->csv_args}, |
25
|
|
|
|
|
|
|
file => $self->file, |
26
|
7
|
|
|
|
|
147
|
header => 0, |
27
|
|
|
|
|
|
|
fields => ['key', 'val'], |
28
|
|
|
|
|
|
|
)->reduce( |
29
|
|
|
|
|
|
|
{}, |
30
|
|
|
|
|
|
|
sub { |
31
|
|
|
|
|
|
|
my ($dict, $pair) = @_; |
32
|
|
|
|
|
|
|
$dict->{$pair->{key}} = $pair->{val}; |
33
|
36
|
|
|
36
|
|
61
|
$dict; |
34
|
36
|
|
|
|
|
86
|
} |
35
|
36
|
|
|
|
|
77
|
); |
36
|
|
|
|
|
|
|
} |
37
|
7
|
|
|
|
|
18
|
|
38
|
|
|
|
|
|
|
my ($self) = @_; |
39
|
|
|
|
|
|
|
my $path = as_path($self->path); |
40
|
|
|
|
|
|
|
my $dict = $self->dictionary; |
41
|
7
|
|
|
7
|
|
88
|
my $has_default = $self->has_default; |
42
|
7
|
|
|
|
|
39
|
my $default = $self->default; |
43
|
7
|
|
|
|
|
1479
|
my $delete = $self->delete; |
44
|
7
|
|
|
|
|
35
|
$path->updater( |
45
|
7
|
|
|
|
|
18
|
sub { |
46
|
7
|
|
|
|
|
17
|
my $val = $_[0]; |
47
|
|
|
|
|
|
|
if (is_value($val) && defined(my $new_val = $dict->{$val})) { |
48
|
|
|
|
|
|
|
return $new_val; |
49
|
9
|
|
|
9
|
|
16
|
} |
50
|
9
|
100
|
66
|
|
|
51
|
elsif ($delete) { |
|
|
100
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
51
|
4
|
|
|
|
|
68
|
return undef, 1, 1; |
52
|
|
|
|
|
|
|
} |
53
|
|
|
|
|
|
|
elsif ($has_default) { |
54
|
2
|
|
|
|
|
31
|
return $default; |
55
|
|
|
|
|
|
|
} |
56
|
|
|
|
|
|
|
return undef, 1, 0; |
57
|
2
|
|
|
|
|
30
|
} |
58
|
|
|
|
|
|
|
); |
59
|
1
|
|
|
|
|
17
|
} |
60
|
|
|
|
|
|
|
|
61
|
7
|
|
|
|
|
195
|
1; |
62
|
|
|
|
|
|
|
|
63
|
|
|
|
|
|
|
|
64
|
|
|
|
|
|
|
=pod |
65
|
|
|
|
|
|
|
|
66
|
|
|
|
|
|
|
=head1 NAME |
67
|
|
|
|
|
|
|
|
68
|
|
|
|
|
|
|
Catmandu::Fix::lookup - change the value of a HASH key or ARRAY index by |
69
|
|
|
|
|
|
|
looking up its value in a dictionary |
70
|
|
|
|
|
|
|
|
71
|
|
|
|
|
|
|
=head1 SYNOPSIS |
72
|
|
|
|
|
|
|
|
73
|
|
|
|
|
|
|
# dictionary.csv |
74
|
|
|
|
|
|
|
# id,planet |
75
|
|
|
|
|
|
|
# 1,sun |
76
|
|
|
|
|
|
|
# 2,earth |
77
|
|
|
|
|
|
|
# 3,moon |
78
|
|
|
|
|
|
|
|
79
|
|
|
|
|
|
|
# values found in the dictionary.csv will be replaced |
80
|
|
|
|
|
|
|
# {foo => {bar => 2}} |
81
|
|
|
|
|
|
|
lookup(foo.bar, dictionary.csv) |
82
|
|
|
|
|
|
|
# {foo => {bar => 'earth'}} |
83
|
|
|
|
|
|
|
|
84
|
|
|
|
|
|
|
# values not found will be kept |
85
|
|
|
|
|
|
|
# {foo => {bar => 232}} |
86
|
|
|
|
|
|
|
lookup(foo.bar, dictionary.csv) |
87
|
|
|
|
|
|
|
# {foo => {bar => 232}} |
88
|
|
|
|
|
|
|
|
89
|
|
|
|
|
|
|
# in case you have a different seperator |
90
|
|
|
|
|
|
|
lookup(foo.bar, dictionary.csv, sep_char: |) |
91
|
|
|
|
|
|
|
|
92
|
|
|
|
|
|
|
# delete value if the lookup fails: |
93
|
|
|
|
|
|
|
lookup(foo.bar, dictionary.csv, delete: 1) |
94
|
|
|
|
|
|
|
|
95
|
|
|
|
|
|
|
# use a default value if the lookup fails: |
96
|
|
|
|
|
|
|
lookup(foo.bar, dictionary.csv, default: 'default value') |
97
|
|
|
|
|
|
|
|
98
|
|
|
|
|
|
|
=head1 SEE ALSO |
99
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
L<Catmandu::Fix>, L<Catmandu::Fix::mapping> |
101
|
|
|
|
|
|
|
|
102
|
|
|
|
|
|
|
=cut |