Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation

Converting scalar value to hash

by Shaveta_Chawla (Sexton)
on Sep 21, 2012 at 10:47 UTC ( #994880=perlquestion: print w/replies, xml ) Need Help??
Shaveta_Chawla has asked for the wisdom of the Perl Monks concerning the following question:

Hi, I have a variable $results = "56/90897"; i want to display this in form of a hash. I have used
sub handler{ my $r = shift; $results = "56/90897"; $r->print({ 'response' => {'result' => $results,}, }); }
but instead of printing the value it prints hash(address), which i don't require. I am new to hashes, Please help;

Replies are listed 'Best First'.
Re: Converting scalar value to hash
by philipbailey (Chaplain) on Sep 21, 2012 at 11:43 UTC

    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.

      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?

Re: Converting scalar value to hash
by greengaroo (Hermit) on Sep 21, 2012 at 13:32 UTC

    philipbailey's explanation is good but let me put this in other words.

    Your $r->print() method receives an HashRef. HashRefs looks like this:

    my $hashref = { 'key' => 'value' };

    A real Hash looks like this:

    my %hash = ('key1' => 'value1', 'key2' => 'value2');

    You can also do this:

    my %hash = ('key1', 'value1', 'key2', 'value2');

    With the same result. You basically populate your Hash using a list.

    You already know that if you pass a list to a method, you receive the same elements in the @_ variable. But if you pass this: { 'key' => 'value' } to your method, it will receive a HashRef (in the first element of @_), a reference to a hash, so if you print it in scalar mode, you effectively print the reference address.

    What you want to do in your method, to print the content of the HashRef and not the address, you have to "dereference" it. Read perlref for more details.

    Now, here is a short example. Imagine your print method:

    sub print { my $self = shift; my ($hashref) = @_; # print the reference address: print $hashref; # dereference the hashref and print the content: print %{$hashref}; }

    Here your method receives a HashRef, but if you want your method to receive a Hash, you can change the way you pass your arguments:

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

    Personally, I would not do that. I would pass the HashRef and dereference on the other side. Now if you are stuck with your content in a real hash and you want to pass it to the same method, you have to pass its reference, like this:

    my %real_hash = ( 'response' => {'result' => $results} ); # Passing the reference of a real hash $r->print( \%real_hash );
    There are no stupid questions, but there are a lot of inquisitive idiots.
Re: Converting scalar value to hash
by Shaveta_Chawla (Sexton) on Sep 24, 2012 at 05:52 UTC
    Hi, I have used
    my %real_hash = ( 'response' => {'result' => $results} ); %real_hash = Dumper(%real_hash); $r->print( %{real_hash} );
    to print the value. Is there any optimized code other than this??

      to print the value. Is there any optimized code other than this??

      For what, what are you trying to do? What do you think that code accomplishes?

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://994880]
Approved by philipbailey
LanX ♪..♫ Fiesta, fiesta mexicana ... ♪..♫
[mandarin]: LanX aren't you a bit late to the party?

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (11)
As of 2018-06-18 17:38 GMT
Find Nodes?
    Voting Booth?
    Should cpanminus be part of the standard Perl release?

    Results (110 votes). Check out past polls.