I just want to know what the process is behind merging a value into a hash. I have a hash which has 5 to 6 keys depending on if the error outputs runtime values. The method that takes in the arguments also take an error message string in first, also. I want it to be able to add this error message string into the hash, to make one big hash basically.
This is how the method would be called:
ASC::Builder::Error->new("Simple error message here", code => "UNABLE_TO_PING_SWITCH_ERROR", switch_ip => $ip3, timeout => $t1);
The last two values assign runtime parameters/values to keys inside the context key in the error hash.
Here is a look at the error hash:
use constant ERROR_CODE => { UNABLE_TO_PING_SWITCH_ERROR => { category => 'Connection Error', template => 'Could not ping switch %s in %s seconds.', context => [qw(switch_ip timeout)], tt => {template => 'disabled'}, fatal => 1, wiki_page => 'www.error-solution.com/ERROR_CODE_A', } };
Here is my method to manipulate the error hash and construct the message
sub _create_error_hash {
my $error_string = shift; if(defined($params{code}) {
my $first_param = delete $params{code};
foreach my $key (@{$first_param->{context}}) {
$first_param->{$key} = $key;
}
my @template_args = map { $first_param->{$_}} @{$first_param->{context} };
$first_param->{message} = sprintf($first_param->{template}, @template_args); }
return bless $first_param;
}
sub _merge_hashes {
my ($message = {message => $messsage}, $first_param = {first_param => $first_param}) = @ _;
#merge these two hashes and bless into $class (don't know how to merge hashes)
my %merged_hash = ($message, $first_param);
return bless $merged_hash, $class;
}
The output of _create_hash
should be the input for _merge_hashes
Not sure if I have handled that properly. These methods will be use inside the new method (which is a mess right now) hence why it's not included.
That's just an attempt , of an example I seen on perlmonks, Here is the link: http://www.perlmonks.org/?node_id=14263