This file is indexed.

/usr/share/perl5/Catmandu/Fix/vacuum.pm is in libcatmandu-perl 1.0700-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
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
package Catmandu::Fix::vacuum;

use Catmandu::Sane;

our $VERSION = '1.07';

use Catmandu::Util qw(is_value is_hash_ref is_array_ref);
use Scalar::Util qw(refaddr);
use Moo;
use namespace::clean;

with 'Catmandu::Fix::Inlineable';

sub _visit {
    my ($self, $v) = @_;
    (is_hash_ref($v) && %$v) || (is_array_ref($v) && @$v);
}

sub _empty {
    my ($self, $v) = @_;
    !defined($v)
        || (is_value($v)     && $v !~ /\S/)
        || (is_hash_ref($v)  && !%$v)
        || (is_array_ref($v) && !@$v);
}

sub fix {
    my ($self, $data) = @_;

    return $data unless $self->_visit($data);

    my @stack = ($data);
    my %seen;

    while (@stack) {
        my $d  = pop @stack;
        my $id = refaddr($d);

        if ($seen{$id}) {
            if (is_hash_ref($d)) {
                for my $k (keys %$d) {
                    delete $d->{$k} if $self->_empty($d->{$k});
                }
            }
            elsif (is_array_ref($d)) {
                my @vals = grep {!$self->_empty($_)} @$d;
                splice(@$d, 0, @$d, @vals);
            }
        }
        else {
            $seen{$id} = 1;
            push @stack, $d;

            if (is_hash_ref($d)) {
                for my $k (keys %$d) {
                    my $v = $d->{$k};
                    if ($self->_empty($v)) {
                        delete $d->{$k};
                    }
                    elsif ($self->_visit($v)) {
                        push @stack, $v;
                    }
                }
            }
            elsif (is_array_ref($d)) {
                my @vals;
                for my $v (@$d) {
                    next if $self->_empty($v);
                    push @vals, $v;
                    push @stack, $v if $self->_visit($v);
                }
                splice @$d, 0, @$d, @vals;
            }
        }
    }

    $data;
}

1;

__END__

=pod

=head1 NAME

Catmandu::Fix::vacuum - delete all empty fields from your data

=head1 SYNOPSIS

   # Delete all the empty fields
   #
   # input:
   #
   # foo: ''
   # bar: []
   # relations: {}
   # test: 123
   #
   vacuum()
   
   # output:
   #
   # test: 123
   #

=head1 SEE ALSO

L<Catmandu::Fix>

=cut