http://www.perlmonks.org?node_id=1182391

daveola has asked for the wisdom of the Perl Monks concerning the following question:

Is there a syntax to simply turn the value of something returned from a function into a reference? For example, I want to return an array reference for an array built by map. Right now I am doing:
my @tmp = map { ... } @somearray; return \@tmp;
I'd prefer something like:
return ref_of map { ... } @somearray;
I know I could do it by writing another routine or using a library, but I was wondering if there was anything in plain old perl that can accomplish this?

Replies are listed 'Best First'.
Re: Dereference the return result from a function
by tybalt89 (Parson) on Feb 21, 2017 at 03:22 UTC
      That would be what I was looking for - I'm surprised I didn't think to just construct the array reference like that, that's pretty much exactly what I wanted.
Re: Dereference the return result from a function
by stevieb (Canon) on Feb 21, 2017 at 03:46 UTC

    As tybalt89 stated, your request doesn't quite match your examples, and he's shown how to return a reference. Here's a way to dereference an array reference returned from a sub, using the circumfix operator:

    my @derefed = @{ blah() }; sub blah { return [1, 2, 3]; }
Re: Dereference the return result from a function
by tybalt89 (Parson) on Feb 21, 2017 at 03:29 UTC

    BTW, your question doesn't match your code. Your code shows creating a reference, not derefing.

      That would evidently be from me wearing my foolish-hat. Thanks for pointing out my misstep - I've edited it to make the question more "sensical" - sorry if that makes these correct responses a little confusing.

        See the PerlMonks FAQ entry How do I change/delete my post? with particular reference to the section headed "It is uncool to update a node in a way that renders replies confusing or meaningless" which recommends use of <strike>.