Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Creating reference to hash and array

by vinoth.ree (Monsignor)
on Apr 10, 2009 at 14:25 UTC ( [id://756833]=perlquestion: print w/replies, xml ) Need Help??

vinoth.ree has asked for the wisdom of the Perl Monks concerning the following question:

use Data::Dumper; sub Create_arrayref{ [@_]; }; sub Create_hashref{ {@_}; }; my $hash_ref=&Create_hashref(1,2,3,4,5,6,7,8,9,'ree'); print Dumper $hash_ref; my $array_ref=&Create_arrayref(1,2,3,4,5,6,7,8,9,'ree'); print Dumper $array_ref;

In the above code I tried to create the reference for the array and hash, For array I was getting array reference correctly but, for hash I was getting 10, When I use return like return {@_};it giving hash reference, why it is like that any specific reason ?

Vinoth,G

Replies are listed 'Best First'.
Re: Creating reference to hash and array
by gone2015 (Deacon) on Apr 10, 2009 at 15:00 UTC

    The problem is that:

    sub Create_hashref{ {@_}; };
    is less than unambiguous... It might look like an anonymous hash constructor to you, but it's actually a BLOCK containing the rather trivial expression @_, whose value is the length of the @_ array. This:
    sub Create_hashref{ return {@_}; };
    takes a different view. As does:
    sub Create_hashref{ +{@_}; };

    This is discussed in Making References -- see paragraph 3.

      Fantastic! Thanks

      Vinoth,G
Re: Creating reference to hash and array
by Bloodnok (Vicar) on Apr 10, 2009 at 14:49 UTC
    When used without a preceding return, perl interprets {@_}; as a code block and as a list is the only statement in a block called in scalar context, perl returns the number of elements in the list i.e. 10, as expected.

    Thanx to a previous posting from ikegami, I now know that the way to get the desired result is to force perl to treat the braces as an anonymous hash by prefixing with a '+' i.e.

    sub Create_hashref{ +{@_}; };

    A user level that continues to overstate my experience :-))
      From a personal point of view, I'd strongly suggest using return {@_} rather than +{@_}.

      If I encountered code with that "+" in it, I'd either waste quite some time wondering what it's doing there, or accidentally break the code when updating it! Using "return" is unlikely to confuse anyone.

      Of course, there may be are other circumstances where "+" is the only way to get the desired result. (Can anyone concoct such an example?) In those cases, I'd have to rely on an informative comment next to it. But hey, we're all diligent with our comments aren't we? :-)

      --
      use JAPH;
      print JAPH::asString();

        Indeed, the PBP recommends such practice, but I answered the question as asked.

        A user level that continues to overstate my experience :-))
Re: Creating reference to hash and array
by kennethk (Abbot) on Apr 10, 2009 at 14:37 UTC

    On the other hand, if you use the code:

    sub Create_hashref{ return {@_}; };

    you get your expected result. It appears that in the absence of an operator, Perl interprets {} as a block and not an anonymous hash.

Re: Creating reference to hash and array
by jethro (Monsignor) on Apr 10, 2009 at 14:35 UTC
    sub Create_hashref{ { my $i=1; @_; }; }

    Try this, it gives the same result of "10". '{' is also used as block delimiter and when the parser has to guess if it is used as a block delimiter or a hash reference, it might guess wrong.

Re: Creating reference to hash and array
by Anonymous Monk on Apr 10, 2009 at 14:36 UTC
Re: Creating reference to hash and array
by bichonfrise74 (Vicar) on Apr 10, 2009 at 16:51 UTC
    Maybe you meant something like this?
    print Dumper \$hash_ref; print Dumper \$array_ref;

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://756833]
Approved by Bloodnok
Front-paged by targetsmart
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others taking refuge in the Monastery: (4)
As of 2024-04-20 17:17 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found