in reply to Creating reference to hash and array

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

Replies are listed 'Best First'.
Re^2: Creating reference to hash and array
by wol (Hermit) on Apr 14, 2009 at 11:11 UTC
    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 :-))