http://www.perlmonks.org?node_id=994885


in reply to Converting scalar value to hash

You don't tell us what the argument to your handler subroutine is, although it is evidently an object of some type, as you call a print method on it. Since we do not know what $r is, we can only guess what arguments its print method accepts.

Your code:

$r->print({ 'response' => {'result' => $results,}, });

calls print with a an anonymous hash reference as the sole argument, which appears to have been stringified by that method, and you indicate that is not what you want. Perl does not actually allow hashes to be directly passed as arguments to subroutines: you can either pass a reference to a hash (which didn't have the behaviour you wanted in this case) or provide a list of alternating key/value pairs, which can be turned into a hash by the subroutine (if it is written that way). The latter would look like this:

$r->print(response => {result => $results});

Whether this works for you depends on what exactly the object $r is.

Replies are listed 'Best First'.
Re^2: Converting scalar value to hash
by Anonymous Monk on Sep 22, 2012 at 21:18 UTC

    Well, judging from the subroutine name (sub handler) and the variable $r, I'm betting it's code running under mod_perl and $r is the HTTP request object.

    What's the OP fails to state is how he expects his hash to print. One way or other he needs to serialise it (transform it into a string suitable for outputting). Perhaps Data::Dumper? Storable? But I can't imagine shoving a Perl-style hash to a client to be of much use. Perhaps he wants the hash in one of the markup formats such as XML or YAML or JSON instead?