File: Parse.pm

package info (click to toggle)
libdpkg-parse-perl 0.03-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, sid
  • size: 320 kB
  • ctags: 26
  • sloc: perl: 497; makefile: 2
file content (184 lines) | stat: -rw-r--r-- 4,487 bytes parent folder | download
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
=head1 NAME

DPKG::Parse - Parse various dpkg files into Perl Objects

=head1 SYNOPSIS

    use DPKG::Parse::Status;
    my $status = DPKG::Parse::Status->new;
    while (my $entry = $status->next_package) {
        print $entry->package . " " . $entry->version . "\n";
    }

    use DPKG::Parse::Available;
    my $available = DPKG::Parse::Available->new;
    while (my $entry = $available->next_package) {
        print $entry->package . " " . $entry->version . "\n";
    }

=head1 DESCRIPTION

DPKG::Parse contains utilities to parse the various files created by
dpkg and turn them into helpful Perl objects.  Current files understood
by various DPKG::Parse modules:

  /var/lib/dpkg/status    - DPKG::Parse::Status
  /var/lib/dpkg/available - DPKG::Parse::Available
  Packages.gz             - DPKG::Parse::Packages

See each module's documentation for particulars - You should not be calling
DPKG::Parse directly.

=head1 METHODS

=over 4

=cut

package DPKG::Parse; # git description: v0.02-2-gca4c3e9

use Params::Validate qw(:all);
use DPKG::Parse::Entry;
use Class::C3;
use base qw(Class::Accessor);

use strict;
use warnings;

DPKG::Parse->mk_accessors(qw(filename entryarray entryhash));
DPKG::Parse->mk_ro_accessors('debug');

our $VERSION = '0.03';

=item filename($filename)

A simple accessor for the file currently being parsed.

=item entryarray

Access to the raw array of entries in a given file.

=item entryhash

Access to the raw hash of entries.  The key is determined by the module,
but is usually the Package name.

=item new('filename' => '/var/lib/dpkg/status');

A generic new function; takes a filename and calls the filename() accessor
with it.  Should not be called directly, but through on of the children of
this package.

=cut
sub new {
    my $pkg = shift;
    my %p = validate(@_,
        {
            'filename' => { 'type' => SCALAR, },
            'debug' => { 'type' => SCALAR, 'default' => 0, 'optional' => 1 }
        }
    );
    my $ref = {};
    if ($p{'filename'}) {
        $ref->{'filename'} = $p{'filename'};
    };
    $ref->{debug} = $p{debug};
    $ref->{'entryarray'} = [];
    $ref->{'entryhash'} = {};
    bless($ref, $pkg);
    return $ref;
}

=item parse

A default parse function; simply calls parse_package_format.

=cut
sub parse {
    my $pkg = shift;
    $pkg->parse_package_format;
}

=item parse_package_format

Takes a file in a format similar to the dpkg "available" file, and creates
L<DPKG::Parse::Entry> objects from each entry.

=cut
sub parse_package_format {
    my $pkg = shift;
    if (! -f $pkg->filename) {
        die "Cannot find " . $pkg->filename . ", or it's not a file at all!";
    }
    open(STATUS, $pkg->filename);
    my $entry = '';
    my $line_num = -1;
    my $entry_line = 0;
    STATUSLINE: while (my $line = <STATUS>) {
        ++$line_num;
        if ($line =~ /^\n$/) {
            my $dpkg_entry = DPKG::Parse::Entry->new('data' => $entry, debug => $pkg->debug, line_num => $entry_line);
            push(@{$pkg->{'entryarray'}}, $dpkg_entry);
            $pkg->{'entryhash'}->{$dpkg_entry->package} = $dpkg_entry;
            $entry = '';
            $entry_line = $line_num + 1;
            next STATUSLINE;
        }
        $entry = $entry . $line;
    }
    close(STATUS);
}

=item get_package('name' => 'postfix', 'hash' => 'entryhash');

The value of a hash, if it exists.  By default, it uses the value returned
by the "entryhash" accessor, but that can be overridden with the "hash"
parameter.  Usually returns a L<DPKG::Parse::Entry> object.

=cut
sub get_package {
    my $pkg = shift;
    my %p = validate( @_,
        {
            'name' => { 'type' => SCALAR, },
            'hash' => { 'type' => SCALAR, 'default' => 'entryhash', },
        },
    );
    if (exists($pkg->{$p{'hash'}}->{$p{'name'}})) {
        return $pkg->{$p{'hash'}}->{$p{'name'}};
    } else {
        return undef;
    }
}

=item next_package

Shifts the next value off the array stored in the entryarray() accessor.
If you want to access the raw values, do not use this function!  It shifts!

=cut
sub next_package {
    my $pkg = shift;
    return shift(@{$pkg->{'entryarray'}});
}

1;

__END__
=back

=head1 SEE ALSO

L<DPKG::Parse::Status>, L<DPKG::Parse::Available>, L<DPKG::Parse::Packages>,
L<DPKG::Parse::Entry>

=head1 AUTHOR

Adam Jacob, C<holoway@cpan.org>

=head1 LICENSE

This library is free software. You can redistribute it and/or modify it under
the same terms as perl itself.

=cut