PackageDetails/Header.pm

filter: Statementstmt 94.1% 64 / 68 Branchbran 78.5% 11 / 14 Conditioncond 83.3% 5 / 6 MC/DCmcdc 50.0% 2 / 4 Subroutinesub 88.8% 16 / 18 Podpod 88.8% 8 / 9 totaltotal 89.0% 106 / 119 CC 12 SCAR 9.8
CC
12
Cov
91%
_external_name_to_internal_name
6.0
can
5.0
as_string
4.0
CRAP 2.7
« PackageDetails/Entries.pm ↑ Summary PackageDetails/Entry.pm »
1 15 ▶use 5.008;
Subroutine BEGIN: called CC=1 SCAR=0.0
2  
3 package CPAN::PackageDetails::Header;
4 15 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 15 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 15 our $VERSION = '0.263';
8  
9 15 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 =encoding utf8
12  
13 =head1 NAME
14  
15 CPAN::PackageDetails::Header - Handle the header of 02packages.details.txt.gz
16  
17 =head1 SYNOPSIS
18  
19 Used internally by CPAN::PackageDetails
20  
21 =head1 DESCRIPTION
22  
23 The 02packages.details.txt.gz header is a short preamble that give information
24 about the creation of the file, its intended use, and the number of entries in
25 the file. It looks something like:
26  
27 File: 02packages.details.txt
28 URL: http://www.perl.com/CPAN/modules/02packages.details.txt
29 Description: Package names found in directory $CPAN/authors/id/
30 Columns: package name, version, path
31 Intended-For: Automated fetch routines, namespace documentation.
32 Written-By: Id: mldistwatch.pm 1063 2008-09-23 05:23:57Z k
33 Line-Count: 59754
34 Last-Updated: Thu, 23 Oct 2008 02:27:36 GMT
35  
36 Note that there is a Columns field. This module tries to respect the ordering
37 of columns in there. The usual CPAN tools expect only three columns and in the
38 order in this example, but C<CPAN::PackageDetails> tries to handle any number
39 of columns in any order.
40  
41 =head2 Methods
42  
43 =over 4
44  
45 =item new( HASH )
46  
47 Create a new Header object. Unless you want a lot of work so you
48 get more control, just let C<CPAN::PackageDetails>'s C<new> or C<read>
49 handle this for you.
50  
51 In most cases, you'll want to create the Entries object first then
52 pass a reference the the Entries object to C<new> since the header
53 object needs to know how to get the count of the number of entries
54 so it can put it in the "Line-Count" header.
55  
56 CPAN::PackageDetails::Header->new(
57 _entries => $entries_object,
58 )
59  
60 =cut
61  
62 24 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
Pod documented
63 24 my( $class, %args ) = @_;
64  
65 24 my %hash = (
66 _entries => undef,
67 %args
68 );
69  
70 24 bless \%hash, $_[0]
71 }
72  
73 =item format_date
74  
75 Write the date in PAUSE format. For example:
76  
77 Thu, 23 Oct 2008 02:27:36 GMT
78  
79 =cut
80  
81 25 ▶sub format_date {
Subroutine format_date: called CC=1 SCAR=0.0
Pod documented
82 25 my( $second, $minute, $hour, $date, $monnum, $year, $wday ) = gmtime;
83 25 $year += 1900;
84  
85 25 my $day = ( qw(Sun Mon Tue Wed Thu Fri Sat) )[$wday];
86 25 my $month = ( qw(Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec) )[$monnum];
87  
88 25 sprintf "%s, %02d %s %4d %02d:%02d:%02d GMT",
89 $day, $date, $month, $year, $hour, $minute, $second;
90 }
91  
92 =item default_headers
93  
94 Returns a list of the the headers that should show up in the file. This
95 excludes various fake headers stored in the object.
96  
97 =cut
98  
99 1 ▶sub default_headers {
Subroutine default_headers: called CC=1 SCAR=0.0
Pod documented
100 7 map { $_, $_[0]->{$_} }
101 1 grep ! /^_|_class|allow/, keys %{ $_[0] }
102 }
103  
104 15 ▶sub can {
Subroutine can: called CC=5 SCAR=16.1
Pod undocumented
105 15 my( $self, @methods ) = @_;
106  
107 15 ▶ my $class = ref $self || $self; # class or instance
Condition66%2/3
ref $self || $self
l!l&&r!l&&!r
1410
MC/DC0%0/2
ref $self || $self
ref $self $self
Truth table66%2/3
A || B
A= ref $selfB= $self
ABresult
1X1
011
000
108  
109 15 foreach my $method ( @methods ) {
110 next if
111 15 ▶ defined &{"${class}::$method"} ||
Branch: 1 branch
BranchTrueFalseTotal
if defined &{"${class}::$method";} or $self->header_exists($method) 13 2 15
Condition100%3/3
defined &{"${class}::$method";} or $self->header_exists($method)
l!l&&r!l&&!r
582
MC/DC100%2/2
defined &{"${class}::$method";} or $self->header_exists($method)
defined &{"${class}::$method";} $self->header_exists($method)
Truth table100%3/3
A or B
A= defined &{"${class}::$method";}B= $self->header_exists($method)
ABresult
1X1
011
000
112 $self->header_exists( $method );
113 2 return 0;
114 }
115  
116 13 return 1;
117 }
118  
119 =item set_header
120  
121 Add an entry to the collection. Call this on the C<CPAN::PackageDetails>
122 object and it will take care of finding the right handler.
123  
124 =cut
125  
126 283 ▶sub set_header {
Subroutine set_header: called CC=1 SCAR=0.0
Pod documented
127 283 my( $self, $field, $value ) = @_;
128  
129 283 $self->{$field} = $value;
130 }
131  
132 =item header_exists( FIELD )
133  
134 Returns true if the header has a field named FIELD, regardless of
135 its value.
136  
137 =cut
138  
139 117 ▶sub header_exists {
Subroutine header_exists: called CC=1 SCAR=0.0
Pod documented
140 117 my( $self, $field ) = @_;
141  
142 117 exists $self->{$field}
143 }
144  
145 =item get_header( FIELD )
146  
147 Returns the value for the named header FIELD. Carps and returns nothing
148 if the named header is not in the object. This method is available from
149 the C<CPAN::PackageDetails> or C<CPAN::PackageDetails::Header> object:
150  
151 $package_details->get_header( 'url' );
152  
153 $package_details->header->get_header( 'url' );
154  
155 The header names in the Perl code are in a different format than they
156 are in the file. See C<default_headers> for an explanation of the
157 difference.
158  
159 For most headers, you can also use the header name as the method name:
160  
161 $package_details->header->url;
162  
163 =cut
164  
165 71 ▶sub get_header {
Subroutine get_header: called CC=2 SCAR=6.9
Pod documented
166 71 my( $self, $field ) = @_;
167  
168 71 ▶ if( $self->header_exists( $field ) ) { $self->{$field} }
Branch: 1 branch
BranchTrueFalseTotal
if ($self->header_exists($field)) { } 69 2 71
169 2 else { carp "No such header as $field!"; return }
170 }
171  
172 =item columns_as_list
173  
174 Returns the columns name as a list (rather than a comma-joined string). The
175 list is in the order of the columns in the output.
176  
177 =cut
178  
179 5 ▶sub columns_as_list { split /,\s+/, $_[0]->{columns} }
Subroutine columns_as_list: called CC=1 SCAR=0.0
Pod documented
180  
181 =item as_string
182  
183 Return the header formatted as a string.
184  
185 =cut
186  
187 15 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
188 15 my %internal_field_name_mapping = (
189 url => 'URL',
190 );
191  
192 15 my %external_field_name_mapping = reverse %internal_field_name_mapping;
193  
194 41 ▶sub _internal_name_to_external_name {
Subroutine _internal_name_to_external_name: called CC=2 SCAR=6.9
195 41 my( $self, $internal ) = @_;
196  
197 return $internal_field_name_mapping{$internal}
198 41 ▶ if exists $internal_field_name_mapping{$internal};
Branch: 1 branch
BranchTrueFalseTotal
if exists $internal_field_name_mapping{$internal} 4 37 41
199  
200 37 (my $external = $internal) =~ s/_/-/g;
201 37 $external =~ s/^(.)/ uc $1 /eg;
202 37 $external =~ s/-(.)/ "-" . uc $1 /eg;
203  
204 37 return $external;
205 }
206  
207 0 ▶sub _external_name_to_internal_name {
Subroutine _external_name_to_internal_name: not called CC=2 SCAR=17.9
208 0 my( $self, $external ) = @_;
209  
210 return $external_field_name_mapping{$external}
211 0 ▶ if exists $external_field_name_mapping{$external};
Branch: 1 branch
BranchTrueFalseTotal
if exists $external_field_name_mapping{$external} 0 0 0
212  
213 0 (my $internal = $external) =~ s/-/_/g;
214  
215 0 lc $internal;
216 }
217  
218 4 ▶sub as_string {
Subroutine as_string: called CC=4 SCAR=13.9
Pod documented
219 4 my( $self, $line_count ) = @_;
220  
221 # XXX: need entry count
222 4 my @lines;
223 4 foreach my $field ( keys %$self ) {
224 45 ▶ next if substr( $field, 0, 1 ) eq '_';
Branch: 1 branch
BranchTrueFalseTotal
if substr($field, 0, 1) eq "_" 4 41 45
225 41 my $value = $self->get_header( $field );
226  
227 41 my $out_field = $self->_internal_name_to_external_name( $field );
228  
229 41 push @lines, "$out_field: $value";
230 }
231  
232 4 ▶ push @lines, "Line-Count: " . $self->_entries->as_unique_sorted_list
Branch: 1 branch
BranchTrueFalseTotal
unless $self->header_exists("line_count") 3 1 4
233 unless $self->header_exists( 'line_count' );
234  
235 4 join "\n", sort( @lines ), "\n";
236 }
237 }
238  
239 7 ▶sub AUTOLOAD {
Subroutine AUTOLOAD: called CC=2 SCAR=7.0
240 7 my $self = shift;
241  
242 7 ( my $method = $CPAN::PackageDetails::Header::AUTOLOAD ) =~ s/.*:://;
243  
244 7 ▶ carp "No such method as $method!" unless $self->can( $method );
Branch: 1 branch
BranchTrueFalseTotal
unless $self->can($method) 0 7 7
245  
246 7 $self->get_header( $method );
247 }
248  
249 0 ▶sub DESTROY { }
Subroutine DESTROY: not called CC=1 SCAR=0.0
250  
251 =back
252  
253 =head1 TO DO
254  
255  
256 =head1 SEE ALSO
257  
258  
259 =head1 SOURCE AVAILABILITY
260  
261 This source is in Github:
262  
263 https://github.com/briandfoy/cpan-packagedetails
264  
265 =head1 AUTHOR
266  
267 brian d foy, C<< <briandfoy@pobox.com> >>
268  
269 =head1 COPYRIGHT AND LICENSE
270  
271 Copyright © 2009-2025, brian d foy <briandfoy@pobox.com>. All rights reserved.
272  
273 You may redistribute this under the terms of the Artistic License 2.0.
274  
275 =cut
276  
277 15 1;
« PackageDetails/Entries.pm ↑ Summary PackageDetails/Entry.pm »