Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

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":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others exploiting the Monastery: (6)
As of 2024-04-19 08:53 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found