File Coverage

blib/lib/Debian/Control/Stanza/CommaSeparated.pm
Criterion Covered Total %
statement 31 31 100.0
branch 1 2 50.0
condition n/a
subroutine 10 10 100.0
pod 4 4 100.0
total 46 47 97.8


line stmt bran cond sub pod time code
1             package Debian::Control::Stanza::CommaSeparated;
2              
3             =head1 NAME
4              
5             Debian::Control::Stanza::CommaSeparated - comma separated debian/control field abstraction
6              
7             =cut
8              
9 9     9   3497803 use strict;
  9         33  
  9         263  
10 9     9   47 use warnings;
  9         18  
  9         375  
11              
12             our $VERSION = '0.96';
13              
14 9     9   2332 use Array::Unique;
  9         6635  
  9         196  
15 9     9   2534 use Text::ParseWords qw(quotewords);
  9         9478  
  9         709  
16              
17 9     9   895 use overload '""' => \&as_string;
  9         787  
  9         152  
18              
19             =head1 SYNOPSIS
20              
21             my $f = Debian::Control::Stanza::CommaSeparated->new(
22             'Joe M ');
23             $f->add('"Smith, Agent" , Joe M ');
24             print $f->as_string;
25             # 'Joe M , "Smith, Agent" '
26             print "$f"; # the same
27             $f->sort;
28              
29             =head1 DESCRIPTION
30              
31             Debian::Control::Stanza::CommaSeparated abstracts handling of comma-separated
32             list of values, often found in F file fields like I.
33             Note that the various dependency fields in F also use
34             comma-separated values, but the L class is more suitable
35             for these as it is for example also capable of finding overlapping dependency
36             declarations.
37              
38             =head1 CONSTRUCTOR
39              
40             =over
41              
42             =item new (initial values)
43              
44             The initial values list is parsed and may contain strings that are in fact
45             comma-separated lists. These are split appropriately using L'
46             I routine.
47              
48             =back
49              
50             =cut
51              
52             sub new {
53 1     1 1 76 my $self = bless [], shift;
54              
55 1         27 tie @$self, 'Array::Unique';
56              
57 1 50       14 $self->add(@_) if @_;
58              
59 1         62 $self;
60             }
61              
62             =head1 METHODS
63              
64             =over
65              
66             =item as_string
67              
68             Returns text representation of the list. A simple join of the elements by C<, >.
69              
70             The same function is used for overloading the stringification operation.
71              
72             =cut
73              
74             sub as_string
75             {
76 1     1 1 644 return join( ', ', @{ $_[0] } );
  1         6  
77             }
78              
79             sub _parse {
80 3     3   5 my $self = shift;
81              
82 3         3 my @output;
83              
84 3         20 for (@_) {
85 3         20 my @items = quotewords( qr/\s*,\s*/, 1, $_ );
86 3         333 push @output, @items;
87             }
88              
89 3         14 return @output;
90             }
91              
92             =item add I<@items>
93              
94             Adds the given items to the list. Items that are already present are not added,
95             keeping the list unique.
96              
97             =cut
98              
99             sub add {
100 3     3 1 1220 my ( $self, @items ) = @_;
101              
102 3         7 push @$self, $self->_parse(@items);
103             }
104              
105             =item sort
106              
107             A handy method for sorting the list.
108              
109             =cut
110              
111             sub sort {
112 1     1 1 1352 my $self = shift;
113              
114 1         4 @$self = sort @$self;
115             }
116              
117             =back
118              
119             =cut
120              
121             1;