I have an object which looks like this
my $result;
$result->{success} = 0;
$result->{error} = {errorType => "SHIT_ERROR"};
When I try to print it using
print "result ".\$result;
It prints
HASH(0xc191a768)
How can I actually get its content ?
At the end of the day, I want to pass this as parameter to my redirect request. Just to add more details to it. @dev-null suggested how to pass it on as json but I want to pass it like - success=0&error[errorType]=..... Kindly suggest how can that be achieved.
To get the content of $result
, you have to:
print $result->{success};
and then print error
, but please note that error
is a nested hash and if you try to print its value:
print $result->{error};
you will still get something like HASH(...)
(you'll have to print the {errorType}
element of the {error}
element).
Back to your problem. You say you want to pass this parameter to your redirect request. How do you want to implement your redirect request?
Using $m->redirect() method
If you are using $m->redirect()
I would suggest you to change your $result
to a simpler structure like this:
% my $result = {
% success => 0,
% errorType => 'SHIRT_ERROR',
% };
% $m->redirect( make_uri('error_handler', $result), 302);
this will make a uri like this one:
/error_handler?errorType=SHIRT_ERROR&success=0
and the redirect method will redirect to it. Your error_handerl.mc
then could contain something like this:
<%class>
has 'success';
has 'errorType';
</%class>
<%init>
if ($.success eq "0") {
print $.errorType;
}
</%init>
Using JSON on $m->redirect()
It looks like Mason doesn't support a query string like success=0&error[errorType]=...
The only alternative I can think of is to serialize the request with JSON like this:
component.mc
<%init>
use JSON;
my $result;
$result->{success} = 0;
$result->{error} = {errorType => "SHIRT_ERROR"};
my $h = {
result => encode_json $result
};
$m->redirect(make_uri('/error_handler', $h), 302);
</%init>
error_handler.mc
<%class>
has 'result';
</%class>
<%init>
use JSON;
my $result = decode_json $.result;
print dh $result;
</%init>
the first component will redirect to a url like this:
http://localhost:5000/error_handler?result=%7B%22success%22%3A0%2C%22error%22%3A%7B%22errorType%22%3A%22SHIRT_ERROR%22%7D%7D
I don't find it too elegant, but it works.