3
0
mirror of https://github.com/pragma-/pbot.git synced 2024-12-24 11:42:35 +01:00

HashObject: improve error messages for removing non-existing items

This commit is contained in:
Pragmatic Software 2020-02-11 22:56:42 -08:00
parent 20695d5f73
commit fae9e866ee

View File

@ -158,9 +158,12 @@ sub unset {
return $result;
}
delete $self->{hash}->{$lc_index}->{$key};
$self->save;
return "[$self->{name}] $self->{hash}->{$lc_index}->{_name}: $key unset.";
if (defined delete $self->{hash}->{$lc_index}->{$key}) {
$self->save;
return "[$self->{name}] $self->{hash}->{$lc_index}->{_name}: $key unset.";
} else {
return "[$self->{name}] $self->{hash}->{$lc_index}->{_name}: $key does not exist.";
}
}
sub exists {
@ -208,15 +211,22 @@ sub remove {
}
if (defined $data_index) {
delete $self->{hash}->{$lc_index}->{$data_index};
delete $self->{hash}->{$lc_index} if keys(%{$self->{hash}->{$lc_index}}) == 1;
$self->save unless $dont_save;
return "$self->{hash}->{$lc_index}->{_name}.$data_index removed from $self->{name}";
if (defined delete $self->{hash}->{$lc_index}->{$data_index}) {
delete $self->{hash}->{$lc_index} if keys(%{$self->{hash}->{$lc_index}}) == 1;
$self->save unless $dont_save;
return "$self->{hash}->{$lc_index}->{_name}.$data_index removed from $self->{name}";
} else {
return "$self->{name}: $self->{hash}->{$lc_index}->{_name}.$data_index does not exist.";
}
}
my $data = delete $self->{hash}->{$lc_index};
$self->save unless $dont_save;
return "$data->{_name} removed from $self->{name}.";
if (defined $data) {
$self->save unless $dont_save;
return "$data->{_name} removed from $self->{name}.";
} else {
return "$self->{name}: $data_index does not exist.";
}
}
1;