File Coverage

blib/lib/Slackware/Slackget/Status.pm
Criterion Covered Total %
statement 6 40 15.0
branch 0 18 0.0
condition n/a
subroutine 2 12 16.6
pod 10 10 100.0
total 18 80 22.5


line stmt bran cond sub pod time code
1             package Slackware::Slackget::Status;
2              
3 2     2   9 use warnings;
  2         2  
  2         57  
4 2     2   6 use strict;
  2         3  
  2         834  
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.99
13              
14             =cut
15              
16             our $VERSION = '1.0.99';
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->to_int == 2)
33             {
34             die "A network error occured\n";
35             }
36              
37             Please note that you must see at the documentation of a class to know the returned codes.
38              
39             =cut
40              
41             sub new
42             {
43 0     0 1   my ($class,%arg) = @_ ;
44 0           my $self={ CURRENT_CODE => undef };
45             # return undef if(!defined($arg{'codes'}) && ref($arg{codes}) ne 'HASH');
46 0           $self->{CODES} = $arg{'codes'} ;
47 0 0         $self->{ERROR_CODES} = $arg{'error_codes'} if($arg{'error_codes'}) ;
48 0 0         $self->{SUCCESS_CODES} = $arg{'success_codes'} if($arg{'success_codes'}) ;
49 0           bless($self,$class);
50 0           return $self;
51             }
52              
53             =head1 CONSTRUCTOR
54              
55             =head2 new
56              
57             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 :
58              
59             my $status = new Slackware::Slackget::Status (
60             codes => {
61             0 => "All good\n",
62             1 => "Network unreachable\n",
63             2 => "Host unreachable\n",
64             3 => "Remote file seems not exist\n"
65             }
66             );
67              
68             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.
69              
70             =head1 FUNCTIONS
71              
72             =head2 to_string
73              
74             Return the explanation string of the current status.
75              
76             if($connection->fetch_file($remote_file,$local_file) > 0)
77             {
78             print "ERROR : ",$status->to_string ;
79             return undef;
80             }
81             else
82             {
83             ...
84             }
85              
86             =cut
87              
88             sub to_string {
89 0     0 1   my $self = shift;
90 0 0         return $self->{SUCCESS_CODES}->{$self->{CURRENT_CODE}} if($self->{SUCCESS_CODES}->{$self->{CURRENT_CODE}}) ;
91 0 0         return $self->{ERROR_CODES}->{$self->{CURRENT_CODE}} if($self->{ERROR_CODES}->{$self->{CURRENT_CODE}}) ;
92 0           return $self->{CODES}->{$self->{CURRENT_CODE}} ;
93             }
94              
95             =head2 to_int
96              
97             Same as to_string but return the code number.
98              
99             =cut
100              
101             sub to_int {
102 0     0 1   my $self = shift;
103 0           return $self->{CURRENT_CODE} ;
104             }
105              
106             =head2 to_XML (deprecated)
107              
108             Same as to_xml(), provided for backward compatibility.
109              
110             =cut
111              
112             sub to_XML {
113 0     0 1   return to_xml(@_);
114             }
115              
116             =head2 to_xml
117              
118             return an xml ecoded string, represented the current status. The XML string will be like that :
119              
120             <status code="0" description="All goes well" />
121              
122             $xml_file->Add($status->to_xml) ;
123              
124             =cut
125              
126             sub to_xml
127             {
128 0     0 1   my $self = shift ;
129 0           return "<status code=\"".$self->to_int()."\" description=\"".$self->to_string()."\" />";
130             }
131              
132             =head2 to_HTML (deprecated)
133              
134             Same as to_html(), provided for backward compatibility.
135              
136             =cut
137              
138             sub to_HTML {
139 0     0 1   return to_html(@_);
140             }
141              
142             =head2 to_html
143              
144             return the status as an HTML encoded string
145              
146             =cut
147              
148             sub to_html
149             {
150 0     0 1   my $self = shift ;
151 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";
152             }
153              
154             =head2 current
155              
156             Called wihtout argument, just call to_int(), call with an integer argument, set the current status code to this int.
157              
158             my $code = $status->current ; # same effect as my $code = $status->to_int ;
159             or
160             $status->current(12);
161            
162             Warning : call current() with a non-integer argument will fail ! The error code MUST BE AN INTEGER.
163              
164             =cut
165              
166             sub current
167             {
168 0     0 1   my ($self,$code) = @_;
169 0 0         if(!defined($code))
170             {
171 0           return $self->to_int ;
172             }
173             else
174             {
175 0 0         if($code=~ /^\d+$/)
176             {
177 0 0         print "[Slackware::Slackget::Status] (debug) setting current status code to $code.\n" if($ENV{SG_DAEMON_DEBUG});
178 0           $self->{CURRENT_CODE} = $code;
179 0           return 1;
180             }
181             else
182             {
183 0           warn "[Slackware::Slackget::Status] '$code' is not an integer.\n";
184 0           return undef;
185             }
186             }
187             }
188              
189             =head2 is_success
190              
191             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).
192              
193             =cut
194              
195             sub is_success {
196 0     0 1   my $self = shift;
197 0 0         return 1 if($self->{SUCCESS_CODES}->{$self->{CURRENT_CODE}});
198 0           return 0;
199             }
200              
201              
202             =head2 is_error
203              
204             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).
205              
206             =cut
207              
208             sub is_error {
209 0     0 1   my $self = shift;
210 0 0         return 1 if($self->{ERROR_CODES}->{$self->{CURRENT_CODE}});
211 0           return 0;
212             }
213              
214             =head1 AUTHOR
215              
216             DUPUIS Arnaud, C<< <a.dupuis@infinityperl.org> >>
217              
218             =head1 BUGS
219              
220             Please report any bugs or feature requests to
221             C<bug-Slackware-Slackget@rt.cpan.org>, or through the web interface at
222             L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Slackware-Slackget>.
223             I will be notified, and then you'll automatically be notified of progress on
224             your bug as I make changes.
225              
226             =head1 SUPPORT
227              
228             You can find documentation for this module with the perldoc command.
229              
230             perldoc Slackware::Slackget::Status
231              
232              
233             You can also look for information at:
234              
235             =over 4
236              
237             =item * Infinity Perl website
238              
239             L<http://www.infinityperl.org/category/slack-get>
240              
241             =item * slack-get specific website
242              
243             L<http://slackget.infinityperl.org>
244              
245             =item * RT: CPAN's request tracker
246              
247             L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=Slackware-Slackget>
248              
249             =item * AnnoCPAN: Annotated CPAN documentation
250              
251             L<http://annocpan.org/dist/Slackware-Slackget>
252              
253             =item * CPAN Ratings
254              
255             L<http://cpanratings.perl.org/d/Slackware-Slackget>
256              
257             =item * Search CPAN
258              
259             L<http://search.cpan.org/dist/Slackware-Slackget>
260              
261             =back
262              
263             =head1 ACKNOWLEDGEMENTS
264              
265             Thanks to Bertrand Dupuis (yes my brother) for his contribution to the documentation.
266              
267             =head1 COPYRIGHT & LICENSE
268              
269             Copyright 2005 DUPUIS Arnaud, All Rights Reserved.
270              
271             This program is free software; you can redistribute it and/or modify it
272             under the same terms as Perl itself.
273              
274             =cut
275              
276             1; # End of Slackware::Slackget::Status