This file is indexed.

/usr/share/perl5/Catmandu/Fix/lookup.pm is in libcatmandu-perl 0.9505-1.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
package Catmandu::Fix::lookup;

use Catmandu::Sane;

our $VERSION = '0.9505';

use Catmandu::Importer::CSV;
use Moo;
use namespace::clean;
use Catmandu::Fix::Has;

with 'Catmandu::Fix::Base';

has path       => (fix_arg => 1);
has file       => (fix_arg => 1);
has default    => (fix_opt => 1);
has delete     => (fix_opt => 1);
has csv_args   => (fix_opt => 'collect');
has dictionary => (is => 'lazy', init_arg => undef);

sub _build_dictionary {
    my ($self) = @_;
    Catmandu::Importer::CSV->new(
        %{$self->csv_args},
        file   => $self->file,
        header => 0,
        fields => ['key', 'val'],
    )->reduce({}, sub {
        my ($dict, $pair) = @_;
        $dict->{$pair->{key}} = $pair->{val};
        $dict;
    });
}

sub emit {
    my ($self, $fixer) = @_;
    my $path     = $fixer->split_path($self->path);
    my $key      = pop @$path;
    my $dict_var = $fixer->capture($self->dictionary);
    my $delete   = $self->delete;
    my $default  = $self->default;

    $fixer->emit_walk_path($fixer->var, $path, sub {
        my $var = shift;
        $fixer->emit_get_key($var, $key, sub {
            my $val_var = shift;
            my $val_index = shift;
            my $dict_val_var = $fixer->generate_var;
            my $perl = "if (is_value(${val_var}) && defined(my ${dict_val_var} = ${dict_var}->{${val_var}})) {" .
                "${val_var} = ${dict_val_var};" .
            "}";
            if ($delete) {
                $perl .= "else {";
                if (defined $val_index) { # wildcard: only delete the value where the lookup failed
                    $perl .= "splice(\@{${var}}, ${val_index}--, 1);";
                } else {
                    $perl .= $fixer->emit_delete_key($var, $key);
                }
                $perl .= "}";
            } elsif (defined $default) {
                $perl .= "else {" .
                    "${val_var} = " . $fixer->emit_value($default) . ";" .
                "}";
            }
            $perl;
        });
    });
}

1;

__END__

=pod

=head1 NAME

Catmandu::Fix::lookup - change the value of a HASH key or ARRAY index by
looking up it's value in a dictionary

=head1 SYNOPSIS

   lookup(foo.bar, dictionary.csv)
   lookup(foo.bar, dictionary.csv, sep_char: |)
   # delete value if the lookup fails:
   lookup(foo.bar, dictionary.csv, delete: 1)
   # use a default value if the lookup fails:
   lookup(foo.bar, dictionary.csv, default: 'default value')

=head1 SEE ALSO

L<Catmandu::Fix>

=cut