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   4888270 use strict;
  9         29  
  9         368  
10 9     9   53 use warnings;
  9         15  
  9         549  
11              
12             our $VERSION = '0.66';
13              
14 9     9   7040 use Array::Unique;
  9         8388  
  9         212  
15 9     9   6969 use Text::ParseWords qw(quotewords);
  9         12021  
  9         792  
16              
17 9     9   55 use overload '""' => \&as_string;
  9         17  
  9         206  
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 15 my $self = bless [], shift;
54              
55 1         36 tie @$self, 'Array::Unique';
56              
57 1 50       15 $self->add(@_) if @_;
58              
59 1         60 $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 766 return join( ', ', @{ $_[0] } );
  1         4  
77             }
78              
79             sub _parse {
80 3     3   4 my $self = shift;
81              
82 3         3 my @output;
83              
84 3         7 for (@_) {
85 3         18 my @items = quotewords( qr/\s*,\s*/, 1, $_ );
86 3         321 push @output, @items;
87             }
88              
89 3         13 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 1513 my ( $self, @items ) = @_;
101              
102 3         9 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 1380 my $self = shift;
113              
114 1         4 @$self = sort @$self;
115             }
116              
117             =back
118              
119             =cut
120              
121             1;