Carton/Index.pm untested

filter: Statementstmt 0.0% 0 / 36 Branchbran 0.0% 0 / 6 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 0.0% 0 / 9 Podpod 0.0% 0 / 6 totaltotal 0.0% 0 / 57 CC 4 SCAR 16.4
CC
4
Cov
0%
pad
12.0
_format_line
6.0
add_package
2.0
CRAP 5.2
1 package Carton::Index;
2 use strict;
3 use Class::Tiny {
4 _packages => sub { +{} },
5 generator => sub { require Carton; "Carton $Carton::VERSION" },
6 };
7  
8 sub add_package {
9 my($self, $package) = @_;
10 $self->_packages->{$package->name} = $package; # XXX ||=
11 }
12  
13 sub count {
14 my $self = shift;
15 scalar keys %{$self->_packages};
16 }
17  
18 sub packages {
19 my $self = shift;
20 sort { lc $a->name cmp lc $b->name } values %{$self->_packages};
21 }
22  
23 sub write {
24 my($self, $fh) = @_;
25  
26 print $fh <<EOF;
27 File: 02packages.details.txt
28 URL: http://www.perl.com/CPAN/modules/02packages.details.txt
29 Description: Package names found in cpanfile.snapshot
30 Columns: package name, version, path
31 Intended-For: Automated fetch routines, namespace documentation.
32 Written-By: @{[ $self->generator ]}
33 Line-Count: @{[ $self->count ]}
34 Last-Updated: @{[ scalar localtime ]}
35  
36 EOF
37 for my $p ($self->packages) {
38 print $fh $self->_format_line($p->name, $p->version_format, $p->pathname);
39 }
40 }
41  
42 sub _format_line {
43 my($self, @row) = @_;
44  
45 # from PAUSE::mldistwatch::rewrite02
46 my $one = 30;
47 my $two = 8;
48  
49 if (length $row[0] > $one) {
50 $one += 8 - length $row[1];
51 $two = length $row[1];
52 }
53  
54 sprintf "%-${one}s %${two}s %s\n", @row;
55 }
56  
57 sub pad {
58 my($str, $len, $left) = @_;
59  
60 my $howmany = $len - length($str);
61 return $str if $howmany <= 0;
62  
63 my $pad = " " x $howmany;
64 return $left ? "$pad$str" : "$str$pad";
65 }
66  
67  
68 1;