Slackget/Status.pm

filter: Statementstmt 18.1% 8 / 44 Branchbran 0.0% 0 / 18 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 16.6% 2 / 12 Podpod 100% 10 / 10 totaltotal 23.8% 20 / 84 CC 11 SCAR 22.7
CC
11
Cov
13%
current
20.0
new
12.0
to_string
12.0
CRAP 9.6
« Slackget/Network/Message.pm ↑ Summary Slackget/Local.pm »
1 package Slackware::Slackget::Status;
2  
3 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 =head1 NAME
7  
8 Slackware::Slackget::Status - A class for returning a status code with its explanations
9  
10 =head1 VERSION
11  
12 Version 1.0.100
13  
14 =cut
15  
16 2 our $VERSION = '1.0.100';
17  
18 =head1 SYNOPSIS
19  
20 This class is used at a status object which can tell more informations to user. In this object are stored couples of integer (the return code of the function which return the status object), and string (the human readable description of the error)
21  
22 use Slackware::Slackget::Status;
23  
24 my $status = Slackware::Slackget::Status->new(
25 codes => {
26 0 => "All operations goes well",
27 1 => "Parameters unexpected",
28 2 => "Network error"
29 }
30 );
31 print "last error message was: ",$status->to_string,"\n";
32 if($status->is_error)
33 {
34 die "Error: ",$status->to_string,"\n";
35 }
36 elsif($status->is_success)
37 {
38 print $status->to_string,"\n";
39 }
40  
41 Please note that you must see at the documentation of a class to know the returned codes.
42  
43 =cut
44  
45 sub new
46 0 ▶{
Subroutine new: not called CC=3 SCAR=24.8
Pod documented
47 0 my ($class,%arg) = @_ ;
48 0 my $self={ CURRENT_CODE => undef };
49 # return undef if(!defined($arg{'codes'}) && ref($arg{codes}) ne 'HASH');
50 0 $self->{CODES} = $arg{'codes'} ;
51 0 ▶ $self->{ERROR_CODES} = $arg{'error_codes'} if($arg{'error_codes'}) ;
Branch: 1 branch
BranchTrueFalseTotal
if $arg{"error_codes"} 0 0 0
52 0 ▶ $self->{SUCCESS_CODES} = $arg{'success_codes'} if($arg{'success_codes'}) ;
Branch: 1 branch
BranchTrueFalseTotal
if $arg{"success_codes"} 0 0 0
53 0 bless($self,$class);
54 0 return $self;
55 }
56  
57 =head1 CONSTRUCTOR
58  
59 =head2 new
60  
61 You need to pass to the constructor a parameter 'codes' wich contain a hashref with number return code as keys and explanation strings as values :
62  
63 my $status = new Slackware::Slackget::Status (
64 codes => {
65 0 => "All good\n",
66 1 => "Network unreachable\n",
67 2 => "Host unreachable\n",
68 3 => "Remote file seems not exist\n"
69 }
70 );
71  
72 You can, optionnally, give to more parameters : success_codes and error_codes within the same format than codes. It'll allow you to control the current status via the is_success() and is_error() methods.
73  
74 =head1 FUNCTIONS
75  
76 =head2 to_string
77  
78 Return the explanation string of the current status.
79  
80 if($connection->fetch_file($remote_file,$local_file) > 0)
81 {
82 print "ERROR : ",$status->to_string ;
83 return undef;
84 }
85 else
86 {
87 ...
88 }
89  
90 =cut
91  
92 0 ▶sub to_string {
Subroutine to_string: not called CC=3 SCAR=24.8
Pod documented
93 0 my $self = shift;
94 0 ▶ return $self->{SUCCESS_CODES}->{$self->{CURRENT_CODE}} if($self->{SUCCESS_CODES}->{$self->{CURRENT_CODE}}) ;
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"SUCCESS_CODES"}{$self->{"CURRENT_CODE"}} 0 0 0
95 0 ▶ return $self->{ERROR_CODES}->{$self->{CURRENT_CODE}} if($self->{ERROR_CODES}->{$self->{CURRENT_CODE}}) ;
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"ERROR_CODES"}{$self->{"CURRENT_CODE"}} 0 0 0
96 0 return $self->{CODES}->{$self->{CURRENT_CODE}} ;
97 }
98  
99 =head2 to_int
100  
101 Same as to_string but return the code number.
102  
103 =cut
104  
105 0 ▶sub to_int {
Subroutine to_int: not called CC=1 SCAR=6.9
Pod documented
106 0 my $self = shift;
107 0 return $self->{CURRENT_CODE} ;
108 }
109  
110 =head2 to_XML (deprecated)
111  
112 Same as to_xml(), provided for backward compatibility.
113  
114 =cut
115  
116 0 ▶sub to_XML {
Subroutine to_XML: not called CC=1 SCAR=6.9
Pod documented
117 0 return to_xml(@_);
118 }
119  
120 =head2 to_xml
121  
122 return an xml ecoded string, represented the current status. The XML string will be like that :
123  
124 <status code="0" description="All goes well" />
125  
126 $xml_file->Add($status->to_xml) ;
127  
128 =cut
129  
130 sub to_xml
131 0 ▶{
Subroutine to_xml: not called CC=1 SCAR=6.9
Pod documented
132 0 my $self = shift ;
133 0 return "<status code=\"".$self->to_int()."\" description=\"".$self->to_string()."\" />";
134 }
135  
136 =head2 to_HTML (deprecated)
137  
138 Same as to_html(), provided for backward compatibility.
139  
140 =cut
141  
142 0 ▶sub to_HTML {
Subroutine to_HTML: not called CC=1 SCAR=6.9
Pod documented
143 0 return to_html(@_);
144 }
145  
146 =head2 to_html
147  
148 return the status as an HTML encoded string
149  
150 =cut
151  
152 sub to_html
153 0 ▶{
Subroutine to_html: not called CC=1 SCAR=6.9
Pod documented
154 0 my $self = shift ;
155 0 return "<p id=\"status\"><h3>Status</h3><strong>code :</strong> ".$self->to_int()."<br/><strong>description :</strong> ".$self->to_string()."<br/>\n</p>\n";
156 }
157  
158 =head2 current
159  
160 Called wihtout argument, just call to_int(), call with an integer argument, set the current status code to this int.
161  
162 my $code = $status->current ; # same effect as my $code = $status->to_int ;
163 or
164 $status->current(12);
165
166 Warning : call current() with a non-integer argument will fail ! The error code MUST BE AN INTEGER.
167  
168 =cut
169  
170 sub current
171 0 ▶{
Subroutine current: not called CC=4 SCAR=30.0
Pod documented
172 0 my ($self,$code) = @_;
173 0 ▶ if(!defined($code))
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $code) { } 0 0 0
174 {
175 0 return $self->to_int ;
176 }
177 else
178 {
179 0 ▶ if($code=~ /^\d+$/)
Branch: 1 branch
BranchTrueFalseTotal
if ($code =~ /^\d+$/) { } 0 0 0
180 {
181 0 ▶ print "[Slackware::Slackget::Status] (debug) setting current status code to $code.\n" if($ENV{SG_DAEMON_DEBUG});
Branch: 1 branch
BranchTrueFalseTotal
if $ENV{"SG_DAEMON_DEBUG"} 0 0 0
182 0 $self->{CURRENT_CODE} = $code;
183 0 return 1;
184 }
185 else
186 {
187 0 warn "[Slackware::Slackget::Status] '$code' is not an integer.\n";
188 0 return undef;
189 }
190 }
191 }
192  
193 =head2 is_success
194  
195 return true (1) if the current() code is declared as a success code (constructor's parameter: success_codes). Return false otherwise (particularly if you have only set codes and not success_codes).
196  
197 =cut
198  
199 0 ▶sub is_success {
Subroutine is_success: not called CC=3 SCAR=24.8
Pod documented
200 0 my $self = shift;
201 # return 1 if($self->{SUCCESS_CODES}->{$self->{CURRENT_CODE}});
202 0 foreach my $code ( keys(%{$self->{SUCCESS_CODES}}) ){
203 # print "[Slackware::Slackget::Status] comparing success code '$code' to current status code '".$self->{CURRENT_CODE}."'.\n";
204 return 1 if($code == $self->{CURRENT_CODE})
205 0 ▶ }
Branch: 1 branch
BranchTrueFalseTotal
if $code == $self->{"CURRENT_CODE"} 0 0 0
206 0 return 0;
207 }
208  
209  
210 =head2 is_error
211  
212 return true (1) if the current() code is declared as an error code (constructor's parameter: error_codes). Return false otherwise (particularly if you have only set codes and not error_codes).
213  
214 =cut
215  
216 0 ▶sub is_error {
Subroutine is_error: not called CC=2 SCAR=17.9
Pod documented
217 0 my $self = shift;
218 0 ▶ return 1 if($self->{ERROR_CODES}->{$self->{CURRENT_CODE}});
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"ERROR_CODES"}{$self->{"CURRENT_CODE"}} 0 0 0
219 0 return 0;
220 }
221  
222 =head1 AUTHOR
223  
224 DUPUIS Arnaud, C<< <a.dupuis@infinityperl.org> >>
225  
226 =head1 BUGS
227  
228 Please report any bugs or feature requests to
229 C<bug-Slackware-Slackget@rt.cpan.org>, or through the web interface at
230 L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Slackware-Slackget>.
231 I will be notified, and then you'll automatically be notified of progress on
232 your bug as I make changes.
233  
234 =head1 SUPPORT
235  
236 You can find documentation for this module with the perldoc command.
237  
238 perldoc Slackware::Slackget::Status
239  
240  
241 You can also look for information at:
242  
243 =over 4
244  
245 =item * Infinity Perl website
246  
247 L<http://www.infinityperl.org/category/slack-get>
248  
249 =item * slack-get specific website
250  
251 L<http://slackget.infinityperl.org>
252  
253 =item * RT: CPAN's request tracker
254  
255 L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=Slackware-Slackget>
256  
257 =item * AnnoCPAN: Annotated CPAN documentation
258  
259 L<http://annocpan.org/dist/Slackware-Slackget>
260  
261 =item * CPAN Ratings
262  
263 L<http://cpanratings.perl.org/d/Slackware-Slackget>
264  
265 =item * Search CPAN
266  
267 L<http://search.cpan.org/dist/Slackware-Slackget>
268  
269 =back
270  
271 =head1 ACKNOWLEDGEMENTS
272  
273 Thanks to Bertrand Dupuis (yes my brother) for his contribution to the documentation.
274  
275 =head1 COPYRIGHT & LICENSE
276  
277 Copyright 2005 DUPUIS Arnaud, All Rights Reserved.
278  
279 This program is free software; you can redistribute it and/or modify it
280 under the same terms as Perl itself.
281  
282 =cut
283  
284 2 1; # End of Slackware::Slackget::Status
« Slackget/Network/Message.pm ↑ Summary Slackget/Local.pm »