package Slackware::Slackget::Status;
use warnings;
use strict;
=head1 NAME
Slackware::Slackget::Status - A class for returning a status code with its explanations
=head1 VERSION
Version 1.0.100
=cut
our $VERSION = '1.0.100';
=head1 SYNOPSIS
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)
use Slackware::Slackget::Status;
my $status = Slackware::Slackget::Status->new(
codes => {
0 => "All operations goes well",
1 => "Parameters unexpected",
2 => "Network error"
}
);
print "last error message was: ",$status->to_string,"\n";
if($status->is_error)
{
die "Error: ",$status->to_string,"\n";
}
elsif($status->is_success)
{
print $status->to_string,"\n";
}
Please note that you must see at the documentation of a class to know the returned codes.
=cut
sub new
{
my ($class,%arg) = @_ ;
my $self={ CURRENT_CODE => undef };
# return undef if(!defined($arg{'codes'}) && ref($arg{codes}) ne 'HASH');
$self->{CODES} = $arg{'codes'} ;
$self->{ERROR_CODES} = $arg{'error_codes'} if($arg{'error_codes'}) ;
$self->{SUCCESS_CODES} = $arg{'success_codes'} if($arg{'success_codes'}) ;
bless($self,$class);
return $self;
}
=head1 CONSTRUCTOR
=head2 new
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 :
my $status = new Slackware::Slackget::Status (
codes => {
0 => "All good\n",
1 => "Network unreachable\n",
2 => "Host unreachable\n",
3 => "Remote file seems not exist\n"
}
);
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.
=head1 FUNCTIONS
=head2 to_string
Return the explanation string of the current status.
if($connection->fetch_file($remote_file,$local_file) > 0)
{
print "ERROR : ",$status->to_string ;
return undef;
}
else
{
...
}
=cut
sub to_string {
my $self = shift;
return $self->{SUCCESS_CODES}->{$self->{CURRENT_CODE}} if($self->{SUCCESS_CODES}->{$self->{CURRENT_CODE}}) ;
return $self->{ERROR_CODES}->{$self->{CURRENT_CODE}} if($self->{ERROR_CODES}->{$self->{CURRENT_CODE}}) ;
return $self->{CODES}->{$self->{CURRENT_CODE}} ;
}
=head2 to_int
Same as to_string but return the code number.
=cut
sub to_int {
my $self = shift;
return $self->{CURRENT_CODE} ;
}
=head2 to_XML (deprecated)
Same as to_xml(), provided for backward compatibility.
=cut
sub to_XML {
return to_xml(@_);
}
=head2 to_xml
return an xml ecoded string, represented the current status. The XML string will be like that :
Status
code : ".$self->to_int()."
description : ".$self->to_string()."
\n