Dpkg

 view release on metacpan or  search on metacpan

lib/Dpkg/Index.pm  view on Meta::CPAN

    }

    # Options set by the user override default values.
    $self->{$_} = $opts{$_} foreach keys %opts;
}

=item $index->get_type()

Returns the type of control information stored. See the type parameter
set during new().

=cut

sub get_type {
    my $self = shift;
    return $self->{type};
}

=item $index->add($item, [$key])

Add a new item in the index. If the $key parameter is omitted, the key
will be generated with the get_key_func function (see set_options() for
details).

=cut

sub add {
    my ($self, $item, $key) = @_;

    $key //= $self->{get_key_func}($item);
    if (not exists $self->{items}{$key}) {
        push @{$self->{order}}, $key;
    }
    $self->{items}{$key} = $item;
}

=item $index->parse($fh, $desc)

Reads the filehandle and creates all items parsed. When called multiple
times, the parsed stanzas are accumulated.

Returns the number of items parsed.

=cut

sub parse {
    my ($self, $fh, $desc) = @_;
    my $item = $self->new_item();
    my $i = 0;
    while ($item->parse($fh, $desc)) {
        $self->add($item);
        $item = $self->new_item();
        $i++;
    }
    return $i;
}

=item $index->load($file)

Reads the file and creates all items parsed. Returns the number of items
parsed. Handles compressed files transparently based on their extensions.

=item $item = $index->new_item()

Creates a new item. Mainly useful for derived objects that would want
to override this method to return something else than a L<Dpkg::Control>
object.

=cut

sub new_item {
    my $self = shift;

    return Dpkg::Control->new(
        %{$self->{item_opts}},
        type => $self->{type},
    );
}

=item $item = $index->get_by_key($key)

Returns the item identified by $key or undef.

=cut

sub get_by_key {
    my ($self, $key) = @_;
    return $self->{items}{$key} if exists $self->{items}{$key};
    return;
}

=item @keys = $index->get_keys(%criteria)

Returns the keys of items that matches all the criteria. The key of the
%criteria hash is a field name and the value is either a regex that needs
to match the field value, or a reference to a function that must return
true and that receives the field value as single parameter, or a scalar
that must be equal to the field value.

=cut

sub get_keys {
    my ($self, %crit) = @_;
    my @selected = @{$self->{order}};

    # Search criteria.
    foreach my $s_crit (keys %crit) {
        if (ref($crit{$s_crit}) eq 'Regexp') {
            @selected = grep {
                exists $self->{items}{$_}{$s_crit} and
                       $self->{items}{$_}{$s_crit} =~ $crit{$s_crit}
            } @selected;
        } elsif (ref($crit{$s_crit}) eq 'CODE') {
            @selected = grep {
                $crit{$s_crit}->($self->{items}{$_}{$s_crit});
            } @selected;
        } else {
            @selected = grep {
                exists $self->{items}{$_}{$s_crit} and
                       $self->{items}{$_}{$s_crit} eq $crit{$s_crit}
            } @selected;



( run in 0.554 second using v1.01-cache-2.11-cpan-39bf76dae61 )