Parse.pm

filter: Statementstmt 66.6% 4 / 6 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 2 / 2 Podpod n/a totaltotal 75.0% 6 / 8 SCAR 0.4
CC
1
Cov
67%
BEGIN
1.3
BEGIN
1.0
CRAP 1.0
« Parse/Status.pm ↑ Summary Parse/Available.pm »
1 #
2 # DPKG::Parse.pm
3 # Created by: Adam Jacob, <holoway@cpan.org>
4 # Created on: 12/19/2005 02:21:03 PM PST
5 #
6 # $Id: $
7  
8 =head1 NAME
9  
10 DPKG::Parse - Parse various dpkg files into Perl Objects
11  
12 =head1 SYNOPSIS
13
14 use DPKG::Parse::Status;
15 my $status = DPKG::Parse::Status->new;
16 while (my $entry = $status->next_package) {
17 print $entry->package . " " . $entry->version . "\n";
18 }
19  
20 use DPKG::Parse::Available;
21 my $available = DPKG::Parse::Available->new;
22 while (my $entry = $available->next_package) {
23 print $entry->package . " " . $entry->version . "\n";
24 }
25  
26 =head1 DESCRIPTION
27  
28 DPKG::Parse contains utilities to parse the various files created by
29 dpkg and turn them into helpful Perl objects. Current files understood
30 by various DPKG::Parse modules:
31  
32 /var/lib/dpkg/status - DPKG::Parse::Status
33 /var/lib/dpkg/available - DPKG::Parse::Available
34 Packages.gz - DPKG::Parse::Packages
35  
36 See each module's documentation for particulars - You should not be calling
37 DPKG::Parse directly.
38  
39 =head1 METHODS
40  
41 =over 4
42  
43 =cut
44  
45 package DPKG::Parse;
46  
47 2 ▶use Params::Validate qw(:all);
Subroutine BEGIN: called CC=1 SCAR=0.0
48 2 ▶use DPKG::Parse::Entry;
Subroutine BEGIN: called CC=1 SCAR=2.6
49 use Class::C3;
50 use base qw(Class::Accessor);
51  
52 use strict;
53 use warnings;
54  
55 DPKG::Parse->mk_accessors(qw(filename entryarray entryhash));
56  
57 my
58 $VERSION = "0.01";
59  
60 =item filename($filename)
61  
62 A simple accessor for the file currently being parsed.
63  
64 =item entryarray
65  
66 Access to the raw array of entries in a given file.
67  
68 =item entryhash
69  
70 Access to the raw hash of entries. The key is determined by the module,
71 but is usually the Package name.
72  
73 =item new('filename' => '/var/lib/dpkg/status');
74  
75 A generic new function; takes a filename and calls the filename() accessor
76 with it. Should not be called directly, but through on of the children of
77 this package.
78  
79 =cut
80 sub new {
81 my $pkg = shift;
82 my %p = validate(@_,
83 {
84 'filename' => { 'type' => SCALAR, },
85 }
86 );
87 my $ref = {};
88 if ($p{'filename'}) {
89 $ref->{'filename'} = $p{'filename'};
90 };
91 $ref->{'entryarray'} = [];
92 $ref->{'entryhash'} = {};
93 bless($ref, $pkg);
94 return $ref;
95 }
96  
97 =item parse
98  
99 A default parse function; simply calls parse_package_format.
100  
101 =cut
102 sub parse {
103 my $pkg = shift;
104 $pkg->parse_package_format;
105 }
106  
107 =item parse_package_format
108  
109 Takes a file in a format similar to the dpkg "available" file, and creates
110 L<DPKG::Parse::Entry> objects from each entry.
111  
112 =cut
113 sub parse_package_format {
114 my $pkg = shift;
115 if (! -f $pkg->filename) {
116 die "Cannot find " . $pkg->filename . ", or it's not a file at all!";
117 }
118 open(STATUS, $pkg->filename);
119 my $entry;
120 STATUSLINE: while (my $line = <STATUS>) {
121 if ($line =~ /^\n$/) {
122 my $dpkg_entry = DPKG::Parse::Entry->new('data' => $entry);
123 push(@{$pkg->{'entryarray'}}, $dpkg_entry);
124 $pkg->{'entryhash'}->{$dpkg_entry->package} = $dpkg_entry;
125 $entry = undef;
126 next STATUSLINE;
127 }
128 $entry = $entry . $line;
129 }
130 close(STATUS);
131 }
132  
133 =item get_package('name' => 'postfix', 'hash' => 'entryhash');
134  
135 The value of a hash, if it exists. By default, it uses the value returned
136 by the "entryhash" accessor, but that can be overridden with the "hash"
137 parameter. Usually returns a L<DPKG::Parse::Entry> object.
138  
139 =cut
140 sub get_package {
141 my $pkg = shift;
142 my %p = validate( @_,
143 {
144 'name' => { 'type' => SCALAR, },
145 'hash' => { 'type' => SCALAR, 'default' => 'entryhash', },
146 },
147 );
148 if (exists($pkg->{$p{'hash'}}->{$p{'name'}})) {
149 return $pkg->{$p{'hash'}}->{$p{'name'}};
150 } else {
151 return undef;
152 }
153 }
154  
155 =item next_package
156  
157 Shifts the next value off the array stored in the entryarray() accessor.
158 If you want to access the raw values, do not use this function! It shifts!
159  
160 =cut
161 sub next_package {
162 my $pkg = shift;
163 return shift(@{$pkg->{'entryarray'}});
164 }
165  
166 1;
167  
168 __END__
« Parse/Status.pm ↑ Summary Parse/Available.pm »