File Coverage

blib/lib/Slackware/Slackget/List.pm
Criterion Covered Total %
statement 6 66 9.0
branch 0 24 0.0
condition 0 21 0.0
subroutine 2 14 14.2
pod 12 12 100.0
total 20 137 14.6


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