Slackget/List.pm

filter: Statementstmt 81.6% 58 / 71 Branchbran 42.8% 12 / 28 Conditioncond 28.5% 6 / 21 MC/DCmcdc 0.0% 0 / 14 Subroutinesub 85.7% 12 / 14 Podpod 100% 12 / 12 totaltotal 62.5% 100 / 160 CC 24 SCAR 17.8
CC
24
Cov
63%
get_all
12.0
to_xml
11.0
Shift
6.7
CRAP 5.9
« Slackget/SpecialFileContainerList.pm ↑ Summary Slackget/MediaList.pm »
1 package Slackware::Slackget::List;
2  
3 5 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 5 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
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 5 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 2 ▶{
Subroutine new: called CC=3 SCAR=11.1
Pod documented
67 2 my ($class,%args) = @_ ;
68 2 ▶ return undef unless(defined($args{list_type}));
Branch: 1 branch
BranchTrueFalseTotal
unless defined $args{"list_type"} 0 2 2
69 2 my $self={%args};
70 2 $self->{LIST} = [] ;
71 2 $self->{ENCODING} = 'utf8' ;
72 2 ▶ $self->{ENCODING} = $args{'encoding'} if(defined($args{'encoding'})) ;
Branch: 1 branch
BranchTrueFalseTotal
if defined $args{"encoding"} 0 2 2
73 2 bless($self,$class);
74 2 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 12 ▶sub add {
Subroutine add: called CC=3 SCAR=11.2
Pod documented
88 12 my ($self,$pack) = @_ ;
89
90 # return undef if(ref($pack) ne "$self->{list_type}");
91 12 ▶ if(defined($self->{list_type}) ){
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"list_type"}) 12 0 12
92 12 ▶ return undef unless(UNIVERSAL::isa($pack,$self->{list_type}));
Branch: 1 branch
BranchTrueFalseTotal
unless UNIVERSAL::isa($pack, $self->{"list_type"}) 0 12 12
93 }
94 12 push @{$self->{LIST}}, $pack;
95 12 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 2 ▶sub get {
Subroutine get: called CC=4 SCAR=15.6
Pod documented
107 2 my ($self,$idx) = @_ ;
108 2 ▶ return undef unless(defined($idx));
Branch: 1 branch
BranchTrueFalseTotal
unless defined $idx 0 2 2
109 2 ▶ return undef unless(defined($self->{LIST}) && ref($self->{LIST}) eq 'ARRAY') ;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"LIST"} and ref $self->{"LIST"} eq "ARRAY" 0 2 2
Condition33%1/3
defined $self->{"LIST"} and ref $self->{"LIST"} eq "ARRAY"
!ll&&!rl&&r
002
MC/DC0%0/2
defined $self->{"LIST"} and ref $self->{"LIST"} eq "ARRAY"
defined $self->{"LIST"} ref $self->{"LIST"} eq "ARRAY"
Truth table33%1/3
A and B
A= defined $self->{"LIST"}B= ref $self->{"LIST"} eq "ARRAY"
ABresult
0X0
100
111
110 2 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 0 ▶sub get_all {
Subroutine get_all: not called CC=3 SCAR=24.8
Pod documented
122 0 my $self = shift ;
123 0 ▶ return [] unless(defined($self->{LIST}) && ref($self->{LIST}) eq 'ARRAY') ;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"LIST"} and ref $self->{"LIST"} eq "ARRAY" 0 0 0
Condition0%0/3
defined $self->{"LIST"} and ref $self->{"LIST"} eq "ARRAY"
!ll&&!rl&&r
000
MC/DC0%0/2
defined $self->{"LIST"} and ref $self->{"LIST"} eq "ARRAY"
defined $self->{"LIST"} ref $self->{"LIST"} eq "ARRAY"
Truth table0%0/3
A and B
A= defined $self->{"LIST"}B= ref $self->{"LIST"} eq "ARRAY"
ABresult
0X0
100
111
124 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 2 ▶sub Shift {
Subroutine Shift: called CC=4 SCAR=19.1
Pod documented
138 2 my ($self,$elem) = @_ ;
139 2 ▶ return undef unless(defined($self->{LIST}) && ref($self->{LIST}) eq 'ARRAY') ;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"LIST"} and ref $self->{"LIST"} eq "ARRAY" 0 2 2
Condition33%1/3
defined $self->{"LIST"} and ref $self->{"LIST"} eq "ARRAY"
!ll&&!rl&&r
002
MC/DC0%0/2
defined $self->{"LIST"} and ref $self->{"LIST"} eq "ARRAY"
defined $self->{"LIST"} ref $self->{"LIST"} eq "ARRAY"
Truth table33%1/3
A and B
A= defined $self->{"LIST"}B= ref $self->{"LIST"} eq "ARRAY"
ABresult
0X0
100
111
140 2 ▶ unless(defined($elem))
Branch: 1 branch
BranchTrueFalseTotal
if (defined $elem) { } 0 2 2
141 {
142 2 return shift(@{$self->{LIST}});
143 }
144 else
145 {
146 0 my $e = $self->get($elem);
147 0 $self->{LIST} = [@{$self->{LIST}}[0..($elem-1)], @{$self->{LIST}}[($elem+1)..$#{$self->{LIST}}]] ;
148 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 1 ▶sub to_XML {
Subroutine to_XML: called CC=1 SCAR=0.0
Pod documented
159 1 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 2 ▶{
Subroutine to_xml: called CC=8 SCAR=24.0
Pod documented
172 2 my $self = shift;
173 2 my $xml = "";
174 2 ▶ return [] unless(defined($self->{LIST}) && ref($self->{LIST}) eq 'ARRAY') ;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"LIST"} and ref $self->{"LIST"} eq "ARRAY" 0 2 2
Condition33%1/3
defined $self->{"LIST"} and ref $self->{"LIST"} eq "ARRAY"
!ll&&!rl&&r
002
MC/DC0%0/2
defined $self->{"LIST"} and ref $self->{"LIST"} eq "ARRAY"
defined $self->{"LIST"} ref $self->{"LIST"} eq "ARRAY"
Truth table33%1/3
A and B
A= defined $self->{"LIST"}B= ref $self->{"LIST"} eq "ARRAY"
ABresult
0X0
100
111
175 2 $self->{ENCODING} = uc($self->{ENCODING}) ; # NOTE: check if it do not screw up
176 2 ▶ $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'}));
Branch: 1 branch
BranchTrueFalseTotal
if not defined $self->{"no-root-tag"} and defined $self->{"root-tag"} 2 0 2
Condition33%1/3
not defined $self->{"no-root-tag"} and defined $self->{"root-tag"}
!ll&&!rl&&r
002
MC/DC0%0/2
not defined $self->{"no-root-tag"} and defined $self->{"root-tag"}
not defined $self->{"no-root-tag"} defined $self->{"root-tag"}
Truth table33%1/3
not(A) and B
A= not defined $self->{"no-root-tag"}B= defined $self->{"root-tag"}
ABresult
0X0
100
111
177 2 foreach (@{$self->{LIST}}){
178 12 $xml .= $_->to_xml();
179 }
180 2 ▶ $xml .= "</$self->{'root-tag'}>\n" if(!defined($self->{'no-root-tag'}) && defined($self->{'root-tag'}));
Branch: 1 branch
BranchTrueFalseTotal
if not defined $self->{"no-root-tag"} and defined $self->{"root-tag"} 2 0 2
Condition33%1/3
not defined $self->{"no-root-tag"} and defined $self->{"root-tag"}
!ll&&!rl&&r
002
MC/DC0%0/2
not defined $self->{"no-root-tag"} and defined $self->{"root-tag"}
not defined $self->{"no-root-tag"} defined $self->{"root-tag"}
Truth table33%1/3
not(A) and B
A= not defined $self->{"no-root-tag"}B= defined $self->{"root-tag"}
ABresult
0X0
100
111
181 2 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 1 ▶sub to_HTML {
Subroutine to_HTML: called CC=1 SCAR=0.0
Pod documented
192 1 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 2 ▶{
Subroutine to_html: called CC=2 SCAR=6.9
Pod documented
206 2 my $self = shift;
207 2 my $xml = '<ul>';
208 2 foreach (@{$self->{LIST}}){
209 12 $xml .= $_->to_html();
210 }
211 2 $xml .= '</ul>';
212 2 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 0 ▶sub to_string{
Subroutine to_string: not called CC=2 SCAR=17.9
Pod documented
224 0 my $self = shift;
225 0 ▶ if( $self->can('__to_string') ){
Branch: 1 branch
BranchTrueFalseTotal
if ($self->can('__to_string')) { } 0 0 0
226 0 return $self->__to_string();
227 }else{
228 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 6 ▶{
Subroutine Length: called CC=3 SCAR=12.0
Pod documented
242 6 my $self = shift;
243 6 ▶ return 0 unless(defined($self->{LIST}) && ref($self->{LIST}) eq 'ARRAY') ;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"LIST"} and ref $self->{"LIST"} eq "ARRAY" 0 6 6
Condition33%1/3
defined $self->{"LIST"} and ref $self->{"LIST"} eq "ARRAY"
!ll&&!rl&&r
006
MC/DC0%0/2
defined $self->{"LIST"} and ref $self->{"LIST"} eq "ARRAY"
defined $self->{"LIST"} ref $self->{"LIST"} eq "ARRAY"
Truth table33%1/3
A and B
A= defined $self->{"LIST"}B= ref $self->{"LIST"} eq "ARRAY"
ABresult
0X0
100
111
244 6 return scalar(@{$self->{LIST}});
245 }
246  
247 =head2 empty
248  
249 Empty the list
250  
251 $list->empty ;
252  
253 =cut
254  
255 sub empty
256 2 ▶{
Subroutine empty: called CC=1 SCAR=0.0
Pod documented
257 2 my $self = shift ;
258 2 $self->{LIST} = undef ;
259 2 delete($self->{LIST});
260 2 $self->{LIST} = [] ;
261 }
262  
263  
264 =head1 AUTHOR
265  
266 DUPUIS Arnaud, C<< <a.dupuis@infinityperl.org> >>
267  
268 =head1 BUGS
269  
270 Please report any bugs or feature requests to
271 C<bug-Slackware-Slackget@rt.cpan.org>, or through the web interface at
272 L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Slackware-Slackget>.
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<http://www.infinityperl.org/category/slack-get>
290  
291 =item * slack-get specific website
292  
293 L<http://slackget.infinityperl.org>
294  
295 =item * RT: CPAN's request tracker
296  
297 L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=Slackware-Slackget>
298  
299 =item * AnnoCPAN: Annotated CPAN documentation
300  
301 L<http://annocpan.org/dist/Slackware-Slackget>
302  
303 =item * CPAN Ratings
304  
305 L<http://cpanratings.perl.org/d/Slackware-Slackget>
306  
307 =item * Search CPAN
308  
309 L<http://search.cpan.org/dist/Slackware-Slackget>
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 5 1; # End of Slackware::Slackget::List
« Slackget/SpecialFileContainerList.pm ↑ Summary Slackget/MediaList.pm »