Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW

Take reference of the return result from a function

by daveola (Acolyte)
on Feb 21, 2017 at 03:19 UTC ( #1182391=perlquestion: print w/replies, xml ) Need Help??

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 (Prior) 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 (Prior) 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>.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1182391]
Approved by stevieb
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (7)
As of 2021-01-18 11:13 GMT
Find Nodes?
    Voting Booth?