File Coverage

blib/lib/Slackware/Slackget/List.pm
Criterion Covered Total %
statement 56 69 81.1
branch 12 28 42.8
condition 6 21 28.5
subroutine 12 14 85.7
pod 12 12 100.0
total 98 144 68.0


line stmt bran cond sub pod time code
1             package Slackware::Slackget::List;
2              
3 3     3   100094 use warnings;
  3         7  
  3         112  
4 3     3   17 use strict;
  3         6  
  3         7047  
5              
6             =head1 NAME
7              
8             Slackware::Slackget::List - A generic list abstraction.
9              
10             =head1 VERSION
11              
12             Version 1.0.1
13              
14             =cut
15              
16             our $VERSION = '1.0.1';
17              
18             =head1 SYNOPSIS
19              
20             This class is a generic list abstraction. Most of the time it rely on Perl implementation of list operation, but it also implements some sanity checks.
21              
22             This class is mainly designed to be inherited from.
23              
24             use Slackware::Slackget::List;
25              
26             my $list = Slackware::Slackget::List->new();
27             $list->add($element);
28             $list->get($index);
29             my $element = $list->Shift();
30            
31              
32             =head1 CONSTRUCTOR
33              
34             =head2 new
35              
36             This class constructor take the followings arguments :
37              
38             * list_type. You must provide a string which will specialize your list. Ex:
39              
40             For a Slackware::Slackget::Package list :
41             my $packagelist = new Slackware::Slackget::List (list_type => 'Slackware::Slackget::Package') ;
42              
43             * root-tag : the root tag of the XML generated by the to_XML method.
44              
45             For a Slackware::Slackget::Package list :
46             my $packagelist = new Slackware::Slackget::List ('root-tag' => 'packagelist') ;
47              
48              
49             * no-root-tag : to disabling the root tag in the generated XML output.
50              
51             For a Slackware::Slackget::Package list :
52             my $packagelist = new Slackware::Slackget::List ('no-root-tag' => 1) ;
53              
54             A traditionnal constructor is :
55              
56             my $speciallist = new Slackware::Slackget::List (
57             'list_type' => 'Slackware::Slackget::Special',
58             'root-tag' => 'special-list'
59             );
60              
61             But look at special class Slackware::Slackget::*List before creating your own list : maybe I have already do the work :)
62              
63             =cut
64              
65             sub new
66             {
67 2     2 1 37 my ($class,%args) = @_ ;
68 2 50       12 return undef unless(defined($args{list_type}));
69 2         12 my $self={%args};
70 2         9 $self->{LIST} = [] ;
71 2         6 $self->{ENCODING} = 'utf8' ;
72 2 50       10 $self->{ENCODING} = $args{'encoding'} if(defined($args{'encoding'})) ;
73 2         8 bless($self,$class);
74 2         10 return $self;
75             }
76              
77             =head1 FUNCTIONS
78              
79             =head2 add
80              
81             Add the element passed in argument to the list. The argument must be an object of the list_type type.
82              
83             $list->add($element);
84              
85             =cut
86              
87             sub add {
88 12     12 1 17 my ($self,$pack) = @_ ;
89            
90             # return undef if(ref($pack) ne "$self->{list_type}");
91 12 50       36 if(defined($self->{list_type}) ){
92 12 50       167 return undef unless(UNIVERSAL::isa($pack,$self->{list_type}));
93             }
94 12         16 push @{$self->{LIST}}, $pack;
  12         28  
95 12         59 return 1;
96             }
97              
98             =head2 get
99              
100             return the $index -nth object in the list
101              
102             $element = $list->get($index);
103              
104             =cut
105              
106             sub get {
107 2     2 1 5 my ($self,$idx) = @_ ;
108 2 50       11 return undef unless(defined($idx));
109 2 50 33     23 return undef unless(defined($self->{LIST}) && ref($self->{LIST}) eq 'ARRAY') ;
110 2         15 return $self->{LIST}->[$idx];
111             }
112              
113             =head2 get_all
114              
115             return a reference on an array containing all packages.
116              
117             $arrayref = $list->get_all();
118              
119             =cut
120              
121             sub get_all {
122 0     0 1 0 my $self = shift ;
123 0 0 0     0 return [] unless(defined($self->{LIST}) && ref($self->{LIST}) eq 'ARRAY') ;
124 0         0 return $self->{LIST};
125             }
126              
127             =head2 Shift
128              
129             Same as the Perl shift. Shifts of and return the first object of the Slackware::Slackget::List;
130              
131             $element = $list->Shift();
132              
133             If a numerical index is passed shift and return the given index.
134              
135             =cut
136              
137             sub Shift {
138 2     2 1 16 my ($self,$elem) = @_ ;
139 2 50 33     29 return undef unless(defined($self->{LIST}) && ref($self->{LIST}) eq 'ARRAY') ;
140 2 50       9 unless(defined($elem))
141             {
142 2         9 return shift(@{$self->{LIST}});
  2         13  
143             }
144             else
145             {
146 0         0 my $e = $self->get($elem);
147 0         0 $self->{LIST} = [@{$self->{LIST}}[0..($elem-1)], @{$self->{LIST}}[($elem+1)..$#{$self->{LIST}}]] ;
  0         0  
  0         0  
  0         0  
148 0         0 return $e;
149             }
150             }
151              
152             =head2 to_XML (deprecated)
153              
154             Same as to_xml(), provided for backward compatibility.
155              
156             =cut
157              
158             sub to_XML {
159 1     1 1 6 return to_xml(@_);
160             }
161              
162             =head2 to_xml
163              
164             return an XML encoded string.
165              
166             $xml = $list->to_xml();
167              
168             =cut
169              
170             sub to_xml
171             {
172 2     2 1 5 my $self = shift;
173 2         5 my $xml = "";
174 2 50 33     24 return [] unless(defined($self->{LIST}) && ref($self->{LIST}) eq 'ARRAY') ;
175 2         9 $self->{ENCODING} = uc($self->{ENCODING}) ; # NOTE: check if it do not screw up
176 2 50 33     25 $xml .= "{ENCODING}\" standalone=\"yes\"?>\n<$self->{'root-tag'}>\n" if(!defined($self->{'no-root-tag'}) && defined($self->{'root-tag'}));
177 2         4 foreach (@{$self->{LIST}}){
  2         8  
178 12         43 $xml .= $_->to_xml();
179             }
180 2 50 33     25 $xml .= "{'root-tag'}>\n" if(!defined($self->{'no-root-tag'}) && defined($self->{'root-tag'}));
181 2         19 return $xml;
182             }
183              
184              
185             =head2 to_HTML (deprecated)
186              
187             Same as to_html(), provided for backward compatibility.
188              
189             =cut
190              
191             sub to_HTML {
192 1     1 1 5 return to_html(@_);
193             }
194              
195              
196             =head2 to_html
197              
198             return an HTML encoded string.
199              
200             $xml = $list->to_html();
201              
202             =cut
203              
204             sub to_html
205             {
206 2     2 1 4 my $self = shift;
207 2         6 my $xml = '
    ';
208 2         5 foreach (@{$self->{LIST}}){
  2         8  
209 12         43 $xml .= $_->to_html();
210             }
211 2         9 $xml .= '';
212 2         11 return $xml;
213             }
214              
215             =head2 to_string
216              
217             If this class is subclassed and if the subclass have a __to_string() method this is one is called.
218              
219             If not, this method is an alias for to_xml().
220              
221             =cut
222              
223             sub to_string{
224 0     0 1 0 my $self = shift;
225 0 0       0 if( $self->can('__to_string') ){
226 0         0 return $self->__to_string();
227             }else{
228 0         0 return $self->to_xml();
229             }
230             }
231              
232             =head2 Length
233              
234             Return the length (the number of element) of the current list. If you are interest by the size in memory you have to multiply by yourself the number returned by this method by the size of a single object.
235              
236             $list->Length ;
237              
238             =cut
239              
240             sub Length
241             {
242 6     6 1 12 my $self = shift;
243 6 50 33     42 return 0 unless(defined($self->{LIST}) && ref($self->{LIST}) eq 'ARRAY') ;
244 6         8 return scalar(@{$self->{LIST}});
  6         33  
245             }
246              
247             =head2 empty
248              
249             Empty the list
250              
251             $list->empty ;
252              
253             =cut
254              
255             sub empty
256             {
257 2     2 1 5 my $self = shift ;
258 2         5 $self->{LIST} = undef ;
259 2         36 delete($self->{LIST});
260 2         13 $self->{LIST} = [] ;
261             }
262              
263              
264             =head1 AUTHOR
265              
266             DUPUIS Arnaud, C<< >>
267              
268             =head1 BUGS
269              
270             Please report any bugs or feature requests to
271             C, or through the web interface at
272             L.
273             I will be notified, and then you'll automatically be notified of progress on
274             your bug as I make changes.
275              
276             =head1 SUPPORT
277              
278             You can find documentation for this module with the perldoc command.
279              
280             perldoc Slackware::Slackget::List
281              
282              
283             You can also look for information at:
284              
285             =over 4
286              
287             =item * Infinity Perl website
288              
289             L
290              
291             =item * slack-get specific website
292              
293             L
294              
295             =item * RT: CPAN's request tracker
296              
297             L
298              
299             =item * AnnoCPAN: Annotated CPAN documentation
300              
301             L
302              
303             =item * CPAN Ratings
304              
305             L
306              
307             =item * Search CPAN
308              
309             L
310              
311             =back
312              
313             =head1 ACKNOWLEDGEMENTS
314              
315             Thanks to Bertrand Dupuis (yes my brother) for his contribution to the documentation.
316              
317             =head1 SEE ALSO
318              
319             =head1 COPYRIGHT & LICENSE
320              
321             Copyright 2005 DUPUIS Arnaud, All Rights Reserved.
322              
323             This program is free software; you can redistribute it and/or modify it
324             under the same terms as Perl itself.
325              
326             =cut
327              
328             1; # End of Slackware::Slackget::List