I am trying to setup a basic error checking system where it will catch shell errors run by a system call. execute_command is a webmin function that runs a system call and then sets an error message to its 4th parameter. I basically call execute_command_error("adduser test"), knowing that I already have a user called test created and based on my predefined arrays, id expect it to print
Unable to add user
Unable to add that user because it already exists on the system.
but instead I get:
Uhhhhhhhhh?
Uhhhhhhhhh?
I have verified that $exe and $return are "adduser" and 1, respectifully. What am I not understanding about arrays? It seems to ignore the string and or number and just go by the last definition with 3 elements. What is a solution to this, or a better solution?
Here is ths code:
$ErrorMsg['adduser',1,'title'] = "Unable to add user";
$ErrorMsg['adduser',1,'msg'] = "Unable to add that user because it already exists on the system.";
$ErrorMsg['random',2,'duaisdhai'] = "Uhhhhhhhhh?";
sub execute_command_error
{
my $error = "";
my $cmd = $_[0];
$return = execute_command($cmd, undef, undef, \$error)>>8;
if ($error) {
my ($exe) = $cmd =~ m|^(.*?)[ ]|;
$exe_title = $ErrorMsg[$exe,$return,'title'];
$exe_msg = $ErrorMsg[$exe,$return,'msg'];
print $exe_title."<br>";
print $exe_msg ."<br>";
}
}
Update:
I am thinking that I need to use hashes, I have no idea why I thought I could use strings in indices. With that said, little research has led me to something like this:
%ErrorMsgs = ('adduser' => {
'1' => {
'title' => 'Unable to add user',
'msg' => 'Unable to add that user because it already exists on the system.',
},
},
);
Now how would I reference it using a variable? because neither of these work:
$exe_title = $ErrorMsgs{"$exe"}{"$return"}{"title"};
$exe_title = $ErrorMsgs{$exe}{$return}{title};