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

Re: on the fly reference to subroutine output

by choroba (Abbot)
on Mar 04, 2013 at 14:51 UTC ( #1021662=note: print w/ replies, xml ) Need Help??


in reply to on the fly reference to subroutine output

Use an anonymous array:

sub2( [ sub1() ] );
لսႽ ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ


Comment on Re: on the fly reference to subroutine output
Download Code
Re^2: on the fly reference to subroutine output
by tmharish (Friar) on Mar 04, 2013 at 14:54 UTC

    Just as a note - this beautiful solution works cause Perl flattens arrays.

    You can see that when you do:

    sub2( [ sub1(), (), () ] );
      Thank you all. It does the "magic".
      How does 'flattening' come into play when there's only one array? I would think 'unwiding' of an array is what takes place.

        There are two arrays ( one inside the other ). The outer array is the anonymous array as shown below:

        sub2( [ sub1() ] ); ^ ^ | | | --------- Inner array ------------ Outer array
        sub 2 now gets the ref of the outer anonymous array which is flattened.
Re^2: on the fly reference to subroutine output
by thirdm (Sexton) on Mar 04, 2013 at 18:30 UTC

    This will make a copy of the array and give a reference to that, right? Is there a way to get a reference directly to the array passed back by sub1 with some assurance the copy won't happen? Or is this the sort of optimization that is up to the compiler (and may or may not happen?)?

    This example makes me think of rvalue references and move semantics in the new C++, where you can now return a std::vector, say, and be assured that the copy will really only grab the innards of the temporary returned rather than allocate anything new and copy things over. I wonder if there's something roughly equivalent in Perl (5 or 6).

      The incorrect assumption here is that sub1 is returning an array.

      In perl5, subs can return scalars or lists. Since it's not returning a (scalar) reference to an array, the only option is to store the list into a (possibly anonymous) array and pass a reference into sub2

      tjd

        Ah, I'm always forgetting this.

        But underneath, is there a chance at some kind of return value optimization? Say if you have...

        sub1 { ... return @a; } $r = [sub1];
        Even if it's officially turning the values of @a into a list en route to copying them into the array that the brackets create a reference to, could perl not notice @a is no longer needed otherwise and instead simply make [sub1] be \@a behind the scenes? It would be neat if you could write sub1 that way instead of making it return \@a even if you anticipated the copy to be expensive.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1021662]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (5)
As of 2014-09-20 16:37 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (160 votes), past polls