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.

In reply to Re: Converting scalar value to hash by greengaroo
in thread Converting scalar value to hash by Shaveta_Chawla

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.